Bean的定义:
//把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/>
@Component
public class MyBean1 {
//@Value注解,实例化MyBean1时,使用该注解设置默认值
@Value("#{systemProperties['user.language']}")
private String message;
配置类:
@Configuration
@ComponentScan(basePackages = {"com.beginning.ch9.java"})
public class ApplicationConfig {
// @Bean
// public MyBean1 getBean(){
// MyBean1 myBean = new MyBean1();
// return myBean;
// }
}
执行main方法:
public class Main {
public static void main(String[] args){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);
MyBean1 myBean = context.getBean(MyBean1.class);
System.out.println(myBean.getMessage());
}
}
zh