Spring Boot注解

@RestController和@RequestMapping注解

咱们的Example类上使用的第一个注解是 @RestController 。这被称为一个构造型(stereotype)注解。它为阅读代码的人们提供建议。对于Spring,该类扮演了一个特殊角色。在本示例中,咱们的类是一个web @Controller ,因此当处理进来的web请求时,Spring会询问它。@RequestMapping 注解提供路由信息。它告诉Spring任何来自"/"路径的HTTP请求都应该被映射到 home 方法。 @RestController 注解告诉Spring以字符串的形式渲染结果,并直接返回给调用者。@RestController 和 @RequestMapping 注解是Spring MVC注解(它们不是Spring Boot的特定部分)java

@EnableAutoConfiguration注解

这个注解告诉Spring Boot根据添加的jar依赖猜想你想如何配置Spring。因为 spring-boot-starter-web 添加了Tomcat和Spring MVC,因此auto-configuration将假定你正在开发一个web应用并相应地对Spring进行设置。Starter POMs和Auto-Configuration:设计auto-configuration的目的是更好的使用"Starter POMs",但这两个概念没有直接的联系。你能够自由地挑选starter POMs之外的jar依赖,而且Spring Boot将仍旧尽最大努力去自动配置你的应用。web

你能够经过将 @EnableAutoConfiguration 或 @SpringBootApplication 注解添加到一个 @Configuration 类上来选择自动配置。
注:你只须要添加一个 @EnableAutoConfiguration 注解。咱们建议你将它添加到主 @Configuration 类上。spring

若是发现应用了你不想要的特定自动配置类,你可使用 @EnableAutoConfiguration 注解的排除属性来禁用它们。sql

import org.springframework.boot.autoconfigure.*;
import org.springframework.boot.autoconfigure.jdbc.*;
import org.springframework.context.annotation.*;
@Configuration
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class MyConfiguration {
}

@Configuration

Spring Boot提倡基于Java的配置。尽管你可使用一个XML源来调用 SpringApplication.run() ,咱们一般建议你使用 @Configuration 类做为主要源。通常定义 main 方法的类也是主要 @Configuration 的一个很好候选。你不须要将全部的 @Configuration 放进一个单独的类。 @Import 注解能够用来导入其余配置类。另外,你也可使用 @ComponentScan 注解自动收集全部的Spring组件,包括 @Configuration 类。app

若是你绝对须要使用基于XML的配置,咱们建议你仍旧从一个 @Configuration 类开始。你可使用附加的 @ImportResource 注解加载XML配置文件。框架

@Configuration注解该类,等价 与XML中配置beans;用@Bean标注方法等价于XML中配置bean。spring-boot

@ComponentScan

你能够自由地使用任何标准的Spring框架技术去定义beans和它们注入的依赖。简单起见,咱们常常使用 @ComponentScan 注解搜索beans,并结合 @Autowired 构造器注入。
若是使用上面建议的结构组织代码(将应用类放到根包下),你能够添加 @ComponentScan 注解而不须要任何参数。你的全部应用程序组件( @Component , @Service , @Repository , @Controller 等)将被自动注册为Spring Beans。this

@SpringBootApplication

不少Spring Boot开发者老是使用 @Configuration , @EnableAutoConfiguration 和 @ComponentScan 注解他们的main类。因为这些注解被如此频繁地一块使用(特别是你遵循以上最佳实践时),Spring Boot提供一个方便的 @SpringBootApplication 选择。
该 @SpringBootApplication 注解等价于以默认属性使用 @Configuration , @EnableAutoConfiguration 和 @ComponentScan 。spa

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);
    }
}

@ConfigurationProperties

属性注入.net

@Component
@ConfigurationProperties(prefix="connection")
public class ConnectionSettings {
    private String username;
    private InetAddress remoteAddress;
    // ... getters and setters
}

为了使用@ConfigurationProperties beans,你可使用与其余任何bean相同的方式注入它们

@Service
public class MyService {
    @Autowired
    private ConnectionSettings connection;
    //...
    @PostConstruct
    public void openConnection() {
        Server server = new Server();
        this.connection.configure(server);
    }
}

正如使用@ConfigurationProperties注解一个类,你也能够在@Bean方法上使用它。当你须要绑定属性到不受你控制的第三方组件时,这种方式很是有用。

为了从Environment属性配置一个bean,将@ConfigurationProperties添加到它的bean注册过程:

@ConfigurationProperties(prefix = "foo")
@Bean
public FooComponent fooComponent() {
...
}

和上面ConnectionSettings的示例方式相同,任何以foo为前缀的属性定义都会被映射到FooComponent上。

Spring Boot将尝试校验外部的配置,默认使用JSR-303(若是在classpath路径中)。你能够轻松的为你的@ConfigurationProperties类添加JSR-303 javax.validation约束注解:

@Component
@ConfigurationProperties(prefix="connection")
public class ConnectionSettings {
@NotNull
private InetAddress remoteAddress;
// ... getters and setters
}

@EnableConfigurationProperties

当@EnableConfigurationProperties注解应用到你的@Configuration时,任何被@ConfigurationProperties注解的beans将自动被Environment属性配置

你能够经过在@EnableConfigurationProperties注解中直接简单的列出属性类来快捷的注册@ConfigurationProperties bean的定义。

@Configuration
@EnableConfigurationProperties(ConnectionSettings.class)
public class MyConfiguration {
}

@Component和@Bean

@Component被用在要被自动扫描和装配的类上。@Component类中使用方法或字段时不会使用CGLIB加强(及不使用代理类:调用任何方法,使用任何变量,拿到的是原始对象)Spring 注解@Component等效于@Service,@Controller,@Repository
@Bean主要被用在方法上,来显式声明要用生成的类;用@Configuration注解该类,等价 与XML中配置beans;用@Bean标注方法等价于XML中配置bean。

如今项目上,本工程中的类,通常都使用@Component来生成bean。在把经过web service取得的类,生成Bean时,使用@Bean和getter方法来生成bean

@Profiles

Spring Profiles提供了一种隔离应用程序配置的方式,并让这些配置只能在特定的环境下生效。任何@Component或@Configuration都能被@Profile标记,从而限制加载它的时机。

@Configuration
@Profile("production")
public class ProductionConfiguration {
// ...
}

以正常的Spring方式,你可使用一个spring.profiles.active的Environment属性来指定哪一个配置生效。你可使用日常的任何方式来指定该属性,例如,能够将它包含到你的application.properties中:

spring.profiles.active=dev,hsqldb

相关文章
相关标签/搜索