Joohong的专栏

每天一小步、三天一大步

Spring自动装配Bean

1、自动装配

        Spring IoC容器可以自动装配bean。只需要使用autowire属性指定装配的方式。

    byName:根据bean的名字和当前bean的setter风格的属性名进行自动装配,若匹配,则进行装配,反之,不装配,结果为null。即要保证bean名称和属性名称完全相同。

    byType:根据bean的类型和当前的属性的类型进行自动装配,若IOC容器中有两个及两个以上的Java类类型匹配的bean,就会抛出异常。即要保证在同一IOC容器中类类型的唯一性。

2、示例分析

<?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="spring.beans.autowire.User"
    	p:name="小明" autowire="byName"></bean>
    
    <bean id="educationInformation" class="spring.beans.autowire.EducationInformation"
    	p:schoolName="清华大学" p:schoolAddr="中国.北京"></bean>
</beans>
package spring.beans.autowire;

public class User {

	private String name;
	
	private Car car;
	
	private EducationInformation educationInformation;

	public String getName() {
		return name;
	}

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

	public EducationInformation getEducationInformation() {
		return educationInformation;
	}

	public void setEducationInformation(EducationInformation educationInformation) {
		this.educationInformation = educationInformation;
	}
	
	@Override
	public String toString() {
		return "User [name=" + name + ", car=" + car + ", eduInformation="
				+ educationInformation + "]";
	}
}
package spring.beans.autowire;

public class EducationInformation {

	private String schoolName;
	
	private String schoolAddr;

	public String getSchoolName() {
		return schoolName;
	}

	public void setSchoolName(String schoolName) {
		this.schoolName = schoolName;
	}

	public String getSchoolAddr() {
		return schoolAddr;
	}

	public void setSchoolAddr(String schoolAddr) {
		this.schoolAddr = schoolAddr;
	}

	@Override
	public String toString() {
		return "EducationInformation [schoolName=" + schoolName
				+ ", schoolAddr=" + schoolAddr + "]";
	} 
}
package spring.beans.autowire;

public class Car {

}

package spring.beans;

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

public class ContextUtils {
	
	private static ApplicationContext ctx = null;
	
	public static ApplicationContext getContext() {
		if (ctx == null) {
			ctx = new ClassPathXmlApplicationContext("spring.autowire.xml");
		}
		return ctx;
	}
}
package spring.beans.autowire;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import spring.beans.ContextUtils;

public class AutowireTest {
	@Test
	public void testAutowire() {
		ApplicationContext ctx = ContextUtils.getContext();
		User user = (User) ctx.getBean("user");
		System.out.println("user>>>>" + user);
	}

}

执行结果:

user>>>>User [name=小明, car=null, eduInformation=EducationInformation [schoolName=清华大学, schoolAddr=中国.北京]]

当.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="user" class="spring.beans.autowire.User"
    	p:name="小明" autowire="byName"></bean>
    
    <bean id="educationInformation1" class="spring.beans.autowire.EducationInformation"
    	p:schoolName="北京大学" p:schoolAddr="中国.北京"></bean>
    
    <bean id="educationInformation" class="spring.beans.autowire.EducationInformation"
    	p:schoolName="清华大学" p:schoolAddr="中国.北京"></bean>
</beans>

结果为:

user>>>>User [name=小明, car=null, eduInformation=EducationInformation [schoolName=北京大学, schoolAddr=中国.北京]]

原因:要保证bean名称和属性名称完全相同

spring面试总结https://www.cnblogs.com/wang-meng/p/5701982.html


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jingzi123456789/article/details/79953377
个人分类: Spring4.X
上一篇Java并发:线程池技术
下一篇JAVA安全:3DES
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭