博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Boot 学习笔记2 - Spring Bean 和依赖
阅读量:5759 次
发布时间:2019-06-18

本文共 4459 字,大约阅读时间需要 14 分钟。

hot3.png

17.Spring bean 和依赖注入

你可以自由的使用任何一种 spring 框架技术定义 bean 和他们之间的依赖. 简单起见,我们通常通过
@ComponentScan 寻找你的 beans,结合使用
@Autowired构造器注入工作的很好. 如果你的代码结构使用上面建议的方式 (放置你的应用类在顶层包), 你可以添加
@ComponentScan 不需要任何参数. 所有你的应用组件 (@Component, , @Repository, @Controller 等.) 讲自动的注册为 Spring Bean. 这里是一个例子 @Service Bean 使用构造器注入获得必须的
RiskAssessor bean.
package com.example.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;@Servicepublic class DatabaseAccountService implements AccountService {    private final RiskAssessor riskAssessor;    @Autowired    public DatabaseAccountService(RiskAssessor riskAssessor) {        this.riskAssessor = riskAssessor;    }    // ...}
如果这个类只有一个构造函数,则可以省略
@Autowired.
@Servicepublic class DatabaseAccountService implements AccountService {    private final RiskAssessor riskAssessor;    public DatabaseAccountService(RiskAssessor riskAssessor) {        this.riskAssessor = riskAssessor;    }    // ...}
注意什么时候使用构造器注入,允许 riskAssessor 成员变量 标记为
final,说明它后续讲不能被修改.

18. 使用 @SpringBootApplication 注解

很多 Spring Boot developers 始终将他们的主类(main class) annotated with
@Configuration,
@EnableAutoConfiguration and
@ComponentScan. 因为这些注解通常一起使用 (尤其是遵循最佳实践的时候), Spring Boot 提供了一个方便的
@SpringBootApplication 注解可以作为一个选择. 这个
@SpringBootApplication 注解等同于使用
@Configuration,
@EnableAutoConfiguration
@ComponentScan 默认参数的情况:
package com.example.myproject;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScanpublic class Application {    public static void main(String[] args) {        SpringApplication.run(Application.class, args);    }}
[注意]
@SpringBootApplication 支持别名自定义
@EnableAutoConfiguration
@ComponentScan的属性.

19. 启动你的应用

打包你的应用为一个 jar 使用内嵌的 HTTP 服务最大的优势是你不受其他影响. 调试 Spring Boot 应用也非常简单;不需要特定的 IDE 插件或是扩展.
[注意]这一章节只包含 jar 的打包部分,如果你打算将你的应用打包为一个 war 文件你应该参考你的服务器和 IDE 文档.

19.1 在IDE中运行

You can run a Spring Boot application from your IDE as a simple Java application, however, first you will need to import your project. Import steps will vary depending on your IDE and build system. Most IDEs can import Maven projects directly, for example Eclipse users can select Import…​ → Existing Maven Projects from the File menu. If you can’t directly import your project into your IDE, you may be able to generate IDE metadata using a build plugin. Maven includes plugins for Eclipse and IDEA; Gradle offers plugins for various IDEs.
[注意]If you accidentally run a web application twice you will see a “Port already in use” error. STS users can use the Relaunch button rather than Run to ensure that any existing instance is closed.

19.2 运行一个打包好的应用

If you use the Spring Boot Maven or Gradle plugins to create an executable jar you can run your application using java -jar. For example: $ java -jar target/myproject-0.0.1-SNAPSHOT.jar It is also possible to run a packaged application with remote debugging support enabled. This allows you to attach a debugger to your packaged application:
$ java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n \   -jar target/myproject-0.0.1-SNAPSHOT.jar

19.3 Using the Maven plugin

The Spring Boot Maven plugin includes a run goal which can be used to quickly compile and run your application. Applications run in an exploded form just like in your IDE. $ mvn spring-boot:run You might also want to use the useful operating system environment variable: $ export MAVEN_OPTS=-Xmx1024m -XX:MaxPermSize=128M

19.4 Using the Gradle plugin

The Spring Boot Gradle plugin also includes a bootRun task which can be used to run your application in an exploded form. The bootRun task is added whenever you import the spring-boot-gradle-plugin: $ gradle bootRun You might also want to use this useful operating system environment variable:
$ export JAVA_OPTS=-Xmx1024m -XX:MaxPermSize=128M

19.5 Hot swapping

Since Spring Boot applications are just plain Java applications, JVM hot-swapping should work out of the box. JVM hot swapping is somewhat limited with the bytecode that it can replace, for a more complete solution JRebel or the Spring Loaded project can be used. The spring-boot-devtools module also includes support for quick application restarts. See the Chapter 20, Developer tools section below and the Hot swapping “How-to” for details.

tips:

本文由导入,原文链接:

转载于:https://my.oschina.net/yangyan/blog/859425

你可能感兴趣的文章
calc code execeute time on c++
查看>>
python glob model(转)
查看>>
有的句子不长,却能鼓舞我们,成为我们坚持下去的动力[摘抄]
查看>>
direct-io-zonian-ChinaUnix博客
查看>>
jquery websocket 插件
查看>>
学着克服平台期
查看>>
static用法小结
查看>>
IaaS、SaaS、PaaS
查看>>
mvc3之URL篇
查看>>
使用 Sails.js 构建和定制企业级的 Node.js 应用
查看>>
颜色控件【MFC】自己写一个ListCtrl控件(实现栅格属性表的编辑)-java教程
查看>>
宋体微软合格JAVA工程师要学那些技术?-java教程
查看>>
我的sublime插件 javascript版本
查看>>
jquery学习笔记---jqGrid学习笔记 完整整理
查看>>
文件信息我的学习生涯(Delphi篇) - 11
查看>>
女人不狠,立足不稳
查看>>
Spread Studio中文支持图解
查看>>
PostgreSQL在VFD管理上是一套还是多套
查看>>
命令服务删除virtual bridge
查看>>
搜索搜索引擎SEO的艺术(原书第2版)
查看>>