//

 package com.jhc.model;

 import org.springframework.stereotype.Component;

 @Component
public interface ModelTest3 {
void sayHello();
}

//

 package com.jhc.model;

 import org.springframework.stereotype.Component;

 @Component
public class ModelTest3_1 implements ModelTest3 { @Override
public void sayHello() {
System.out.println("hello word modelTest3_1");
}
}

//

package com.jhc.model;

import org.springframework.stereotype.Component;

@Component
public class ModelTest3_2 implements ModelTest3 {
@Override
public void sayHello() {
System.out.println("hello word modelTest3_2");
}
}

//

 package com.jhc.model;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component; @Component
public class TestModelTest3 { @Autowired
@Qualifier("modelTest3_1")
private ModelTest3 modelTest3=null; public void sayHello(){
modelTest3.sayHello();
} }

//@Qualifier("modelTest3_1")处的参数一定要与实现类的名字相同,且首字母小写

 package com.jhc.test;

 //import com.jhc.model.PojoConfig;
import com.jhc.model.TestModelTest3;
import org.springframework.context.ApplicationContext;
//import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test3 {
public static void main(String[] args){
//ApplicationContext applicationContext=new AnnotationConfigApplicationContext(PojoConfig.class);
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring-config.xml");
TestModelTest3 testModelTest3=applicationContext.getBean(TestModelTest3.class);
testModelTest3.sayHello();
}
}

//上面第11行与第12行是两种方法,二选一,如果采用第11行处方法就添加如下代码(注意取消注释),此时可以删除XML文件的第19行代码,注意XML第19行的扫描范围,和下面被注释代码的包名

 /*package com.jhc.model;

 import org.springframework.context.annotation.ComponentScan;

 @ComponentScan
public class PojoConfig {
}*/
 <?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
">
<!--数据源配置-->
<!--<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<property name="url" value="jdbc:mysql://localhost:3306/study2"/>
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="username" value="root"/>
<property name="password" value="mysql"/>
</bean>-->
<!--扫描com.jhc包路径下的类-->
<context:component-scan base-package="com.jhc"></context:component-scan> <!--第三方数据库连接池-->
<!--<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/study2"/>
<property name="username" value="root"/>
<property name="password" value="mysql"/>
最大连接数
最大等待连接中的数量
最大等待毫秒数
<property name="maxTotal" value="255"/>
<property name="maxIdle" value="5"/>
<property name="maxWaitMillis" value="10000"/>
</bean>--> <!--jdbcTemplate-->
<!--<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>--> </beans>

//运行结果是

hello word modelTest3_1

欢迎广大朋友指出不足和纠错,在下将不胜感激

spring关于@Autowired和@Qualifier的使用的更多相关文章

  1. Spring 的@@Autowired 和 @Qualifier注释

    @Autowired spring2.1中允许用户通过@Autowired注解对Bean的属性变量.属性Setter方法以及构造方法进行标注,配合AutowiredAnnotationBeanProc ...

  2. Spring注解@Component、@Repository、@Service、@Controller @Resource、@Autowired、@Qualifier、@scope

    以下内容摘自部分网友的,并加上了自己的理解 @Service用于标注业务层组件(我们通常定义的service层就用这个) @Controller用于标注控制层组件(如struts中的action.Sp ...

  3. Spring 学习——Spring常用注解——@Component、@Scope、@Repository、@Service、@Controller、@Required、@Autowired、@Qualifier、@Configuration、@ImportResource、@Value

    Bean管理注解实现 Classpath扫描与组件管理 类的自动检测与注册Bean 类的注解@Component.@Service等作用是将这个实例自动装配到Bean容器中管理 而类似于@Autowi ...

  4. Spring注解 @Component、@Repository、@Service、@Controller @Resource、@Autowired、@Qualifier 解析

    @Repository.@Service.@Controller 这几个是一个类型,其实@Component 跟他们也是一个类型的 Spring 2.5 中除了提供 @Component 注释外,还定 ...

  5. Spring注解之@Autowired、@Qualifier、@Resource、@Value

    前言 @Autowired.@Qualifier.@Resource.@Value四个注解都是用于注入数据的,他们的作用就和在xml配置文件中的bean标签中写一个标签的作用是一样的!本篇中特别要讲解 ...

  6. 关于Spring注解@Component、@Repository、@Service、@Controller @Resource、@Autowired、@Qualifier 解析

    1.Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository.@Service和 @Controller 其实这三个跟@Com ...

  7. Spring 注释 @Autowired 和@Resource

    一. @Autowired和@Resource都可以用来装配bean,都可以写在字段上,或者方法上. 二. @Autowired属于Spring的:@Resource为JSR-250标准的注释,属于J ...

  8. Spring 注释 @Autowired 和@Resource 的区别

    Spring 注释 @Autowired 和@Resource 的区别 一. @Autowired和@Resource都可以用来装配bean,都可以写在字段上,或者方法上. 二. @Autowired ...

  9. 04 Spring的@Autowired注解、@Resource注解、@Service注解

    什么是注解 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事务,这么做有两个缺点: 1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分 ...

随机推荐

  1. Ubuntu下安装Yarm-PM2

    首先打开yarm的官网.https://www.yarnpkg.com/zh-Hant/ (一)yarn的官方安装方法: 1.上通过 Debian 套件安裝 Yarn,粘贴以下命令 curl -sS ...

  2. tyvj P4877 _1.组合数

    时间限制:1s 内存限制:256MB [问题描述] 从m个不同元素中,任取n(n≤m)个元素并成一组,叫做从m个不同元素中取出n个元素的一个组合:从m个不同元素中取出n(n≤m)个元素的所有组合的个数 ...

  3. python基础---有关nparray----切片和索引(一)

    Numpy最重要的一个特点就是其N维数组对象,即ndarray,该对象是一种快速而灵活的大数据集容器,实际开发中,我们可以利用这种数组对整块数据执行一些数学运算. 有关ndarray,我们就从最简单的 ...

  4. java 使用uuid生成唯一字符串

    UUID(Universally Unique Identifier)全局唯一标识符,是指在一台机器上生成的数字,它保证对在同一时空中的所有机器都是唯一的.按照开放软件基金会(OSF)制定的标准计算, ...

  5. background-size在IE8不兼容问题

    background-size在IE8及以下浏览器不兼容:要解决的话要用滤镜: filter: progid: DXImageTransform.Microsoft.AlphaImageLoader( ...

  6. cssText在js中写样式表兼容全部

    oDiv.style.cssText="width:100px;height:200px;";是前面的升级版(oDiv.style.width='200px';) <styl ...

  7. git的常用操作指令

    git学习网址: http://www.backlogtool.com/git-guide/cn/intro/intro2_3.html 廖雪峰的git教程 git的工作区和暂存区(描述git的工作流 ...

  8. jsp四大作用域之Session

    <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding= ...

  9. lastlog命令

    lastlog——检查某特定用户上次登录的时间 命令所在路径:/usr/bin/lastlog 示例1: # lastlog 列出所有用户,并显示用户最后一次登录的时间等信息 示例2: # lastl ...

  10. eclipse中代码注释及其他常用快捷键

    html代码注释/取消注释             Ctrl + Shift + c php代码注释/取消注释                 Ctrl + / (4)Ctrl+Shift+/ 说明: ...