//

 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. java lombok包在maven已经配置,但是注解没用

    如果你是用eclipse作为开发环境,配置了maven依赖以后,还需要在eclipse/myeclipse中手动安装lombok. 其实就是加一个jar包,添加2行代码 1. 将 lombok.jar ...

  2. MVC3 自定义的错误页

    ASP.NET MVC3中如果配置文件出错了,怎么跳转到自定义的错误页,现在参考网上的档案是说 添加 如下配置文件,并且在路径Views/Shared/下添加Error页面,测试下没有用的,请大家看看 ...

  3. 【css】css2实现两列三列布局的方法

    前言 对于 flex 弹性布局相信大家都有所了解,它是 css3 中的属性,然而它具有一定的兼容性问题.楼主前几天面试时遇到了面试官需要设计一个两列布局,我当然就说父元素 flex 吧哩吧啦,然而需要 ...

  4. Git入门学习总结

    用了两天时间看完廖雪峰老师的git教程(http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b0 ...

  5. U3D加载服务器上的assetbundle

    在Unity3D中,如果加载服务器上的AssetBundle,总是会提示找不到crossdomain.xml文件,即使添加了该文件,也会报同样的错误.属于跨域访问报错的问题. 官方的解决方案如下: h ...

  6. SQL Server 2016,2014 “无法找到数据库引擎启动句柄”

    当我决定安装SharePoint 2016 IT预览版时,我想我应该将它安装在Windows Server 2016技术预览版以及SQL Server 2016社区技术预览版(CTP)上.我敢打赌,你 ...

  7. Python+selenium之下载文件

    一.Firefox文件下载 Web容许我们设置默认的文件下载路劲,文件会自动下载并且存放在指定的目录下. from selenium import webdriver import os fp = w ...

  8. 解决ubuntu上ifconfig没有eth0/ens33且无法上网的问题

    ifconfig只有一个轮回端口lo,没有我们的网卡eth0,一开始以为是vsphere(新手对于vsphere不是很熟悉)上我的虚拟机配置问题,还查看了相关的网络配置,后来才知道是因为: 问题出在配 ...

  9. 常用的ement语法

    缩写语法: 介绍:Emmet 使用类似于 CSS 选择器的语法描述元素在生成的文档树中的位置及其属性. 声明:第一次写博客大家多多关照,如有错误或者需要补充的请到评论里留言,谢谢大家! 快速生成htm ...

  10. Oracle Real Application Clusters (RAC)

    Oracle Real Application Clusters — 概述 包含 Oracle Real Application Clusters (RAC) 选件的 Oracle 数据库允许依托一组 ...