要点
@Autowired
private LSub mSub;
<bean id="l" class="dd.L"></bean>
<bean id="lsub" class="dd.LSub"></bean>
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
package dd;
import org.springframework.beans.factory.annotation.Autowired;
public class L {
@Autowired
private LSub mSub;
public void print() {
mSub.print();
}
}
package dd;
public class LSub {
public void print() {
System.out.println("LSub print");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="l" class="dd.L"></bean>
<bean id="lsub" class="dd.LSub"></bean>
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
</beans>
package dd;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class T {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("bean.xml");
//Spring @Autowired 注释-属性中的 @Autowired
System.out.println("属性中的 @Autowired");
L l = (L)context.getBean("l");
l.print();
}
}