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;}}

其他可参考前面的代码



原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 魅族mx4pro玩王者荣耀卡怎么办 魅蓝5s玩游戏卡怎么办 魅蓝s6玩游戏卡怎么办 OPPO王者荣耀对局闪退怎么办 魅族手机太慢怎么办 魅蓝5信号不好怎么办 魅蓝数据网速慢怎么办 魅族联通网速慢怎么办 魅族手机wifi信号弱怎么办 魅蓝e2信号差怎么办 魅蓝e2gps信号弱怎么办 魅族网络信号差怎么办 魅族手机gps信号弱怎么办 魅族手机突然没有信号怎么办 魅族手机流量信号不好怎么办 魅族手机wifi信号差怎么办 魅族5s信号不好怎么办 魅族mx5的双击不亮屏怎么办 魅族mx5返回键失灵怎么办 电信苹果3g网速慢怎么办 魅蓝6开不了机怎么办 手机应用被锁了怎么办 魅族电池不耐用怎么办 魅族mx6现在很卡怎么办 魅族e2手机屏幕背景黑色怎么办 魅族x6手机锁了怎么办 360n5返回键失灵怎么办 360n5返回键不好用怎么办 魅族手机锁屏怎么办 魅族手机锁住了怎么办 手机己锁定怎么办魅族 魅蓝u10触屏没反应怎么办 魅蓝e2手机锁定怎么办 苹果手机声音键坏了怎么办 一加6的屏幕问题怎么办 魅蓝note6卡顿怎么办 苹果4s内屏坏了怎么办 魅族mx6一直重启怎么办 魅族充电慢了怎么办啊 笔记本玩游戏掉帧怎么办 psv玩游戏掉帧怎么办