使用注解注入步骤
1、加入xsd【头文件中】
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
2、在xsd文件后面,bean结点之前加入
①、<context:annotation-config></context:annotation-config>
②、 < context:component-scan base-package = "你要注入属性的文件位置的包名,即目的地" /> 【小白还没试验】
3、以userDaoImpl为例
①、userDaoImpl的结点
<bean id="userDaoImpl" class="dao.impl.UserDaoImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
②、在需要注入的UserServiceImpl中
@Resource【代表使用注释的方式注入】
private UserDao userDaoImpl;
(实质:在注入的过程,会先使用名称对比注入,再使用类型对比注入
如上面的例子中,会先用UserServiceImpl中的userDaoImpl和xml文件中的bean结点的id比较,如果相同,则自动注入,如果不同,则用类型{dao.impl.UserDaoImpl}来对比,推荐使用名称对比)
1、加入xsd【头文件中】
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
2、在xsd文件后面,bean结点之前加入
①、<context:annotation-config></context:annotation-config>
②、 < context:component-scan base-package = "你要注入属性的文件位置的包名,即目的地" /> 【小白还没试验】
3、以userDaoImpl为例
①、userDaoImpl的结点
<bean id="userDaoImpl" class="dao.impl.UserDaoImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
②、在需要注入的UserServiceImpl中
@Resource【代表使用注释的方式注入】
private UserDao userDaoImpl;
(实质:在注入的过程,会先使用名称对比注入,再使用类型对比注入
如上面的例子中,会先用UserServiceImpl中的userDaoImpl和xml文件中的bean结点的id比较,如果相同,则自动注入,如果不同,则用类型{dao.impl.UserDaoImpl}来对比,推荐使用名称对比)