public class Main {
public static void main(String[] args) {
/* ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");*/
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext("spring.xml");
System.out.println(applicationContext.getBean("user1"));
}
}
发现报错:
config.class
public class config {
@Bean
public User user(){
return new User();
}
}
public class Main {
public static void main(String[] args) {
/* ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");*/
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(config.class);
System.out.println(applicationContext.getBean("user"));
}
}
还有一种写法(也是基于注解)
public class Main {
public static void main(String[] args) {
/* ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");*/
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(config.class);
System.out.println(applicationContext.getBean("user"));
}
}
@ComponentScan("com.luban")
public class config {
}
@Component
public class User {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
输出:
总结:
也就是说 AnnotationConfigApplicationContext不支持xml的形式,只支持注解的形式