依赖注入(控制反转)方式:设值, 构造 ex(setter,constructor);
public class HelloBean{
private String beanName;
public HelloBean(String beanName){
System.out.println("通过构造函数进行注入");
this.beanName=beanName;
}
public void setBeanName(String beanName){
System.out.println("通过设值进行注入");
this.beanName=beanName;
}
public void printBeanName(){
System.out.println("beanName is:"+beanName);
}
Attention(注意)-->设值注入时参数(beanName)要和applicationContext.xml中property name="beanName"一致,不然注入会报错!!!
applicationContext.xml-->
<bean id="helloBean" class="com.xx.HelloBean">
<property name="xx" value="xx"/>
SetterIoc--><property name="wwService" ref=''wwService"/>
</bean>
TestDemo---.
@test
public void testDI(){
ApplicationContext appContext=new ClassPathXmlApplicationContext("applicationContext.xml");
HelloBean helloBean=(HelloBean)appContext.getBean("helloBean");
helloBean.printBeanName();
}
Result--->print:beanName is:xx;
三种注入方式-->属性设值注入(Setter)常用;
spring核心思想是容器负责管理所需对象的创建(IOC)