//

 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. 超全面的vue.js使用总结

    一.Vue.js组件 vue.js构建组件使用 Vue.component('componentName',{ /*component*/ }): 这里注意一点,组件要先注册再使用,也就是说: Vue ...

  2. sqlserver 删除两种方法比较

    delete from tb where id=1    删除数据 truncate table  tb where id=1  第一种 自增长id不会删除,第二种会删除

  3. css3背景与边框相关样式

    background-attachment          背景图像是否固定或者随着页面的其余部分滚动 background-color                    设置元素的背景颜色 b ...

  4. git-gui:使用终端打开以后出现错误提示 Spell checking is unavable

    参考链接:http://www.lai18.com/content/10706682.html 安装了git-gui,打开以后出现以下提示: Spell checking is unavable: e ...

  5. mysql 忘记root密码的解决办法

    1.修改MySQL的登录设置: # vim /etc/my.cnf 在[mysqld]的段中加上一句:skip-grant-tables 例如: [mysqld] datadir=/var/lib/m ...

  6. Android商城开发系列(二)——App启动欢迎页面制作

    商城APP一般都会在应用启动时有一个欢迎界面,下面我们来实现一个最简单的欢迎页开发:就是打开商城App,先出现欢迎界面,停留几秒钟,自动进入应用程序的主界面. 首先先定义WelcomeActivity ...

  7. 使 windows 无需输入开机密码自动进入系统

    步骤 运行netplwiz: 勾去图中所示复选框,确定后输入密码.

  8. python基础教程总结10——文件

    1.打开文件 open(name[mode[,buffing])    参数:  文件,模式,缓冲 1)name: 是强制选项,模式和缓冲是可选的 #如果文件不在,会报下面错误1 >>&g ...

  9. 获取地址栏参数,json遍历

    1. 获取地址栏参数 GetQueryString: function(name){ // 获取地址栏参数 var reg = new RegExp("(^|&)"+ na ...

  10. javaweb基础(19)_jsp标签

    一.JSP标签介绍 JSP标签也称之为Jsp Action(JSP动作)元素,它用于在Jsp页面中提供业务逻辑功能,避免在JSP页面中直接编写java代码,造成jsp页面难以维护. 二.JSP常用标签 ...