有关报错AnnotationConfigApplicationContext has not been refreshed yet - what’s wrong?
//User.java
@Component//相当于配置文件中的<bean id="" class=""/>
public class User {
@Value("Kidd")
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return "name = " + this.name;
}
}
//Test.java
//解决方法
/*法一:显式注册和刷新
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(User.class);
context.refresh();
*/
//法二:隐式注册和刷新
ApplicationContext context = new AnnotationConfigApplicationContext(User.class);
User user = context.getBean("user", User.class);
System.out.println(user);