bean的自动装配 Autowried
bean的自动装配
- 自动是Spring满足bean依赖的一种方式
- Spring会在上下文中自动寻找,并自动给bean装配属性
在spring中有3中装配方式
- 在XML中显式的配置
- 在java中显式配置
- 在xml中隐式自动装配
在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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
      https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="cat" class="com.liu.pojo.Cat"></bean>
    <bean id="dog" class="com.liu.pojo.Dog"></bean>
<!--
buName  会自动在容器上下文查找,和自己对象set方法后面的值对应的beanID!
byType  会自动在容器上下文查找,和自己对象属性类型相同的bean!
-->
    <bean id="people" class="com.liu.pojo.People" autowire="byType">
        <property name="name" value="小明"></property>
    </bean>
</beans>
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号