spring注释注入

    技术2022-06-08  63

     package ky.springtest.method; import ky.springtest.dao.MySpr; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; public class MethodLookM { @Autowired@Qualifier("mySpr") private MySpr mySpr;//注入对象 public void mLookM(){ mySpr.say(); } }

     

     

     

     

    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-3.0.xsd"> <bean id="mySpr" class="ky.springtest.dao.MySpr"> <property name="cm"> <ref bean="Duck" /> </property> </bean> <bean id="Duck" class="ky.springtest.animal.Duck"> <property name="name"> <value>Duck</value> </property> </bean> <bean id="methodLookM" class="ky.springtest.method.MethodLookM"></bean> <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> <!-- 使用Autowired就必须在xml中导入上面的类文件 --> </beans>


    最新回复(0)