JAVA的Spring依赖注入

Spring依赖注入方式主要有两种,即设值注入和构造函数注入。设值注入是通过类中某个属性的Setter、Getter方法完成,构造函数注入是通过创建对象时调用相关的构造函数完成。本文主要阐述一下,两种注入方式的实现方法以及他们对应的另一种简洁方法。
其中配置内容(容器)是放在类路径中文件命名为bean.xml
1、设值注入
这种方式的bean中对应JAVA代码如下:

package com.ev.bean;
public class Bean {
    private String name;
    private String email;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }

}

该类中有两个成员变量,同时分别设置这两个变量的Setter、Getter方法,以便容器中设置变量的值,以及从外部获取变量值。
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"
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="obj" class="com.ev.bean.Bean" >
     <property name="name" value="AAA"/>
     <property name="email" value="[email protected]"/>
</beans>

property 元素的name属性的值与分别与Bean类中的两个成员变量一致,该bean的id设为“obj”。这可在ApplicationContext启动时,实现依赖注入。测试类代码为:

package com.ev.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ev.bean.Bean;

public class Test {
    public static void main(String[] args){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        Bean bean = (Bean)context.getBean("obj", Bean.class);
        System.out.println("name:"+bean.getName()+" email:"+bean.getEmail());
    }
}

其中ClassPathXmlApplicationContext方法用于加载容器的资源,getBean方法从资源中获取实例化的对象,本例对象类型为:Bean,代码运行后结果如下:
这里写图片描述
我们可以发现,获取到的两个变量的值其实就是在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"
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="obj" class="com.ev.bean.Bean" p:name="AAA" p:email="[email protected]"/>
</beans>

beans元素多加xmlns:p="http://www.springframework.org/schema/p"
这就是所谓的p-namespace
这个就是设值注入的方式实现。
2、构造函数注入
该注入方式和前面的设值注入的思想是一样的。配置文件所用到的元素不一样。
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"
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="obj" class="com.ev.bean.Bean" >
     <constructor-arg name="name" value="AAA"/>
     <constructor-arg name="email" value="[email protected]"/>
</beans>

对应的JAVA代码:

package com.ev.bean;

public class Bean {
    private String name;
    private String email;

    public Bean(String name,String email){
        this.name = name;
        this.email = email;
    }

    public String getName() {
        return name;
    }

    public String getEmail() {
        return email;
    }

}

在这里只需一个构造方法设置值即可,得到的结果和之前的一样。
简洁方式:

<?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="obj" class="com.ev.bean.Bean" c:name="AAA" c:email="[email protected]"/>
</beans>

也一样要在beans元素中加

xmlns:c="http://www.springframework.org/schema/c"

运行效果与之前的一样。
3、对容器集合的依赖注入
这种情况表现为,类中的成员变量为,List、Set、Map、Properties类型。
其实也一样,只需修改配置文件即可
对于List集合(以设值注入为例),对该类型变量的设值如下:

<property name="list">
    <list>
        <value>a</value>
        <value>b</value>
        <value>c</value>
    </list>
</property>

Map类型变量为:

<property name="map">
    <map>
        <entry key="one" value="1.1"/>
        <entry key="two" value="2.1"/>
    </map>
</property>

Set类型变量为:

<property name="set">
    <value>aaa</value>  
</property>

Properties类型变量为:

<property name="prop">
    <props>
        <prop key="aaa">111</prop>
        <prop key="bbb">222</prop>
        <prop key="ccc">333</prop>      
    </props>
</property>

最后如果一个bean要引用另一个bean,则可以在bean元素中用ref属性或在properties元素或constructor-arg元素中加<ref bean=""/>元素来引用。

猜你喜欢

转载自blog.csdn.net/yangkaige111/article/details/79940251