1. 程式人生 > >spring註解方式註入bean

spring註解方式註入bean

ota alt contex () implement bstr mage 所在 lap

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

技術分享圖片

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 }

spring註解方式註入bean