用注解的方式注入bean,spring的配置文件也要增加一些约束和导入注解所在的包

spring注解方式注入bean_基本数据类型

applicationContext.xml

 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="http://www.springframework.org/schema/beans 
 6                               http://www.springframework.org/schema/beans/spring-beans.xsd
 7                               http://www.springframework.org/schema/context 
 8                               http://www.springframework.org/schema/context/spring-context.xsd">
 9 
10     <context:component-scan base-package="com.xiaostudy.service"></context:component-scan>
11     <context:component-scan base-package="com.xiaostudy.dao"></context:component-scan>
12 </beans>

用注解注入的bean类PersonImple.java

 1 package com.xiaostudy.service;
 2 
 3 import javax.annotation.PostConstruct;
 4 import javax.annotation.PreDestroy;
 5 import javax.annotation.Resource;
 6 
 7 import org.springframework.beans.factory.annotation.Value;
 8 import org.springframework.context.annotation.Scope;
 9 import org.springframework.stereotype.Component;
10 /*
11  * bean注入
12  * 1、@Component("id")
13  * 2、WEB:
14  *             @Controller("id")    web
15  *             @Service("id")        service
16  *            @Repository("id")    dao
17  */
18 @Component("person")
19 /*
20  * bean作用域
21  * @Scope("singleton")        单例(也是默认)
22  * @Scope("prototype")        每new一次都是新的对象
23  */
24 @Scope("prototype")
25 public class PersonImple implements Person {
26     /*
27      * bean参数注入
28      * 1、普通参数(也就是说那些基本数据类型)
29      *         在参数上面或者在相应的set方法    @Value("值")
30      * 2、引用参数
31      *         2.1按照类型注入:@Autowired
32      *         2.2按照名称注入:@Autowired    @Qualifier("id")
33      *         2.3按照名称注入:@Resource
34      * 
35      */
36     //@Resource
37     private Dao_demoImple dao_demo;
38     @Value("xiaostudy")
39     private String name;
40     
41 
42     public String getName() {
43         return name;
44     }
45 
46     public void setName(String name) {
47         this.name = name;
48     }
49 
50     public Dao_demoImple getDao_demo() {
51         return dao_demo;
52     }
53 
54     @Resource
55     public void setDao_demo(Dao_demoImple dao_demo) {
56         this.dao_demo = dao_demo;
57     }
58     /*
59      * bean初始化方法注解
60      *         @PostConstruct
61      */
62     @PostConstruct
63     public void init() {
64         System.out.println("init()>>>>>>");
65     }
66     
67     /*
68      * bean销毁注解
69      *         @PreDestroy
70      */
71     @PreDestroy
72     public void destroy() {
73         System.out.println("destroy()>>>>>");
74     }
75     
76 }

Person接口

1 package com.xiaostudy.service;
2 
3 public interface Person {
4 
5 }

Dao_demo接口

1 package com.xiaostudy.dao;
2 
3 public interface Dao_demo {
4 
5 }

Dao_demoImple.java

 1 package com.xiaostudy.service;
 2 
 3 import org.springframework.beans.factory.annotation.Value;
 4 import org.springframework.stereotype.Component;
 5 
 6 import com.xiaostudy.dao.Dao_demo;
 7 @Component("dao_demo")
 8 public class Dao_demoImple implements Dao_demo {
 9     
10     private String name;
11     
12     public String getName() {
13         return name;
14     }
15 
16     @Value("demo")
17     public void setName(String name) {
18         this.name = name;
19     }
20 
21     public void add() {
22         System.out.println("add>>>>>>");
23     }
24     
25 }

测试类Test.java

 1 package com.xiaostudy.service;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.AbstractApplicationContext;
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 
 7 import com.xiaostudy.dao.Dao_demo;
 8 import com.xiaostudy.service.Person;
 9 
10 public class Test {
11 
12     public static void main(String[] args) {
13         ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
14         Person person = ac.getBean("person", Person.class);
15         System.out.println(person);
16         PersonImple pi = (PersonImple)person;
17         System.out.println(pi.getName());
18         Dao_demoImple dao_demo = pi.getDao_demo();
19         System.out.println(dao_demo);
20         dao_demo.add();
21         System.out.println(dao_demo.getName());
22         /*Person person1 = ac.getBean("person", Person.class);
23         Person person2 = ac.getBean("person", Person.class);
24         System.out.println(person1);
25         System.out.println(person2);
26         ((AbstractApplicationContext) ac).close();*/
27     }
28 
29 }