spring注入之使用标签 @Autowired @Qualifier
使用标签的缺点在于必需要有源代码(由于标签必须放在源代码上),当我们并没有程序源代码的时候。我们仅仅有使用xml进行配置。
比如我们在xml中配置某个类的属性
- <bean name="studentService"class="com.bjsxt.service.StudentService">
- <property name="studentDao"ref="stuDaoImpl"></property><!-- 普通值用value 对于特定的类的对象则用ref须要注意的是 ref须要在xml文件里进行配置-->
- </bean>
(1)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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config /><!-- 自己主动装配一定要加上此片段-->
<bean name="s"class="com.bjsxt.dao.impl.StudentDaoImpl"></bean>
<bean name="student"class="com.bjsxt.model.Student" scope="singleton" lazy-init="true"init-method="init" destroy-method="destroy"></bean>
<!-- prototype 每次生成的对象不同 prototype
lazy-init=false 默认在容器进行初始化的时候就初始化了该对象
-->
<bean name="studentService"class="com.bjsxt.service.StudentService"> </bean>
</beans>
(2)类 注意在这里的两种装配方式 第一种由于破坏了封装性不建议 spring容器在进行查找时。是依照xml进行查找
packagecom.bjsxt.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier; importcom.bjsxt.dao.*;
import com.bjsxt.dao.impl.*;
importcom.bjsxt.model.*;
public class StudentService {
/*@Autowired //依照类型进行匹配
@Qualifier("s")依照名称进行匹配 第一种表示方式放在属性名的前面
*/
privateStudentDao studentDao;
//减少了耦合性:StudentService并不知道详细由谁来保存学生s 由ioc容器来控制装配 publicStudentDao getStudentDao() {
return studentDao;
}
@Autowired//放到set方法上
public void setStudentDao( @Qualifier("s")StudentDao studentDao) {
this.studentDao = studentDao;
}
public void add(Student s)
{
this.studentDao.StudentSave(s);
}
}
spring注入之使用标签 @Autowired @Qualifier的更多相关文章
- 学习 Spring (九) 注解之 @Required, @Autowired, @Qualifier
Spring入门篇 学习笔记 @Required @Required 注解适用于 bean 属性的 setter 方法 这个注解仅仅表示,受影响的 bean 属性必须在配置时被填充,通过在 bean ...
- spring注入注解@Resource和@Autowired
一.@Autowired和@Qualifier @Autowired是自动注入的注解,写在属性.方法.构造方法上,会按照类型自动装配属性或参数.该注解,可以自动装配接口的实现类,但前提是spring容 ...
- Spring注入方式及用到的注解
注入方式: 把DAO实现类注入到service实现类中,把service的接口(注意不要是service的实现类)注入到action中,注 入时不要new 这个注入的类,因为spring会自动注入,如 ...
- Spring注解标签详解@Autowired @Qualifier等 @Slf4j
@Slf4j @Slf4j注解实现日志输出 自己写日志的时候,肯定需要: private final Logger logger = LoggerFactory.getLogger(LoggerTes ...
- Spring @Resource,@Autowired,@Qualifier的注解注入和区别
spring2.5提供了基于注解(Annotation-based)的配置,我们可以通过注解的方式来完成注入依赖.在Java代码中可以使用 @Resource或者@Autowired注解方式来经行注入 ...
- Spring 注解原理(三)@Qualifier @Value
Spring 注解原理(三)@Qualifier @Value Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) 一.Aut ...
- spring mvc常用注解标签
@Controller 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model , ...
- spring : springmvc常用注解标签详解(转)
新的项目,新的学习,好久没用这些注解了,同时在学习使用shiro ,lucene 等等.在网上找了些博文,感谢作者的总结和分享. 欢迎交流,言归正传: 1.@Controller 在SpringMVC ...
- spring 注入bean的两种方式
我们都知道,使用spring框架时,不用再使用new来实例化对象了,直接可以通过spring容器来注入即可. 而注入bean有两种方式: 一种是通过XML来配置的,分别有属性注入.构造函数注入和工厂方 ...
随机推荐
- [Leetcode Week4]Course Schedule II
Course Schedule II题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/course-schedule-ii/description/ De ...
- BeeFramework 系列一 安装篇(Arc)
http://ilikeido.iteye.com/blog/1881390 Beeframework 是一款iOS快速开发框架,它以UISignal强大的路由功能替代原有Delegate方式,完成复 ...
- 内存管理相关函数 -- Linux【转】
转自:http://blog.csdn.net/cy_cai/article/details/47001245 1.kmalloc()/kfree() static __always_inline v ...
- 利用cron监视后台进程状态
利用cron监视后台进程状态 http://blog.csdn.net/dyx810601/article/details/72967758 1. 利用cron监视后台进程状态,如果进程死掉或服务器重 ...
- 5.shell运算符
无论是哪门语言,运算都是跑不掉的,shell中如何进行运算呢?语法: $((运算式))或者 $[运算式]expr 运算式.
- Android开发工具--AndroidStudio
1.Android studio更改快捷键File->setttings 搜索key map就可以更改成自己喜欢的会计键风格了
- JavaScript的条件运算符与条件语句
1.条件运算符 比较运算符 == 判断左右两边数据的值是否相等 === 判断左右两边数据的之是否相等,同时还判断两边的数据类型是否一样 != 比较运算符的比较结果都是布尔值,true或者f ...
- hdu 5171(矩阵快速幂,递推)
GTY's birthday gift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- Qt笔记——Event
#ifndef MYBUTTON_H #define MYBUTTON_H #include <QPushButton> class MyButton : public QPushButt ...
- Spark部署及应用
在飞速发展的云计算大数据时代,Spark是继Hadoop之后,成为替代Hadoop的下一代云计算大数据核心技术,目前Spark已经构建了自己的整个大数据处理生态系统,如流处理.图技术.机器学习.NoS ...