Spring Boot 学习笔记2 – Spring Bean 和依赖
17.Spring bean 和依赖注入
你可以自由的使用任何一种 spring 框架技术定义 bean 和他们之间的依赖. 简单起见,我们通常通过@ComponentScan
寻找你的 beans,结合使用@Autowired
构造器注入工作的很好.
如果你的代码结构使用上面建议的方式 (放置你的应用类在顶层包), 你可以添加 @ComponentScan
不需要任何参数. 所有你的应用组件 (@Component, @Service, @Repository, @Controller 等.) 讲自动的注册为 Spring Bean.
这里是一个例子 @Service Bean 使用构造器注入获得必须的RiskAssessor
bean.
package com.example.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class DatabaseAccountService implements AccountService {
private final RiskAssessor riskAssessor;
@Autowired
public DatabaseAccountService(RiskAssessor riskAssessor) {
this.riskAssessor = riskAssessor;
}
// ...
}
如果这个类只有一个构造函数,则可以省略 @Autowired
.
@Service
public 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 @ComponentScan
public 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.
相关文章
- 微软:想要直升 Windows11,需运行 Windows10 20H1 或更新版本
- 微软 Windows 11 Build 22000.65 更新(KB5004745)发布:新增搜索框,任务栏支持多屏显示
- 网友吐槽 Windows 11 预览版 22000 部分界面无圆角,无新文件资源管理器,微软回应:会推出越来越多功能和
- 微软开启 Windows 11 Bug Bash,参与即可获得成就徽章
- 微软暗示Windows 11生命周期为10年:和Windows 10一样
- 微软禁止修改Windows 11任务栏位置 修改注册表可移到顶部
- 微软预告:未来数月Windows 11将会获得一系列新功能
- 微软:升级 Windows 11 十天内可轻松降级到 Windows 10
- 微软宣布暂停自家 Windows 容器的 SQL Server:去用 Linux 版吧
- 微软现发布紧急更新,力求修复 Windows 7/8/8.1/10 零日漏洞
- 不少用户提前尝鲜新版Office:全新用户界面来了
- Windows 11 任务栏基于 UWP(XAML)打造,但不少功能退化或缺失,微软回应
- 那些Windows 11任务栏消失的重要功能:不允许拖拽应用/快捷方式
- 微软 Windows 10 21H2 版本号将是 Build 19044,更多新功能曝光
- 微软 Windows 10 21H2 有望年内发布,更多新功能曝光
- 微软:Windows 10 免费升级 Windows 11 有时间限制,但将至少持续一年
- 微软:大多数符合条件设备Windows 11升级实际将于2022年初完成
- 注意了!Windows 11用户将被允许在10天内轻松降级至Windows 10
- 微软 Windows11 全新设置功能一览,加快替代控制面板
- Windows 7/8.1/10免费升级Windows 11有时间限制!微软给出答案