spring关于@Autowired和@Qualifier的使用

//

1 package com.jhc.model;
2 
3 import org.springframework.stereotype.Component;
4 
5 @Component
6 public interface ModelTest3 {
7     void sayHello();
8 }

//

 1 package com.jhc.model;
 2 
 3 import org.springframework.stereotype.Component;
 4 
 5 @Component
 6 public class ModelTest3_1 implements ModelTest3 {
 7 
 8     @Override
 9     public void sayHello() {
10         System.out.println("hello word modelTest3_1");
11     }
12 }

//

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");
    }
}

//

 1 package com.jhc.model;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.beans.factory.annotation.Qualifier;
 5 import org.springframework.stereotype.Component;
 6 
 7 @Component
 8 public class TestModelTest3 {
 9 
10     @Autowired
11     @Qualifier("modelTest3_1")
12     private ModelTest3 modelTest3=null;
13 
14 
15     public void sayHello(){
16         modelTest3.sayHello();
17     }
18 
19 }

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

 1 package com.jhc.test;
 2 
 3 //import com.jhc.model.PojoConfig;
 4 import com.jhc.model.TestModelTest3;
 5 import org.springframework.context.ApplicationContext;
 6 //import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 7 import org.springframework.context.support.ClassPathXmlApplicationContext;
 8 
 9 public class Test3 {
10     public static void main(String[] args){
11         //ApplicationContext applicationContext=new AnnotationConfigApplicationContext(PojoConfig.class);
12         ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring-config.xml");
13         TestModelTest3 testModelTest3=applicationContext.getBean(TestModelTest3.class);
14         testModelTest3.sayHello();
15     }
16 }

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

1 /*package com.jhc.model;
2 
3 import org.springframework.context.annotation.ComponentScan;
4 
5 @ComponentScan
6 public class PojoConfig {
7 }*/

 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xsi:schemaLocation=
 6                "http://www.springframework.org/schema/beans
 7            http://www.springframework.org/schema/beans/spring-beans.xsd
 8            http://www.springframework.org/schema/context
 9            http://www.springframework.org/schema/context/spring-context.xsd
10  ">
11     <!--数据源配置-->
12     <!--<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
13         <property name="url" value="jdbc:mysql://localhost:3306/study2"/>
14         <property name="driver" value="com.mysql.jdbc.Driver"/>
15         <property name="username" value="root"/>
16         <property name="password" value="mysql"/>
17     </bean>-->
18     <!--扫描com.jhc包路径下的类-->
19     <context:component-scan base-package="com.jhc"></context:component-scan>
20 
21     <!--第三方数据库连接池-->
22     <!--<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
23         <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
24         <property name="url" value="jdbc:mysql://localhost:3306/study2"/>
25         <property name="username" value="root"/>
26         <property name="password" value="mysql"/>
27         最大连接数
28         最大等待连接中的数量
29         最大等待毫秒数
30         <property name="maxTotal" value="255"/>
31         <property name="maxIdle" value="5"/>
32         <property name="maxWaitMillis" value="10000"/>
33     </bean>-->
34 
35 
36     <!--jdbcTemplate-->
37     <!--<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
38         <property name="dataSource" ref="dataSource"/>
39     </bean>-->
40 
41 
42 </beans>

//运行结果是

hello word modelTest3_1

 

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

posted @ 2018-11-26 20:18  左耳听过流年的声  阅读(1171)  评论(0编辑  收藏  举报