Spring-注解方式獲取對象


一、修改ApplicationContext.xml配置文件

將<bean>內容注釋掉,增加一句如下代碼:

<context:component-scan base-package="com.yanyan.pojo"/>

 此時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:aop="http://www.springframework.org/schema/aop"
 5     xmlns:tx="http://www.springframework.org/schema/tx"
 6     xmlns:context="http://www.springframework.org/schema/context"
 7     xsi:schemaLocation="
 8    http://www.springframework.org/schema/beans
 9    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
10    http://www.springframework.org/schema/aop
11    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
12    http://www.springframework.org/schema/tx
13    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
14    http://www.springframework.org/schema/context     
15    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
16   
17   
18       <context:component-scan base-package="com.how2java.pojo"/>
19   
20       <!-- <context:annotation-config/>
21     <bean name="c" class="com.how2java.pojo.Category">
22         <property name="name" value="category 1" />
23     </bean>
24     
25     <bean name='p' class="com.how2java.pojo.Product">
26         <property name="name" value="product1"/>
27         <property name="category" ref="c"/>
28     </bean> -->
29   
30 </beans>

 

二、將pojo類加上注解,及屬性初始化

1.為類加上@Component(" ")注解

2.為部分屬性增加@Autowired注解

3.屬性初始化放到屬性聲明上進行了。

 1 package com.yanyan.pojo;
 2 
 3 import org.springframework.stereotype.Component;
 4 
 5 @Component("c")
 6 public class Category {
 7 
 8     private int id;
 9     private String name="category1";
10     
11     public int getId() {
12         return id;
13     }
14     public void setId(int id) {
15         this.id = id;
16     }
17     
18     public String getName() {
19         return name;
20     }
21     public void setName(String name) {
22         this.name = name;
23     }
24 }

 

 1 package com.yanyan.pojo;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Component;
 5 
 6 @Component("p")
 7 public class Product {
 8 
 9     private int id;
10     private String name="product1";
11     @Autowired
12     private Category category;
13     
14     public int getId() {
15         return id;
16     }
17     public void setId(int id) {
18         this.id = id;
19     }
20     
21     public String getName() {
22         return name;
23     }
24     public void setName(String name) {
25         this.name = name;
26     }
27     
28     public Category getCategory() {
29         return category;
30     }
31     public void setCategory(Category category) {
32         this.category = category;
33     }
34 }

 

 

三、使用測試程序測試結果

 1 package com.yanyan.test;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 import com.how2java.pojo.Category;
 7 import com.how2java.pojo.Product;
 8 
 9 public class TestSpring {
10 
11     public static void main(String[] args) {
12         ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});
13         Product p = (Product)context.getBean("p");
14         System.out.println(p.getName());
15         System.out.println(p.getCategory().getName());
16     }
17 }

運行結果為:

product1

category1


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM