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文件将会十分庞大:如果按需求分 ...
随机推荐
- 博弈论 && 题目
终于我也开始学博弈了,说了几个月,现在才学.学多点套路,不深学.(~~) 参考刘汝佳蓝书p132 nim游戏: 假设是两维的取石子游戏,每次可以在任意一堆拿任意数量个(至少一根,因为这样游戏的状态集有 ...
- Spark Mllib里的Mllib基本数据类型(图文详解)
不多说,直接上干货! Spark Mllib基本数据类型,根据不同的作用和应用场景,分为四种不同的类型 1.Local vector : 本地向量集,主要向spark提供一组可进行操作的数据集合 2 ...
- 小G搭积木
A小 G 搭积木文件名 输入文件 输出文件 时间限制 空间限制box.cpp box.in box.out 2s 128MB题目描述小 G 喜欢搭积木.小 G 一共有 n 块积木,并且积木只能竖着一块 ...
- 1169 传纸条 2008年NOIP全国联赛提高组 个人博客:attack.cf
1169 传纸条 2008年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 小渊和小轩 ...
- 评价PE基金绩效的常用指标
作为信息系统,辅助管理层决策是重要的功能之一.前文介绍了PE基金管理系统的建设,对PE业务的运转有了一些了解,但没有介绍如何评价PE基金的绩效,而这是管理层作出重大决策的主要依据之一.PE基金本质也是 ...
- Python之时间表示
Python的time模块中提供了丰富的关于时间操作方法,可以利用这些方法来完成这个需求. time.time() :获取当前时间戳 time.ctime(): 当前时间的字符串形式 time.loc ...
- 字符编码ANSI和ASCII区别、Unicode和UTF-8区别
ANSI码ANSI编码是一种对ASCII码的拓展:ANSI编码用0x00~0x7f (即十进制下的0到127)范围的1 个字节来表示 1 个英文字符,超出一个字节的 0x80~0xFFFF 范围来表示 ...
- 原型模式 -- JavaScript语言的灵魂
原型模式就是将原型对象指向创建对象的类,使这些类共享原型对象的方法与属性.JS是基于原型链实现对象之间的继承,是对属性或者方法的共享,而不是对属性和方法的复制. // 图片轮播类 var LoopIm ...
- Form组件的验证流程及扩展(钩子)
Form组件的验证流程及扩展(钩子) 常用的form class TestForm(Form): t1 = fields.CharField( widget=widgets.Textarea # 输入 ...
- Python面向对象(三)
类的使用:实例化.属性引用 实例化 g1 = Garen('草丛伦1') # 实例化 g2 = Garen('草丛伦2') g3 = Garen('草丛伦3') 类的属性:变量和函数 print(Ga ...