1.什么是自动装配?
根据指定的装配规则(byName or byType),Spring自动匹配属性值进行注入。
2.代码演示。
xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-beans.xsd">
<!--实现自动装配使用autowire标签
byName:根据属性名称自动注入,注入的bean的地必须和类属性名一直
byType:根据属性类型自动注入。
-->
<bean name="emp" class="com.cap.autowire.Emp" autowire="byType"></bean>
<bean name="dep" class="com.cap.autowire.Dep"></bean>
</beans>