package com.astute.sparrow.spring.ioc.method_injection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component("a")
public class A {
@Autowired
@Qualifier("b")
private B b;
public void printB() {
System.out.println(getB());
}
public B getB() {
return b;
}
public void setB(B b) {
this.b = b;
}
}
package com.astute.sparrow.spring.ioc.method_injection;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component("b")
@Scope("prototype")
public class B {
}
<bean id="a" class="com.astute.sparrow.spring.ioc.method_injection.A"> <lookup-method name="getB" bean="b"/> </bean> <bean id="b" class="com.astute.sparrow.spring.ioc.method_injection.B" scope="prototype"/>
BeanFactory factory = new XmlBeanFactory(new ClassPathResource(
"com/astute/sparrow/spring/ioc/method_injection/spring-method-injection.xml"));
A a = (A) factory.getBean("a");
a.printB();
a.printB();
输出:
com.astute.sparrow.spring.ioc.method_injection.B@d08633
com.astute.sparrow.spring.ioc.method_injection.B@1a9334