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来配置的,分别有属性注入.构造函数注入和工厂方 ...
随机推荐
- 教你从头到尾利用DQN自动玩flappy bird(全程命令提示,GPU+CPU版)【转】
转自:http://blog.csdn.net/v_JULY_v/article/details/52810219?locationNum=3&fps=1 目录(?)[-] 教你从头到尾利用D ...
- PL/SQL 06 函数 function
--函数 create or replace function 函数名称(参数1 类型1,参数2 类型2,...) return 数据类型as 变量.常量声明;begin 代码;end; cr ...
- PL/SQL 03 流程控制
--IF语法IF condition THEN statements;[ELSIF condition THEN statements;][ELSE statements;]END IF; -- ...
- golang之http
go获取html页面 package main import ( "fmt" "io/ioutil" "net/http" "ne ...
- javascript:入门笔记
1:html注释: <html> <body> <script type="text/javascript"> <!-- document ...
- [设计模式-行为型]观察者模式(Observer)
一句话 事件监听就是观察者模式最好的例子. 概括
- Zookeeper概念学习系列之zookeeper实现分布式进程监控
不多说,直接上干货! 假设要监控多台服务器上的A程序运行状态, 当发现有服务器上的A程序下线的时候, 给管理员发短信, 并且尝试重启A程序. zookeeper实现分布式进程监控主要利用zk的临时节点 ...
- springBoot 发布war包
1.packaging 改为war <packaging>war</packaging> 2.剔除内置tomcat <dependency> <groupId ...
- linux上redis的安装与配置
1.redis安装 wget http://download.redis.io/releases/redis-4.0.8.tar.gz tar xzf redis-4.0.8.tar.gz ln -s ...
- Spring创建对象的原理
当容器启动时,首先会加载给定的配置文件,将配置文件逐行解析.当解析到bean标签时,根据class属性的值,通过反射调用创建对象. 将创建好的对象存储到Spring自身维护的Map当中.map中的ke ...