【spring-bean】浅谈Spring的依赖注入(基础)

    spring作为一种轻量级企业框架,其易用性和简化java开发的宗旨得以充分体现。在spring中,对象及其所依赖的对象(bean)创建都由Spring IOC容器进行管理(创建,销毁)。spring是一个基于容器的框架,本文就spring容器的依赖注入进行详解。

一丶属性注入。

      属性注入是通过POJO的setXxx()方法注入Bean的属性值或所依赖对象,由于属性注入具有可选择性和灵活性高的特点,因此企业级J2EE应用通常都采用此方式.属性注入需要Bean提供默认的构造函数并为其对应属性提供set方法.原理是spring IOC容器利用JNDI技术获得bean的class属性,通过反射来调用bean的默认构造函数创建实例对象,继续利用反射机制调用set方法为其注入属性.

public class Cat implements Animal
{
    
    private String name;
    
    private String sound;
    
    @Override
    public void eat()
    {
        System.out.println(this.name + "【猫】正在吃。。。(⊙o⊙)…。。");
    }
    
    @Override
    public void speed()
    {
        System.out.println(this.name + "【猫】正在发出" + sound + "的声音!");
    }
    
    /**
     * @return  返回 name
     */
    public String getName()
    {
        return name;
    }
    
    /**
     * @param 对 name 进行赋值
     */
    public void setName(String name)
    {
        this.name = name;
    }
    
    /**
     * @return  返回 sound
     */
    public String getSound()
    {
        return sound;
    }
    
    /**
     * @param 对 sound 进行赋值
     */
    public void setSound(String sound)
    {
        this.sound = sound;
    }
    
}
父类Animal
public interface Animal
{
    
    void eat();
    
    void speed();
}

对应的spring配置文件applicationContext.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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	">
	<bean id="cat" class="com.test.dao.impl.Cat">
		<property name="name" value="汪星人"></property>
		<property name="sound" value="喵喵的"></property>
	</bean>
<strong></beans></strong>

定义一个bean对应cat类,其中一个属性为name="汪星人" ,另一个sound="喵喵的".

其中spring-beans包中有一个BeanDefinitionParser解析接口,用来解析bean的。然后利用反射创建bean。

其中<property>还有一种配置方式,首先应用p命名空间 引入xmlns:p="http://www.springframework.org/schema/p"   如下:

<?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:context="http://www.springframework.org/schema/context"
	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.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	">
	<bean id="cat" class="com.test.dao.impl.Cat" p:name="汪星人" p:sound="喵喵的">
	</bean>
</beans>
junit测试类如下

public class AnimalTest
{
    @Test
    public void eat()
    {
        @SuppressWarnings("resource")
        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        Animal a = (Animal)ac.getBean("cat");
        a.eat();
        a.speed();
    }
}
其中ClassPathXmlApplicationContext是读取classpath路径下的ApplicationContext文件.

显示结果如下:

2015-2-16 9:30:08 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7054c4ad: startup date [Mon Feb 16 09:30:07 CST 2015]; root of context hierarchy
2015-2-16 9:30:08 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
汪星人【猫】正在吃。。。(⊙o⊙)…。。
汪星人【猫】正在发出喵喵的的声音!

二丶构造函数注入

   构造函数注入是除属性注入之外的另一种常用的注入方式,它保证了一些必要的属性在Bean实例化时就得到设置,并且确保了Bean实例化后可以使用.

   1> 按类型匹配入参

   使用构造注入的前提是Bean必须提供相应需要注入属性参数的构造函数,如下:

public class Dog implements Animal
{
    private String name;
    
    private String sound;
    
    Dog(String name, String sound)
    {
        this.name = name;
        this.sound = sound;
    }
    
    @Override
    public void eat()
    {
        System.out.println(this.name + "【狗】正在吃。。。(⊙o⊙)…。。");
    }
    
    @Override
    public void speed()
    {
        System.out.println(this.name + "【狗】正在发出" + sound + "的声音!");
    }
    
    /**
     * @return  返回 name
     */
    public String getName()
    {
        return name;
    }
    
    /**
     * @param 对 name 进行赋值
     */
    public void setName(String name)
    {
        this.name = name;
    }
    
    /**
     * @return  返回 sound
     */
    public String getSound()
    {
        return sound;
    }
    
    /**
     * @param 对 sound 进行赋值
     */
    public void setSound(String sound)
    {
        this.sound = sound;
    }
    
}
其中属性注入和构造注入的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:context="http://www.springframework.org/schema/context"
	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.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	">
	<bean id="dog" class="com.test.dao.impl.Dog">
		<constructor-arg type="java.lang.String" value="小泰迪" />
		<constructor-arg type="java.lang.String" value="汪汪的" />
	</bean>
</beans>


其中的type="java.lang.String" 代表注入的属性在java中是String类型的.构造注入用的是 <constructor>标签,还可以用 index来代表第几个参数进行构造注入.

<bean id="dog" class="com.test.dao.impl.Dog">
		<constructor-arg index="0" value="小泰迪" />
		<constructor-arg index="1" value="汪汪的" />
	</bean>

用junit测试如下.

2015-2-16 9:45:20 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7054c4ad: startup date [Mon Feb 16 09:45:20 CST 2015]; root of context hierarchy
2015-2-16 9:45:20 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
小泰迪【狗】正在吃。。。(⊙o⊙)…。。
小泰迪【狗】正在发出汪汪的的声音!


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

笑起来贼好看

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

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

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

打赏作者

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

抵扣说明:

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

余额充值