@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Component public @interface Configuration { String value() default ""; }
@Configuration底層是含有@Component ,所以@Configuration 具有和 @Component 的作用。
@Configuration可理解為用spring的時候xml里面的<beans>標簽。
@Configuration標注在類上,相當於把該類作為spring的xml配置文件中的<beans>,作用為:配置spring容器(應用上下文) package com.dsx.demo; import org.springframework.context.annotation.Configuration; @Configuration public class TestConfiguration { public TestConfiguration() { System.out.println("TestConfiguration容器啟動初始化。。。"); } }
相當於:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd" default-lazy-init="false"> </beans>
@Bean可理解為用spring的時候xml里面的<bean>標簽。
@Bean標注在方法上(返回某個實例的方法),等價於spring的xml配置文件中的<bean>,作用為:注冊bean對象 package com.dsx.demo; public class TestBean { private String username; private String url; private String password; public void sayHello() { System.out.println("TestBean sayHello..."); } public String toString() { return "username:" + this.username + ",url:" + this.url + ",password:" + this.password; } public void start() { System.out.println("TestBean 初始化。。。"); } public void cleanUp() { System.out.println("TestBean 銷毀。。。"); } }
配置類
package com.dsx.demo; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Scope; @Configuration public class TestConfiguration { public TestConfiguration() { System.out.println("TestConfiguration容器啟動初始化。。。"); } // @Bean注解注冊bean,同時可以指定初始化和銷毀方法 @Bean @Scope("prototype") public TestBean testBean() { return new TestBean(); } }
上述操作相當於實例化TestBean ,並交給spring管理。
注:
(1)、@Bean注解在返回實例的方法上,如果未通過@Bean指定bean的名稱,則默認與標注的方法名相同;
(2)、@Bean注解默認作用域為單例singleton作用域,可通過@Scope(“prototype”)設置為原型作用域;
(3)、既然@Bean的作用是注冊bean對象,那么完全可以使用@Component、@Controller、@Service、@Repository等注解注冊bean(在需要注冊的類上加注解),當然需要配置@ComponentScan注解進行自動掃描。
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Bean { @AliasFor("name") String[] value() default {}; @AliasFor("value") String[] name() default {}; Autowire autowire() default Autowire.NO; String initMethod() default ""; String destroyMethod() default "(inferred)"; }
使用@Bean注解時,可以配置initMethod()和destoryMethod方法,分別在實例化和銷毀的時候執行。
或者使用通過@PostConstruct 和 @PreDestroy 方法 實現初始化和銷毀bean之前進行的操作 。
Spring Boot不是spring的加強版,所以@Configuration和@Bean同樣可以用在普通的spring項目中。