飞5的Spring Boot2(5)- SpringApplication和Configuration

SpringApplication

Spring Boot中最重要的注解之一。通常建议放在文件的根目录下,由于它会隐式的扫描它下面的全部目录。java

目录结构如代码所示:spring

 1com
2 +- example
3     +- myapplication
4         +- Application.java
5         |
6         +- customer
7         |
  +- Customer.java
8         |   +- CustomerController.java
9         |
  +- CustomerService.java
10         |   +- CustomerRepository.java
11         |

12         +- order
13             +- Order.java
14             +- OrderController.java
15             +- OrderService.java
16             +- OrderRepository.java

@Configuration

从Spring3.0,@Configuration用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,并用于构建bean定义,初始化Spring容器。ruby

注意:@Configuration注解的配置类有以下要求:微信

  • @Configuration不能够是final类型;app

  • @Configuration不能够是匿名类;测试

  • 嵌套的configuration必须是静态类。this

@Configuration配置spring并启动spring容器url

@Configuration标注在类上,至关于把该类做为spring的xml配置文件中的 ,做用为:配置spring容器(应用上下文) spa

1import org.springframework.context.annotation.Configuration;
2@Configuration
3public class TestConfiguration {
4    public TestConfiguration() {
5        System.out.println("TestConfiguration容器启动初始化。。。");
6    }
7}

至关于.net

 1<?xml version="1.0" encoding="UTF-8"?>
2<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3    xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
4    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
5    xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="
6        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
7        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
8        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
9        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
10        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
11        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
12        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd"
default-lazy-init="false">

13</beans>

@Bean

@Bean标注在方法上(返回某个实例的方法),等价于spring的xml配置文件中的 ,做用为:注册bean对象。

新建测试bean

 1public class TestBean {
2    private String username;
3    private String url;
4    private String password;
5    public void sayHello() {
6        System.out.println("TestBean sayHello...");
7    }
8    public String toString() {
9        return "username:" + this.username + ",url:" + this.url + ",password:" + this.password;
10    }
11    public void start() {
12        System.out.println("TestBean 初始化。。。");
13    }
14    public void cleanUp() {
15        System.out.println("TestBean 销毁。。。");
16    }
17}

配置类中注入

 1import org.springframework.context.annotation.Bean;
2import org.springframework.context.annotation.Configuration;
3import org.springframework.context.annotation.Scope;
4@Configuration
5public class TestConfiguration {
6    public TestConfiguration() {
7        System.out.println("TestConfiguration容器启动初始化。。。");
8    }
9    // @Bean注解注册bean,同时能够指定初始化和销毁方法
10    // @Bean(name="testBean",initMethod="start",destroyMethod="cleanUp")
11    @Bean
12    @Scope("prototype")
13    public TestBean testBean() {
14        return new TestBean();
15    }
16}

加载而且测试:

 1mport org.springframework.context.ApplicationContext;
2import org.springframework.context.annotation.AnnotationConfigApplicationContext;
3public class TestMain {
4    public static void main(String[] args) {
5        // @Configuration注解的spring容器加载方式,用AnnotationConfigApplicationContext替换ClassPathXmlApplicationContext
6        ApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
7        // 若是加载spring-context.xml文件:
8        // ApplicationContext context = new
9        // ClassPathXmlApplicationContext("spring-context.xml");
10         //获取bean
11        TestBean tb = (TestBean) context.getBean("testBean");
12        tb.sayHello();
13    }
14}

获得测试若是:

注: 
(1)、@Bean注解在返回实例的方法上,若是未经过@Bean指定bean的名称,则默认与标注的方法名相同; 
(2)、@Bean注解默认做用域为单例singleton做用域,可经过@Scope(“prototype”)设置为原型做用域; 
(3)、既然@Bean的做用是注册bean对象,那么彻底能够使用@Component、@Controller、@Service、@Ripository等注解注册bean,固然须要配置@ComponentScan注解进行自动扫描。

本文分享自微信公众号 - soft张三丰(aguzhangsanfeng)。
若有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一块儿分享。

相关文章
相关标签/搜索