Spring 的四种方式依赖注入

一、Spring 基于构造函数的依赖注入

package com.home.model;
public class Teditor {
    private Specker specker;
    public Teditor(Specker specker){
        System.out.println("Inside Teditor constructor.");
        this.specker=specker;
    }
    public void speckeCheck(){
        specker.Speckering();
    }
}
public class Specker {
    public Specker(){
        System.out.println("Inside Specker constructor.");
    }
    public void Speckering(){
        System.out.println("Inside Speckering!");
    }

}
//以下为配置文件
<!-- 基于构造函数的依赖注入 -->
    <bean id="teditor" class="com.home.model.Teditor">
       <constructor-arg ref="specker"></constructor-arg>
    </bean>
    <bean id="specker" class="com.home.model.Specker"></bean>

//测试代码
 @RequestMapping(value="/teditor",method=RequestMethod.GET)
    public ModelAndView DITEST(){
        ModelAndView view=new ModelAndView("/login/hello");
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        Teditor teditor=(Teditor) context.getBean("teditor");
        teditor.speckeCheck();
        return view;
    }

输出结果:
Inside Specker constructor.
Inside Teditor constructor.
inside into Sparker construstor

二、Spring 基于设值函数的依赖注入

//SparkSystem类
public class SparkSystem {
    private Sparker sparker;
    public void setSparker(Sparker sparker){
        System.out.println("SparkSystem===Inside setSpark");
        this.sparker=sparker;
    }
    public Sparker getSparker(){
        System.out.println("SparkSystem==Inside get getPark");
        return sparker;
    }
    public void spark(){
        sparker.sparkering();
    }

}
//Sparker类
package com.home.model;
public class Sparker {
    public Sparker(){
        System.out.println("inside into Sparker construstor");
    }
    public void sparkering(){
        System.out.println("inside in Sparker for sparkering");
    }
}
//配置文件
 <!-- Spring 基于设值函数的依赖注入 -->
 <bean id="sparkSystem" class="com.home.model.SparkSystem">
      <property name="sparker" ref="sparker"></property>
 </bean>
 <bean id="sparker" class="com.home.model.Sparker"></bean>

三、Spring 注入内部 Beans
正如你所知道的 Java 内部类是在其他类的范围内被定义的,同理,inner beans 是在其他 bean 的范围内定义的 bean。因此在 或 元素内 元素被称为内部bean

package com.home.model;
//Computer类
public class Computer {
    private Message message;
    public Computer(){
        System.out.println("inside into Computer constructor");
    }
    public void setMessage(Message message){
        System.out.println("inside into setComputer");
        this.message=message;
    }
    public Message getMessage(){
        System.out.println("inside into getComputer");
        return message;
    }
    public void messageing(){
        message.messageing();
    }
}
//Message类
package com.home.model;
public class Message {
    public Message(){
        System.out.println("inside into Message constructor!");
    }
    public void messageing(){
        System.out.println("inside messageing....");
    }
}
//配置文件
<!-- 注入内部 Beans -->
    <bean id="computer" class="com.home.model.Computer">
        <property name="message">
            <bean id="message" class="com.home.model.Message">
            </bean>
        </property>
   </bean>

四、Spring 注入集合

  ****<list>它有助于连线,如注入一列值,允许重复。
  <set>它有助于连线一组值,但不能重复。 
  <map>它可以用来注入名称-值对的集合,其中名称和值可以是任何类型。    
  <props>它可以用来注入名称-值对的集合,其中名称和值都是字符串类型。****
package com.home.model;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class CollectionExample {
    List addressList;//它有助于连线,如注入一列值,允许重复
    Set  addressSet;//它有助于连线一组值,但不能重复
    Map  addressMap;//它可以用来注入名称-值对的集合,其中名称和值可以是任何类型
    Properties addressProp;//它可以用来注入名称-值对的集合,其中名称和值都是字符串类型
    public List getAddressList() {
        System.out.println("List:"+addressList);
        return addressList;
    }
    public void setAddressList(List addressList) {
        this.addressList = addressList;
    }
    public Set getAddressSet() {
        System.out.println("Set:"+addressSet);
        return addressSet;
    }
    public void setAddressSet(Set addressSet) {
        this.addressSet = addressSet;
    }
    public Map getAddressMap() {
        System.out.println("Map:"+addressMap);
        return addressMap;
    }
    public void setAddressMap(Map addressMap) {
        this.addressMap = addressMap;
    }
    public Properties getAddressProp() {
        return addressProp;
    }
    public void setAddressProp(Properties addressProp) {
        System.out.println("Prop:"+addressProp);
        this.addressProp = addressProp;
    }
}
//配置文件如下:
 <!-- 注入集合 -->
    <bean id="collectionExample" class="com.home.model.CollectionExample">
        <property name="addressList">
            <list>
               <value>China</value>
               <value>India</value>
               <value>India</value>
               <value>USA</value>
            </list>
        </property>
        <property name="addressSet">
            <set>
               <value>China</value>
               <value>India</value>
               <value>India</value>
               <value>USA</value>
            </set>
        </property>
        <property name="addressMap">
            <map>
                <entry key="1" value="China"></entry>
                <entry key="2" value="India"></entry>
                <entry key="3" value="India"></entry>
                <entry key="4" value="USA"></entry>
            </map>
        </property>
        <property name="addressProp">
             <props>
                 <prop key="one">China</prop> 
                 <prop key="two">India</prop> 
                 <prop key="three">India</prop> 
                 <prop key="four">USA</prop> 
             </props>
        </property>
    </bean>
//测试类
  @RequestMapping(value="/collection",method=RequestMethod.GET)
    public ModelAndView collection(){
        ModelAndView view=new ModelAndView("/login/hello");
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        CollectionExample collectionExample=(CollectionExample)context.getBean("collectionExample");
        collectionExample.getAddressList();
        collectionExample.getAddressSet();
        collectionExample.getAddressMap();
        collectionExample.getAddressProp();
        return view;
    }
打印结果如下:
List:[China, India, India, USA]
Set:[China, India, USA]
Map:{1=China, 2=India, 3=India, 4=USA}
Prop:{two=India, one=China, three=India, four=USA}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

为你写诗_xue

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值