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

猜你喜欢

转载自www.cnblogs.com/Mmumu/p/8985781.html