0%

spring根据配置文件获取bean

文章字数:53,阅读全文大约需要1分钟

@Configuration注解的类就是配置文件,@Bean注解的方法就会注入名为方法名的实例

获取方法之一

1
2
3
4
5
6
7
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

// 获取配置文件
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);
// 获取 bean文件
Person bean = context.getBean(Person.class);
System.out.println(bean);