前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Boot 学习笔记2 – Spring Bean 和依赖

Spring Boot 学习笔记2 – Spring Bean 和依赖

作者头像
前Thoughtworks-杨焱
发布2021-12-08 08:30:16
4580
发布2021-12-08 08:30:16
举报
文章被收录于专栏:杨焱的专栏

17.Spring bean 和依赖注入

你可以自由的使用任何一种 spring 框架技术定义 bean 和他们之间的依赖. 简单起见,我们通常通过@ComponentScan 寻找你的 beans,结合使用@Autowired构造器注入工作的很好.

如果你的代码结构使用上面建议的方式 (放置你的应用类在顶层包), 你可以添加 @ComponentScan 不需要任何参数. 所有你的应用组件 (@Component, @Service, @Repository, @Controller 等.) 讲自动的注册为 Spring Bean.

这里是一个例子 @Service Bean 使用构造器注入获得必须的RiskAssessor bean.

代码语言:javascript
复制
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.

代码语言:javascript
复制
@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 默认参数的情况:

代码语言:javascript
复制
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 插件或是扩展.

代码语言:javascript
复制
[注意]
这一章节只包含 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.

代码语言:javascript
复制
[注意]
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:

代码语言:javascript
复制
$ 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:

代码语言:javascript
复制
$ 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.

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-03-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 18. 使用 @SpringBootApplication 注解
  • 19. 启动你的应用
    • 19.1 在IDE中运行
      • 19.2 运行一个打包好的应用
        • 19.3 Using the Maven plugin
          • 19.4 Using the Gradle plugin
            • 19.5 Hot swapping
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档