spring关于@Autowired和@Qualifier的使用
//
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的使用的更多相关文章
- Spring 的@@Autowired 和 @Qualifier注释
@Autowired spring2.1中允许用户通过@Autowired注解对Bean的属性变量.属性Setter方法以及构造方法进行标注,配合AutowiredAnnotationBeanProc ...
- Spring注解@Component、@Repository、@Service、@Controller @Resource、@Autowired、@Qualifier、@scope
以下内容摘自部分网友的,并加上了自己的理解 @Service用于标注业务层组件(我们通常定义的service层就用这个) @Controller用于标注控制层组件(如struts中的action.Sp ...
- Spring 学习——Spring常用注解——@Component、@Scope、@Repository、@Service、@Controller、@Required、@Autowired、@Qualifier、@Configuration、@ImportResource、@Value
Bean管理注解实现 Classpath扫描与组件管理 类的自动检测与注册Bean 类的注解@Component.@Service等作用是将这个实例自动装配到Bean容器中管理 而类似于@Autowi ...
- Spring注解 @Component、@Repository、@Service、@Controller @Resource、@Autowired、@Qualifier 解析
@Repository.@Service.@Controller 这几个是一个类型,其实@Component 跟他们也是一个类型的 Spring 2.5 中除了提供 @Component 注释外,还定 ...
- Spring注解之@Autowired、@Qualifier、@Resource、@Value
前言 @Autowired.@Qualifier.@Resource.@Value四个注解都是用于注入数据的,他们的作用就和在xml配置文件中的bean标签中写一个标签的作用是一样的!本篇中特别要讲解 ...
- 关于Spring注解@Component、@Repository、@Service、@Controller @Resource、@Autowired、@Qualifier 解析
1.Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository.@Service和 @Controller 其实这三个跟@Com ...
- Spring 注释 @Autowired 和@Resource
一. @Autowired和@Resource都可以用来装配bean,都可以写在字段上,或者方法上. 二. @Autowired属于Spring的:@Resource为JSR-250标准的注释,属于J ...
- Spring 注释 @Autowired 和@Resource 的区别
Spring 注释 @Autowired 和@Resource 的区别 一. @Autowired和@Resource都可以用来装配bean,都可以写在字段上,或者方法上. 二. @Autowired ...
- 04 Spring的@Autowired注解、@Resource注解、@Service注解
什么是注解 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事务,这么做有两个缺点: 1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分 ...
随机推荐
- 牛客网Java刷题知识点之抽象类与接口
不多说,直接上干货! 接口和内部类为我们提供了一种将接口与实现分离的更加结构化的方法. 抽象类与接口是Java语言中对抽象概念进行定义的两种机制,正是由于它们的存在才赋予java强大的面向对象的能力. ...
- mysql 存储过程变量及循环的使用
1.用游标循环 BEGIN -- 定义变量 -- 定义done DECLARE done INT; -- 定义 ammeter_id_bl DECLARE ammeter_id_bl DOUBLE; ...
- rabbitmq 不发送ack消息如何处理:rabbitmq可靠发送的自动重试机制
转载地址:http://www.jianshu.com/p/6579e48d18ae http://www.jianshu.com/p/4112d78a8753 接这篇 在上文中,主要实现了可靠模式的 ...
- autofac 注入生命周期
创建实例方法 1.InstancePerDependency 对每一个依赖或每一次调用创建一个新的唯一的实例.这也是默认的创建实例的方式. 官方文档解释:Configure the component ...
- React Router 4.0中文快速入门
import React from 'react' import { BrowserRouter as Router, Route, Link } from 'react-router-dom' co ...
- rest_framework组件之认证,权限,访问频率
共用的models from django.db import models # Create your models here. class User(models.Model): username ...
- css:hover伪类的使用
:hover的使用,即当鼠标指针移入元素时,所做出的样式设置 示例一 <!DOCTYPE html> <html lang="en"> <head&g ...
- SQL数据库基础二
- BZOJ 3130: [Sdoi2013]费用流 网络流+二分
3130: [Sdoi2013]费用流 Time Limit: 10 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 1230 Solved: ...
- openstack keystone修改horizon密码
命令行修改密码:token在/etc/keystone/keystone.conf中的一个admin_token export OS_SERVICE_TOKEN=165a1766c12a497b8fb ...