1.@Resource(重要)
a)加入 :j2ee/common-annotations.jar
b)默认按名称,名称找不到,按类型
默认按照名称setName1到xml中找和id相同的,没有的话再找和SetName类型一样的
public class HelloWorld { SetName setName; @Resource public void setMessage(SetName setName1){ this.setName=setName1; System.out.println("setmessage"); setName1.getSetName() ; }
}
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config/> <bean id="helloWorld" class="com.yibai.HelloWorld"> <!-- <property name="message" value="Hello World!"/> --> </bean> <bean id="setName1" class="com.yibai.SetName"> </bean> <bean id="setname12" class="com.yibai.SetName"> </bean> </beans>
原文:http://www.cnblogs.com/paisen/p/4382067.html