Spring依赖注入的实现方式


依赖注入时,最常用的是setter注入和构造函数注入。
本篇介绍了setter注入时,如何注入常量、对象、数组、List、Map、Set、Null和Properties,以及p命名空间注入;也介绍了构造函数注入和c命名空间注入。

setter注入

在这里插入图片描述

  • com.jepcc.demo.model.FullName
package com.jepcc.demo.model;

public class FullName {
    private String firstName;
    private String lastName;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}
  • com.jepcc.demo.model.Student
package com.jepcc.demo.model;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class Student {
    private String id;
    private FullName fullName;
    private String[] skills;
    private List<String> hobbies;
    private Map<String,Integer> scores;
    private Set<String> artists;
    private String company;
    private Properties info;


    public void setId(String id) {
        this.id = id;
    }

    public void setFullName(FullName fullName) {
        this.fullName = fullName;
    }

    public void setSkills(String[] skills) {
        this.skills = skills;
    }

    public void setHobbies(List<String> hobbies) {
        this.hobbies = hobbies;
    }

    public void setScores(Map<String, Integer> scores) {
        this.scores = scores;
    }

    public void setArtists(Set<String> artists) {
        this.artists = artists;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    public void setInfo(Properties info) {
        this.info = info;
    }

    public void show(){
        System.out.print("id="+id+
                ",fullName="+fullName.getFirstName()+" "+fullName.getLastName()+"" +
                ",skills=");
        for(String skill: skills){
            System.out.print(skill + " ");
        }
        System.out.println();
        System.out.println("hobbies="+hobbies);
        System.out.println("scores="+scores);
        System.out.println("artists="+artists);
        System.out.println("company="+company);
        System.out.println("info="+info);
    }
}
  • com.jepcc.demo.test.Test
package com.jepcc.demo.test;

import com.jepcc.demo.model.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
        Student stu = (Student) context.getBean("student");
        stu.show();
    }
}
  • config.xml
<?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.xsd">
    <bean id="fullname" class="com.jepcc.demo.model.FullName">
        <property name="firstName" value="Steven"></property>
        <property name="lastName" value="Jobs"></property>
    </bean>
    <bean id="student"  class="com.jepcc.demo.model.Student">
        <property name="id" value="374887"></property>
        <property name="fullName" ref="fullname"></property>
        <property name="skills">
            <array>
                <value>HTML</value>
                <value>CSS</value>
                <value>JavaScript</value>
                <value>Java</value>
            </array>
        </property>
        <property name="hobbies">
            <list>
                <value>羽毛球</value>
                <value>游泳</value>
                <value>跑步</value>
            </list>
        </property>
        <property name="scores">
            <map>
                <entry key="数据结构与算法" value="90"></entry>
                <entry key="数据库原理" value="88"></entry>
                <entry>
                    <key><value>操作系统</value></key>
                    <value>95</value>
                </entry>
                <entry>
                    <key><value>编译原理</value></key>
                    <value>85</value>
                </entry>
            </map>
        </property>
        <property name="artists">
            <set>
                <value>周杰伦</value>
                <value>陈小春</value>
                <value>胡歌</value>
            </set>
        </property>
        <property name="company"><null></null></property>
        <property name="info">
            <props>
                <prop key="address">深圳市</prop>
                <prop key="email">test@exam.com</prop>
            </props>
        </property>
    </bean>
</beans>

运行Test.main(),控制台打印如下信息:
在这里插入图片描述

常量注入
<bean id="student"  class="com.jepcc.demo.model.Student">
    <property name="id" value="374887"></property>
</bean>
对象注入
<bean id="fullname" class="com.jepcc.demo.model.FullName">
    <property name="firstName" value="Steven"></property>
    <property name="lastName" value="Jobs"></property>
</bean>
<bean id="student"  class="com.jepcc.demo.model.Student">
    <property name="fullName" ref="fullname"></property>
</bean>
数组注入
<bean id="student"  class="com.jepcc.demo.model.Student">
    <property name="skills">
        <array>
            <value>HTML</value>
            <value>CSS</value>
            <value>JavaScript</value>
            <value>Java</value>
        </array>
    </property>
</bean>
List注入
<bean id="student"  class="com.jepcc.demo.model.Student">
   <property name="hobbies">
       <list>
           <value>羽毛球</value>
           <value>游泳</value>
           <value>跑步</value>
       </list>
   </property>
</bean>
Map注入
<bean id="student"  class="com.jepcc.demo.model.Student">
    <property name="scores">
        <map>
            <entry key="数据结构与算法" value="90"></entry>
            <entry key="数据库原理" value="88"></entry>
            <entry>
                <key><value>操作系统</value></key>
                <value>95</value>
            </entry>
            <entry>
                <key><value>编译原理</value></key>
                <value>85</value>
            </entry>
        </map>
    </property>
</bean>
Set注入
<bean id="student"  class="com.jepcc.demo.model.Student">
    <property name="artists">
        <set>
            <value>周杰伦</value>
            <value>陈小春</value>
            <value>胡歌</value>
        </set>
    </property>
</bean>
Null注入
<bean id="student"  class="com.jepcc.demo.model.Student">
    <property name="company"><null></null></property>
</bean>
Properties注入
<bean id="student"  class="com.jepcc.demo.model.Student">
    <property name="info">
        <props>
            <prop key="address">深圳市</prop>
            <prop key="email">test@exam.com</prop>
        </props>
    </property>
</bean>
p命名空间注入

在这里插入图片描述

  • 新建类User,com.jepcc.demo.model.User
package com.jepcc.demo.model;

public class User {
    private String name;
    private int age;

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void show(){
        System.out.println("name="+name+",age="+age);
    }
}
  • 修改测试类Test,com.jepcc.demo.test.Test
package com.jepcc.demo.test;

import com.jepcc.demo.model.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
        User user = (User) context.getBean("user");
        user.show();
    }
}
  • config.xml
    注意,头文件中新增了xmlns:p="http://www.springframework.org/schema/p",且属性是通过p:namep:age设置的。
<?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.xsd">
    <bean id="user" class="com.jepcc.demo.model.User" p:name="李小明" p:age="20">
    </bean>
</beans>

运行Test.main(),控制台打印出如下信息:
在这里插入图片描述
在这里插入图片描述

构造函数注入
  • com.jepcc.demo.model.User
package com.jepcc.demo.model;

public class User {
    private String name;
    private int age;

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void show(){
        System.out.println("name="+name+",age="+age);
    }
}
  • com.jepcc.demo.test.Test
package com.jepcc.demo.test;

import com.jepcc.demo.model.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
        User user = (User) context.getBean("user");
        user.show();
    }
}
  • config.xml
<?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.xsd">
    <bean id="user" class="com.jepcc.demo.model.User">
        <constructor-arg name="name" value="李小明"></constructor-arg>
        <constructor-arg name="age" value="20"></constructor-arg>
    </bean>
</beans>

在这里插入图片描述

c命名空间注入

在这里插入图片描述
对config.xml稍作修改,注意,头文件增加xmlns:c="http://www.springframework.org/schema/c",且属性是通过c:namec:age设置的。

<?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:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="user" class="com.jepcc.demo.model.User" c:name="Nicholas" c:age="18">
    </bean>
</beans>

在这里插入图片描述

bean的作用域

在这里插入图片描述
其中, singleton,单例,即整个容器中只有一个对象实例,scope的默认值是singletonprototype,原型,每次获取bean时都会产生一个新的对象实例。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值