码迷,mamicode.com
首页 > 编程语言 > 详细

Spring注入对象(3)

时间:2019-03-08 11:12:23      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:getter   sys   beans   setname   mamicode   info   col   block   演示   

2019-03-08/10:45:04

演示:对Product对象,注入一个Category对象

1.创建pojo类 Product类中有对Category对象的setter getter

 1 package pojo;
 2  
 3 public class Product {
 4  
 5     private int id;
 6     private String name;
 7     private Category category;   //引入Category对象同时添加get和set
 8     public int getId() {
 9         return id;
10     }
11     public void setId(int id) {
12         this.id = id;
13     }
14     public String getName() {
15         return name;
16     }
17     public void setName(String name) {
18         this.name = name;
19     }
20     public Category getCategory() {
21         return category;
22     }
23     public void setCategory(Category category) {
24         this.category = category;
25     }
26 }

 

2.配置文件applicationContext.xml的修改     在创建Product的时候注入一个Category对象,这里要使用ref来注入另一个对象

 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" xmlns:aop="http://www.springframework.org/schema/aop"
 4     xmlns:tx="http://www.springframework.org/schema/tx" 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-3.0.xsd
 8    http://www.springframework.org/schema/aop
 9    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
10    http://www.springframework.org/schema/tx
11    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
12    http://www.springframework.org/schema/context     
13    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
14  
15     <bean name="c" class="pojo.Category">
16         <property name="name" value="category 1" />
17     </bean>
18     <bean name="p" class="pojo.Product">
19         <property name="name" value="product1" />
20         <property name="category" ref="c" />
21     </bean>
22  
23 </beans>

 

3.TestSpring

通过Spring拿到的Product对象已经被注入了Category对象了

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

 

4.测试结果

技术图片

 

Spring注入对象(3)

标签:getter   sys   beans   setname   mamicode   info   col   block   演示   

原文地址:https://www.cnblogs.com/bencoper/p/10494474.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!