1.创建配置类,能import其他配置类,也可以是xml文件;
@Configuration
@Import({ImportConfig.class})
public class UserConfig {
@Bean
public User getUser(){
return new User();
}
}
@Configuration
public class ImportConfig {
@Bean
public ImportPojo importPojo(){
return new ImportPojo();
}
}
2.测试的实体类
public class ImportPojo {
@Value("wangwu")
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "ImportPojo{" +
"name='" + name + '\'' +
'}';
}
}
3.测试类
public class Main {
@Test
public void userConfigTest(){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(UserConfig.class);
ImportPojo importPojo = context.getBean(ImportPojo.class);
System.out.println(importPojo);
}
}