目录
关于Configuration
@Configuration+@Bean注册Bean
@Configuration+@Component注册Bean
@configuration中引入其它注解配置
@configuration嵌套静态Configuration类(须是静态类)
关于@Bean
@Bean定义生命周期的回调
@Bean的自定义命名
@Bean多个名字
@Bean定义描述信息
@Bean和@Scope的关系
@Configuration用于定义配置类,可替换xml配置文件,@Configuration就要提到他的好朋友@Bean,两个注解在一起可以创建简单的spring配置类,被注解的类内部包含一个或者多个被@Bean注解的方法,这些方法会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,并用于构建bean定义,初始化Spring容器。
原本的xml文件:
对等于@Configuration启动容器+@Bean注册Bean:
@Configuration
public class PeopleConfiguration {
@Bean
public People people() {
People people = new People();
//注入
people.setEyes(eyes());
//注册bean
return people;
}
@Bean
public Eyes eyes() {
return new Eyes();
}
}
相关类:
public class People {
public Eyes eyes;
public void sayHello() {
System.out.println("oh, hey man");
}
public void setEyes(Eyes eyes) {
this.eyes = eyes;
}
public Eyes getEyes() {
return eyes;
}
public void init() {
System.out.print("初始化");
}
public void destroy() {
System.out.print("销毁");
}
}
public class Eyes {
public void test() {
System.out.println("test");
}
}
测试Main类:
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class TestMain {
public static void main(String[] args) {
// @Configuration注解spring容器加载
ApplicationContext context = new AnnotationConfigApplicationContext(PeopleConfiguration.class);
//拿出bean
People people = (People) context.getBean("people");
people.sayHello();
}
}
和上面一样的例子,这次我们通过@Component申明注册Eyes
import org.springframework.stereotype.Component;
@Component
public class Eyes {
public void test() {
System.out.println("test");
}
}
Eyes注册到容器中我们在注入People类中的属性并且注册People
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class People {
//注入
@Autowired
private Eyes eyes;
public void sayHello() {
System.out.println("oh, hey man");
}
public void setEyes(Eyes eyes) {
this.eyes = eyes;
}
public Eyes getEyes() {
return eyes;
}
public void init() {
System.out.print("初始化");
}
public void destroy() {
System.out.print("销毁");
}
}
配置类扫描包
import org.springframework.context.annotation.*;
@Configuration
//添加自动扫描注解,basePackages为People包路径
@ComponentScan(basePackages = "com.xxx.xxx")
public class PeopleConfiguration {
PeopleConfiguration(){
System.out.println("初始化");
}
}
main函数测试:
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class TestMain {
public static void main(String[] args) {
//@Configuration注解spring容器加载
ApplicationContext context = new AnnotationConfigApplicationContext(PeopleConfiguration.class);
//拿出bean
People people = (People) context.getBean("people");
System.out.println(people);
people.sayHello();
}
}
结果:
例:
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;
import com.xxx.xxx.MyConfiguration;
@Configuration
@ImportResource("classpath:applicationContext-configuration.xml")
//导入配置类,可获取MyConfiguration中的Bean
@Import(MyConfiguration.class)
public class Config {
}
例如:
import org.springframework.context.annotation.*;
@Configuration
//添加自动扫描注解,basePackages为TestBean包路径
@ComponentScan(basePackages = "com.xxx.xxx")
public class PeopleConfiguration {
PeopleConfiguration(){
}
//嵌套(静态类)
@Configuration
static class TestConfig {
@Bean
Eyes eyes() {
return new Eyes();
}
}
}
上面介绍过了@Bean配合@Configuration可以代替xml的配置,方法中可以实现Bean之间的注入,如上方例子中的eyes注入到people中。
@Bean的相关注意事项:
@Bean支持两种属性,initMethod和destroyMethod,用于在实例化bean或者销毁bean的时候,调用指定的方法。
如上面的例子中的PeopleConfiguration类中的@Bean修改如下:
@Configuration
public class PeopleConfiguration {
@Bean(initMethod = "init", destroyMethod = "destroy")
public People people() {
People people = new People();
//注入
people.setEyes(eyes());
//注册bean
return people;
}
@Bean
public Eyes eyes() {
return new Eyes();
}
}
我们重新运行测试的Main方法效果如下:
在实例化people的时候调用了定义的initMethod
测试一下:
结果:
详细内容记录在我的这篇https://blog.csdn.net/qq_36652619/article/details/103214729博客中