spring_Demo4_Annotation_Resource
来源:互联网 发布:炫浪网络社区下载不了 编辑:程序博客网 时间:2024/06/06 03:06
来源参考:
@ResourceSpring also supports injection using the JSR-250 @Resource annotation on fields or bean property setter methods. This is a common pattern in Java EE 5 and 6, for example in JSF 1.2 managed beans or JAX-WS 2.0 endpoints. Spring supports this pattern for Spring-managed objects as well.@Resource takes a name attribute, and by default Spring interprets that value as the bean name to be injected. In other words, it follows by-name semantics, as demonstrated in this example:public class SimpleMovieLister { private MovieFinder movieFinder; @Resource(name="myMovieFinder") public void setMovieFinder(MovieFinder movieFinder) { this.movieFinder = movieFinder; }}If no name is specified explicitly, the default name is derived from the field name or setter method. In case of a field, it takes the field name; in case of a setter method, it takes the bean property name. So the following example is going to have the bean with name "movieFinder" injected into its setter method:public class SimpleMovieLister { private MovieFinder movieFinder; @Resource public void setMovieFinder(MovieFinder movieFinder) { this.movieFinder = movieFinder; }}
demo 参考:
bean.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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><context:annotation-config /> <bean id="userDAO" class="com.dao.impl.UserDaoImpl"> </bean> <bean id="user2" class="com.dao.impl.UserDaoImpl"> </bean> <bean id="userService" class="com.service.UserService"> </bean></beans>
service
package com.service;import javax.annotation.Resource;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import com.dao.UserDAO;import com.model.User;public class UserService {private UserDAO userDAO; public void add(User user) {userDAO.saveMySql(user);}public UserDAO getUserDAO() {return userDAO;}/** * @Resource 默认使用userDAO * @Resource(name = "user2") * @param userDAO *///@Resource@Resource(name = "user2")public void setUserDAO(UserDAO userDAO) {this.userDAO = userDAO;}}
阅读全文
0 0
- spring_Demo4_Annotation_Resource
- 3.1 FFT概述
- 国外网站类似于stackoverflow、github访问慢的解决办法
- Ubuntu下在QTCreator中使用glew.h
- CountDownLatch与AIDL获取应用软件大小结合使用实例
- php的token详解
- spring_Demo4_Annotation_Resource
- 【log4j】log4j日志框架使用总结
- 关于单点登录的原理
- Kotlin笔记(五)——继承
- SQL常用命令语句收集
- 大数据和高并发的解决方案总结
- 深入理解Java虚拟机-- java虚拟机字节码执行引擎浅析
- 3.2 时间抽取基2-FFT算法
- Logistic回归原理及公式推导