spring配置文件代码如下:
注意:使用annotation需要加入common-annotation.jar这个工具包
然后只要在java类里面对应的属性名上面加上@Resource即可
注意:@Resource注解(j2ee提供的)和@Autowired(Spring提供的)一样,也可以标注在字段或者属性的setter方法上,但它默认按名称装配,名称可以通过@Resource的name属性指定,如果没有指定name属性,当注解标注在字段上,即默认取字段的名称作为bean名称寻找依赖对象,当注解标注在setter方法上,即默认取属性名作为bean名称寻找依赖对象。
<?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名称寻找依赖对象。
Spring依赖注入详解
本文介绍了Spring框架中依赖注入的配置方式,包括XML配置文件的结构和使用@Resource及@Autowired注解进行依赖注入的方法。同时,还讲解了@Resource注解的默认行为和如何通过name属性指定依赖名称。
868

被折叠的 条评论
为什么被折叠?



