使用标签的缺点在于必需要有源代码(由于标签必须放在源代码上),当我们并没有程序源代码的时候。我们仅仅有使用xml进行配置。

比如我们在xml中配置某个类的属性

          

  1. <bean name="studentService"class="com.bjsxt.service.StudentService">
  2. <property name="studentDao"ref="stuDaoImpl"></property><!-- 普通值用value 对于特定的类的对象则用ref须要注意的是 ref须要在xml文件里进行配置-->
  3. </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的更多相关文章

  1. 学习 Spring (九) 注解之 @Required, @Autowired, @Qualifier

    Spring入门篇 学习笔记 @Required @Required 注解适用于 bean 属性的 setter 方法 这个注解仅仅表示,受影响的 bean 属性必须在配置时被填充,通过在 bean ...

  2. spring注入注解@Resource和@Autowired

    一.@Autowired和@Qualifier @Autowired是自动注入的注解,写在属性.方法.构造方法上,会按照类型自动装配属性或参数.该注解,可以自动装配接口的实现类,但前提是spring容 ...

  3. Spring注入方式及用到的注解

    注入方式: 把DAO实现类注入到service实现类中,把service的接口(注意不要是service的实现类)注入到action中,注 入时不要new 这个注入的类,因为spring会自动注入,如 ...

  4. Spring注解标签详解@Autowired @Qualifier等 @Slf4j

    @Slf4j @Slf4j注解实现日志输出 自己写日志的时候,肯定需要: private final Logger logger = LoggerFactory.getLogger(LoggerTes ...

  5. Spring @Resource,@Autowired,@Qualifier的注解注入和区别

    spring2.5提供了基于注解(Annotation-based)的配置,我们可以通过注解的方式来完成注入依赖.在Java代码中可以使用 @Resource或者@Autowired注解方式来经行注入 ...

  6. Spring 注解原理(三)@Qualifier @Value

    Spring 注解原理(三)@Qualifier @Value Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) 一.Aut ...

  7. spring mvc常用注解标签

    @Controller 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model , ...

  8. spring : springmvc常用注解标签详解(转)

    新的项目,新的学习,好久没用这些注解了,同时在学习使用shiro ,lucene 等等.在网上找了些博文,感谢作者的总结和分享. 欢迎交流,言归正传: 1.@Controller 在SpringMVC ...

  9. spring 注入bean的两种方式

    我们都知道,使用spring框架时,不用再使用new来实例化对象了,直接可以通过spring容器来注入即可. 而注入bean有两种方式: 一种是通过XML来配置的,分别有属性注入.构造函数注入和工厂方 ...

随机推荐

  1. http://www.himigame.com/mac-cocoa-application/893.html

    [Cocoa(mac) Application 开发系列之一]创建第一个application—计算器 终于HTTP与Socket服务器以及cocos2dx之间的通信各种框架成功完成后,现在抽时间学习 ...

  2. [ MongoDB ] 副本集的搭建及测试

    Replica Sets  复制 (副本集) node1: 10.0.0.10node2: 10.0.0.11node3: 10.0.0.12 副本集结构图:

  3. hdu4240 求一条流量最大的路/(此题网上百分之90以上算法是错误的)

    题意:求最大流/一条流量最大的路的流量.(此题HDU上数据水,下面俩种错误的都能过....) 思路1;每次增广的时候更新流量,保存最大的那条.  错误性:每次更新,有可能最大的那条流量是前几次已经增广 ...

  4. 【hdoj_1009】FatMouse's Trade

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1009< 本题用到贪心策略和结构体排序. 问题简化:现有资本M,N个房间,第i个房间对应着价格为F[i ...

  5. JS面试题及答案

    一.JS闭包. f = function() {return true;};    g = function() {return false;};    (function() {       if ...

  6. MySQL数据库中的Date,DateTime,int,TimeStamp和Time类型的对比

    DATETIME 用在你需要同时包含日期和时间信息的值时.MySQL检索并且以'YYYY-MM-DD HH:MM:SS'格式显示DATETIME值,支持的范围是'1000-01-01 00:00:00 ...

  7. 虚拟机Visualbox安装Ubuntu Server

    关于虚拟机的新建及设置,请查看<Visualbox安装Ubuntu网络设置> 从光盘启动系统中,首先是选择语言,我这里选择英文 选择英文安装Ubuntu服务器 继续选择英文 选择地理位置, ...

  8. docker 与 yarn

    有时我们的项目是使用yarn去发布的,当需要使用docker发布这个项目时,安装yarn是必须的,但是平时使用的npm install -g yarn此时却不可用 从网站上找到解决的方法 地址:htt ...

  9. Python3 list与循环练习(购物车)

    #!/usr/bin/env python3 # -*- coding: utf-8 -*- # Author;Tsukasa product_list = [ ('Iphone',5800), (' ...

  10. AHOI2009中国象棋

    首先以行为阶段,根据象棋的规则,在同一行中,至多只能有两个炮,同理:在同一列中,至多只能有两个炮思考一个可以覆盖整个状态空间的dp数组:dp[i]表示到了第i行接下来我们想:某列中的炮能否通过位运算求 ...