Spring使用annotation
spring配置文件代码如下:
<?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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config/></beans>
注意:使用annotation需要加入common-annotation.jar这个工具包
然后只要在java类里面对应的属性名上面加上@Resource即可
注意:@Resource注解(j2ee提供的)和@Autowired(Spring提供的)一样,也可以标注在字段或者属性的setter方法上,但它默认按名称装配,名称可以通过@Resource的name属性指定,如果没有指定name属性,当注解标注在字段上,即默认取字段的名称作为bean名称寻找依赖对象,当注解标注在setter方法上,即默认取属性名作为bean名称寻找依赖对象。