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

0
(0)

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.

这篇文章有用吗?

平均评分 0 / 5. 投票数: 0

到目前为止还没有投票!成为第一位评论此文章。

很抱歉,这篇文章对您没有用!

让我们改善这篇文章!

告诉我们我们如何改善这篇文章?

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据