spring configuration dependencies

1. Spring injection method and configuration

Spring can be divided into two forms according to different injection methods:

Properties: value injection through <property> element;

Constructor parameters: constructed via <constructor-arg> injection.

Since the attribute values ​​of java instances can be various data types, in addition to basic types, they can also be other java instances, java collections, arrays, etc., so spring allows specifying values ​​for the attributes of bean instances through the following two elements:

value

ref

bean

list set map props


These 4 situations are introduced in detail below:

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. Set common attribute values

Set value injection:


<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>

 
 
 
 

Construct injection:

<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. Inject the partner bean

If you need to configure a collaborator bean, you should use the <ref> element.


<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. Inject the collection value

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>

Output result:

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

2. Use automatic assembly to inject partner beans

Spring's autowiring can be specified through the default-autowire attribute of the <beans> element, or through the autowire attribute of the <bean> element. The autowire attribute can accept the following values:

no: Do ​​not use automatic assembly. Bean dependencies must be defined through ref elements.

byName: Automatic assembly based on attribute name. BeanFactory searches all beans in the container and finds the bean whose id attribute has the same name as the attribute to complete the injection. If no matching bean instance is found, spring will not do any injection.

byType: Automatic assembly based on attribute type. BeanFactory searches all beans in the container. If there is exactly one bean of the same type as the dependent property, it injects this property; if there are multiple such beans, an exception is thrown; if there is no matching bean, the property will not be injected. .

constructor: Similar to byType, the difference is the parameters used to construct the injection. If there is not exactly one Bean in the BeanFactory of the same type as the constructor parameter, an exception will be thrown.

autodetect: BeanFactory decides to use constructor or byType based on the internal structure of the Bean. If a default constructor is found, then byType is applied.

Guess you like

Origin blog.csdn.net/suyuaidan/article/details/44903349