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

SpringDI_constructor注入

时间:2015-11-02 01:35:13      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:

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        xsi:schemaLocation="http://www.springframework.org/schema/beans
 5            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 6         
 7         
 8     <bean id="person" class="com.spring.di.xml.constructor.Person">
 9         <!-- 
10             constructor-arg 指的是构造器中的参数
11             index: 角标  从0开始
12             valkue :如果一般类型,用value赋值
13             ref : 引用类型赋值
14          -->
15         <constructor-arg index="0" value="abcdef"></constructor-arg>
16         
17         <constructor-arg index="1" ref="student"></constructor-arg>
18          
19         
20     </bean>
21     
22     <bean id="student" class="com.spring.di.xml.constructor.Student"></bean>    
23     
24         
25 </beans>

 

Person

 

  1 package com.spring.di.xml.constructor;
  2 
  3 import java.util.List;
  4 import java.util.Map;
  5 import java.util.Properties;
  6 import java.util.Set;
  7 
  8 public class Person {
  9     
 10     private Long pid;
 11     private String name;
 12     private Student student;
 13     private List list;
 14     private Set sets;
 15     private Map map;
 16     private Properties properties;
 17     private Object[] objects;
 18     
 19     
 20 
 21     public Person() {
 22         super();
 23     }
 24 
 25 
 26     public Person(String name) {
 27         super();
 28         this.name = name;
 29     }
 30 
 31 
 32     public Person(String name, Student student) {
 33         super();
 34         this.name = name;
 35         this.student = student;
 36     }
 37     
 38     
 39     public void setPid(Long pid) {
 40         this.pid = pid;
 41     }
 42 
 43 
 44     public void setName(String name) {
 45         this.name = name;
 46     }
 47 
 48 
 49     public void setStudent(Student student) {
 50         this.student = student;
 51     }
 52 
 53 
 54     public void setList(List list) {
 55         this.list = list;
 56     }
 57 
 58 
 59     public void setSets(Set sets) {
 60         this.sets = sets;
 61     }
 62 
 63 
 64     public void setMap(Map map) {
 65         this.map = map;
 66     }
 67 
 68 
 69     public void setProperties(Properties properties) {
 70         this.properties = properties;
 71     }
 72 
 73 
 74     public void setObjects(Object[] objects) {
 75         this.objects = objects;
 76     }
 77 
 78 
 79     public Long getPid() {
 80         return pid;
 81     }
 82     public String getName() {
 83         return name;
 84     }
 85     public Student getStudent() {
 86         return student;
 87     }
 88     public List getList() {
 89         return list;
 90     }
 91     public Set getSets() {
 92         return sets;
 93     }
 94     public Map getMap() {
 95         return map;
 96     }
 97     public Properties getProperties() {
 98         return properties;
 99     }
100     public Object[] getObjects() {
101         return objects;
102     }
103     
104 
105 }

 

 

student

 1 package com.spring.di.xml.constructor;
 2 
 3 public class Student {
 4 
 5     public void say(){
 6         
 7         System.out.println("student");
 8     }
 9     
10 }

 

测试代码

 

 1 package com.spring.di.xml.constructor.test;
 2 
 3 import org.junit.Test;
 4 import org.springframework.context.ApplicationContext;
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 
 7 import com.spring.di.xml.constructor.Person;
 8 
 9 public class DIConstructorTest {
10     
11     @Test
12     public void testDI_XML_constructor(){
13         
14         ApplicationContext context=
15                 new ClassPathXmlApplicationContext("applicationContext.xml");
16         
17         Person person=(Person) context.getBean("person");
18         
19         System.out.println(person.getName());
20         person.getStudent().say();
21         
22         
23         
24     }
25     
26 
27 }

 

 

输出:

abcdef
student

 

 

SpringDI_constructor注入

标签:

原文地址:http://www.cnblogs.com/thinkpad/p/4929058.html

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