spring 配置依赖

一、spring注入方式及配置

spring根据注入方式不同可以分为两种形式:

属性:通过<property>元素设值注入;

构造器参数:通过<constructor-arg>构造注入。

由于java 实例的属性值可以是各种数据类型,除了基本类型,还可以是其它java实例,也可以是java集合、数组等,所以spring允许通过如下两种元数为bean实例的属性指定值:

value

ref

bean

list set map props


下面详细介绍这4种情况:

package test.demo;

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

    public ExampleBean() {

    }

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

    /**
     * 获取name
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置name
     * @param name name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取age
     * @return age
     */
    public int getAge() {
        return age;
    }

    /**
     * 设置age
     * @param age age
     */
    public void setAge(int age) {
        this.age = age;
    }
}


1、设置普通属性值

设值注入:


<span style="white-space:pre">	</span><pre name="code" class="html"><pre name="code" class="html"><bean id="exampleBean" class="test.demo.ExampleBean" init-method="init">
        	<property name="name" value="xxx"></property>
        	<property name="age" value="11"></property>
    </bean>

 

 

构造注入:

<bean id="exampleBean" class="test.demo.ExampleBean" init-method="init">
		<constructor-arg index="0" value="bbb"></constructor-arg>
		<constructor-arg index="1" value="12"></constructor-arg>
	</bean>

2、注入合作者bean

如果需要配置合作者Bean的话,则应该使用<ref>元素。


<bean id="exampleBean" class="test.demo.ExampleBean" init-method="init">
		<property name="dog" ref="dog"></property>
	</bean>
	<bean id ="dog" class="test.demo.Dog"></bean>

3、注入集合值

package test.demo;

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

public class Chinese {
    private List<String>     schools;
    private Map              scores;
    private Map<String, Axe> phaseAxes;
    private Properties       health;
    private Set              axes;
    private String[]         books;

    /**
     * 获取schools
     * @return schools
     */
    public List<String> getSchools() {
        return schools;
    }

    /**
     * 设置schools
     * @param schools schools
     */
    public void setSchools(List<String> schools) {
        this.schools = schools;
    }

    /**
     * 获取scores
     * @return scores
     */
    public Map getScores() {
        return scores;
    }

    /**
     * 设置scores
     * @param scores scores
     */
    public void setScores(Map scores) {
        this.scores = scores;
    }

    /**
     * 获取phaseAxes
     * @return phaseAxes
     */
    public Map<String, Axe> getPhaseAxes() {
        return phaseAxes;
    }

    /**
     * 设置phaseAxes
     * @param phaseAxes phaseAxes
     */
    public void setPhaseAxes(Map<String, Axe> phaseAxes) {
        this.phaseAxes = phaseAxes;
    }

    /**
     * 获取health
     * @return health
     */
    public Properties getHealth() {
        return health;
    }

    /**
     * 设置health
     * @param health health
     */
    public void setHealth(Properties health) {
        this.health = health;
    }

    /**
     * 获取axes
     * @return axes
     */
    public Set getAxes() {
        return axes;
    }

    /**
     * 设置axes
     * @param axes axes
     */
    public void setAxes(Set axes) {
        this.axes = axes;
    }

    /**
     * 获取books
     * @return books
     */
    public String[] getBooks() {
        return books;
    }

    /**
     * 设置books
     * @param books books
     */
    public void setBooks(String[] books) {
        this.books = books;
    }
public void test(){
    System.out.println(schools);
    System.out.println(scores);
    System.out.println(phaseAxes);
    System.out.println(health);
    System.out.println(axes);
    System.out.println(java.util.Arrays.toString(books));
    
}
}

package test.demo;

public interface Axe {
    void chop();
}

package test.demo;

public class SteelAxe implements Axe {

    @Override
    public void chop() {
        System.out.println("钢斧砍柴真快!");

    }

}

package test.demo;

public class StoneAxe implements Axe {

    @Override
    public void chop() {
        System.out.println("石斧砍柴真慢!");
    }

}
<bean id="steelAxe" class="test.demo.SteelAxe"></bean>
	<bean id="stoneAxe" class="test.demo.StoneAxe"></bean>
	<bean id="chinese" class="test.demo.Chinese" init-method="test">
		<property name="schools">
			<list>
				<value>小学</value>
				<value>中学</value>
				<value>大学</value>
			</list>
		</property>
		<property name="scores">
			<map>
				<entry key="语文" value="99"></entry>
				<entry key="数学" value="100"></entry>
				<entry key="英语" value="44"></entry>
			</map>
		</property>
		<property name="phaseAxes">
			<map>
				<entry key="石头的" value-ref="stoneAxe"></entry>
				<entry key="钢铁的" value-ref="steelAxe"></entry>
			</map>
		</property>
		<property name="health">
			<props>
				<prop key="血压">正常</prop>
				<prop key="身高">172</prop>
			</props>
		</property>
		<property name="axes">
			<set>
				<value>字符串</value>
				<bean class="test.demo.SteelAxe"></bean>
			</set>
		</property>
		<property name="books">
			<list>
				<value>疯狂java讲义</value>
				<value>大型网站技术架构</value>
				<value>java算法</value>
			</list>
		</property>
	</bean>

输出结果:

[小学, 中学, 大学]
{语文=99, 数学=100, 英语=44}
{石头的=test.demo.StoneAxe@1e12f6d, 钢铁的=test.demo.SteelAxe@f0b4a3}
{血压=正常, 身高=172}
[字符串, test.demo.SteelAxe@1f6f27b]
[疯狂java讲义, 大型网站技术架构, java算法]

二、使用自动装配注入合作者bean

spring的自动装配可以通过<beans>元素的default-autowire属性指定,也可以通过<bean>元素的autowire属性指定。autowire属性可以接受如下值:

no:不使用自动装配。bean依赖必须通过ref元素定义。

byName:根据属性名自动装配。BeanFactory查找容器中的全部bean,找出其中id属性与属性同名的bean来完成注入。如果没有找到匹配的bean实例,则spring不会进行任何注入。

byType:根据属性类型自动装配。BeanFactory查找容器中的全部bean,如果正好有一个与依赖属性类型相同的bean,就注入这个属性;如果有多个这样的bean则抛出一个异常;如果没有匹配的bean,则属性不会被注入。

constructor:与byType类似,区别是用于构造注入的参数。如果BeanFactory中不是恰好有一个Bean与构造器参数类型相同,则会抛出一个异常。

autodetect:BeanFactory根据Bean内部结构,决定使用constructor或byType。如果找到一个默认的构造函数,那么就会应用byType.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值