Spring注入:设值注入、构造注入

设值注入

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
 <bean id="icupblue" class="com.practice.entity.PaperCup">
 <property name="color" value="blue"></property>
 </bean>

 <bean id="icupblack" class="com.practice.entity.PaperCup">
 <property name="color" value="black"></property>
 </bean>

 <bean id="Ipeople" class="com.practice.entity.PeopleImp">
   <property name="cup" ref="icupblue"></property>
   <property name="cup1" ref="icupblack"></property>
 </bean>


</beans>

package com.practice.entity;

public interface ICup {
 public void fillWater();
}

package com.practice.entity;

public interface Ipeople {
  public void drink();
}

package com.practice.entity;

public class PaperCup implements ICup{

    String color;
    public void setColor(String color) {
        this.color = color;
    }
    @Override
    public void fillWater() {
        System.out.println("纸杯里的酒是"+this.color+"的");

    }
}

package com.practice.entity;

public class PeopleImp implements Ipeople{

private ICup cup;
private ICup cup1;
public void setCup1(ICup cup1) {
    this.cup1 = cup1;
}

public void setCup(ICup cup) {
//可理解为this.cup=ClassPathXmlApplicationContext("applicationContext.xml").getBean("icupblue");
    this.cup = cup;
}

@Override
public void sayHello() {
    // TODO Auto-generated method stub
    System.out.println("你好!!");
}

@Override
public void drink() {
    // TODO Auto-generated method stub
    this.cup.fillWater();
    this.cup1.fillWater();
}
}

package com.practice.test;

import org.apache.catalina.core.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.practice.entity.ICup;
import com.practice.entity.Ipeople;
import com.sun.glass.ui.Application;

public class mn {
  public static void main(String[]args){
      ClassPathXmlApplicationContext aContext=new ClassPathXmlApplicationContext("applicationContext.xml");
      Ipeople ipeople=(Ipeople) aContext.getBean("Ipeople");
      ipeople.drink();    
  }
}

总结: 1. 被依赖的属性需要定义set()方法; 2. 配置文件中使用<property>标签。如果是简单属性,使用value赋值;如果是复杂属性。使用ref赋值。


构造注入

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
 <bean id="icupblue" class="com.practice.entity.PaperCup">
 <property name="color" value="blue"></property>
 </bean>

 <bean id="icupblack" class="com.practice.entity.PaperCup">
 <property name="color" value="black"></property>
 </bean>

 <bean id="Ipeople" class="com.practice.entity.PeopleImp">
 <!--   <property name="cup" ref="icupblue"></property>
   <property name="cup1" ref="icupblack"></property> -->
   <constructor-arg ref="icupblue" index="0"></constructor-arg>
   <constructor-arg ref="icupblack" index="1"></constructor-arg>
   <constructor-arg value="ye" index="2"></constructor-arg>
 </bean>


</beans>
package com.practice.entity;

public class PeopleImp implements Ipeople{

    private ICup cup;
    private ICup cup1;
    private String name;

    public PeopleImp() {

    }

    public PeopleImp(ICup cup, ICup cup1,String name) {
        super();
        this.cup = cup;
        this.cup1 = cup1;
        this.name=name;
    }

    @Override
    public void sayHello() {
        // TODO Auto-generated method stub
        System.out.println("你好!!");
    }

    @Override
    public void drink() {
        // TODO Auto-generated method stub
        this.cup.fillWater();
        this.cup1.fillWater();
        System.out.println(name+"喝茶!!!");
    }

}

总结:采用以设值注入为主、构造注入为辅的注入策略。对于依赖关系无需变化的注入,尽量次啊要构造注入;而其他的依赖关系的注入,则考虑采用设值注入。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值