8.0w+
社区成员
//Application.java
public class Application {
public static void main(String[] args){
args = new String[] {
"classpath:spring/applicationContext.xml"
};
ApplicationContext actx = new ClassPathXmlApplicationContext(args);
Entry entry = (Entry) actx.getBean("Entry");
entry.main(actx);
}
}
//Entry.java
@Component("Entry")
public class Entry {
@Autowired
private SpringTest springTest;
public void main(ApplicationContext ac){
System.out.println(springTest);
}
}
//SpringTest.java
@Component
public class SpringTest {
private String test;
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}
}
public class Application {
public static void main(String[] args){
args = new String[] {
"classpath:spring/applicationContext.xml"
};
ApplicationContext actx = new ClassPathXmlApplicationContext(args);
//Entry entry = (Entry) actx.getBean("Entry");
Entry entry = new Entry();
entry.main(actx);
}
}
//HandlerMethod.class 130行
public HandlerMethod createWithResolvedBean() {
Object handler = this.bean;
if (this.bean instanceof String) {
String beanName = (String)this.bean;
handler = this.beanFactory.getBean(beanName);
}
return new HandlerMethod(this, handler);
}
也就是说先getBean实例化Controller,然后Controller中用@Autowire标记的bean才能自动装配,这和我写的第一种情况是一样的,即我getBean实例化Entry,然后Entry中的SpringTest才能自动装配