依赖注入有两种方式 通过 get   set 方法

Person.java

 package cn.itcast.spring.sh.di.set;

 import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set; public class Person {
private Long pid;
private String pname;
private Student student; private Set sets; public Long getPid() {
return pid;
} public void setPid(Long pid) {
this.pid = pid;
} public String getPname() {
return pname;
} public void setPname(String pname) {
this.pname = pname;
} public Student getStudent() {
return student;
} public void setStudent(Student student) {
this.student = student;
} public Set getSets() {
return sets;
} public void setSets(Set sets) {
this.sets = sets;
} public List getLists() {
return lists;
} public void setLists(List lists) {
this.lists = lists;
} public Map getMap() {
return map;
} public void setMap(Map map) {
this.map = map;
} public Properties getProperties() {
return properties;
} public void setProperties(Properties properties) {
this.properties = properties;
} private List lists; private Map map; private Properties properties;
}

Student.java

package cn.itcast.spring.sh.di.set; public class Student { }

applicationContext-di-set.xml中

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!--
把person和student放入到spring容器中
-->
<!--
property是用来描述一个类的属性
基本类型的封装类、String等需要值的类型用value赋值
引用类型用ref赋值
-->
<bean id="person" class="cn.itcast.spring.sh.di.set.Person">
<property name="pid" value="1"></property>
<property name="pname" value="aaa"></property>
<property name="student">
<ref bean="student"/>
</property>
<property name="lists">
<list>
<value>list1</value>
<ref bean="student"/>
<value>list2</value>
</list>
</property>
<property name="sets">
<set>
<value>set1</value>
<ref bean="student"/>
<value>set2</value>
</set>
</property>
<property name="map">
<map>
<entry key="m1">
<value>map1</value>
</entry>
<entry key="m2">
<ref bean="student"/>
</entry>
</map>
</property>
<property name="properties">
<props>
<prop key="prop1">
prop1
</prop>
<prop key="prop2">
prop2
</prop>
</props>
</property>
</bean> <bean id="student" class="cn.itcast.spring.sh.di.set.Student"></bean>
</beans>

还有一种是通过构造函数 注入

Person.java

 package cn.itcast.spring.sh.di.constructor;

 import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set; public class Person {
private Long pid;
private String pname;
private Student student; private Set sets; public Person(String pname,Student student){
this.pname = pname;
this.student = student;
} public Person(Long pid,String pname,Student student){
this.pid = pid;
this.pname = pname;
this.student = student;
} public Person(){} public Long getPid() {
return pid;
} public void setPid(Long pid) {
this.pid = pid;
} public String getPname() {
return pname;
} public void setPname(String pname) {
this.pname = pname;
} public Student getStudent() {
return student;
} public void setStudent(Student student) {
this.student = student;
} public Set getSets() {
return sets;
} public void setSets(Set sets) {
this.sets = sets;
} public List getLists() {
return lists;
} public void setLists(List lists) {
this.lists = lists;
} public Map getMap() {
return map;
} public void setMap(Map map) {
this.map = map;
} public Properties getProperties() {
return properties;
} public void setProperties(Properties properties) {
this.properties = properties;
} private List lists; private Map map; private Properties properties;
}

Student.java

 package cn.itcast.spring.sh.di.constructor;

 public class Student {
public void student(){
System.out.println("student");
}
}

applicationContext-di-constructor.xml

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- -->
<bean id="person_Con" class="cn.itcast.spring.sh.di.constructor.Person">
<constructor-arg index="0" type="java.lang.Long" value="1"></constructor-arg>
<constructor-arg index="1" value="aaa"></constructor-arg>
<constructor-arg index="2" ref="student_Con"></constructor-arg>
</bean> <bean id="student_Con" class="cn.itcast.spring.sh.di.constructor.Student"></bean>
</beans>

测试

 package cn.itcast.spring.sh.test;

 import org.junit.Test;

 import cn.itcast.spring.sh.di.constructor.Person;
import cn.itcast.spring.sh.utils.SpringInit; public class DIConTest extends SpringInit{
@Test
public void testSet(){
Person person = (Person)context.getBean("person_Con");
System.out.println(person.getPname());
person.getStudent().student();
}
}

spring Di依赖注入的更多相关文章

  1. 手写Spring DI依赖注入,嘿,你的益达!

    目录 提前实例化单例Bean DI分析 DI的实现 构造参数依赖 一:定义分析 二:定义一个类BeanReference 三:BeanDefinition接口及其实现类 四:DefaultBeanFa ...

  2. Spring DI - 依赖注入

    1.IOC(DI) - 控制反转(依赖注入) 所谓的IOC称之为控制反转,简单来说就是将对象的创建的权利及对象的生命周期的管理过程交由Spring框架来处理,从此在开发过程中不再需要关注对象的创建和生 ...

  3. 初识Spring框架实现IOC和DI(依赖注入)

    学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的, IoC是 ...

  4. Spring的依赖注入(DI)三种方式

    Spring依赖注入(DI)的三种方式,分别为: 1.  接口注入 2.  Setter方法注入 3.  构造方法注入 下面介绍一下这三种依赖注入在Spring中是怎么样实现的. 首先我们需要以下几个 ...

  5. Spring详解(三)------DI依赖注入

    上一篇博客我们主要讲解了IOC控制反转,也就是说IOC 让程序员不在关注怎么去创建对象,而是关注与对象创建之后的操作,把对象的创建.初始化.销毁等工作交给spring容器来做.那么创建对象的时候,有可 ...

  6. Spring:(二)DI依赖注入方式

    DI 依赖注入 DI(Dependency Injection)依赖注入,说简单一点就将类里面的属性在创建类的过程中给属性赋值,即将对象依赖属性(简单值,集合,对象)通过配置设值给该对象. 属性注入的 ...

  7. 一) Spring 介绍、IOC控制反转思想与DI依赖注入

    一.spring介绍1.IOC反转控制思想(Inversion of Control)与DI依赖注入(Dependency Injection)2.AOP面向切面的编程思想与动态代理3.作用:项目的粘 ...

  8. 【Spring】Spring之依赖注入(DI)传递参数的方式

    DI依赖注入传入参数的方式,这里介绍了基本数据类型,集合,符合数据类型的传递(String类型比较特殊,其传递值和基本数据类型的方法一致) 注入基本数据类型和String类型 通过setter方法注入 ...

  9. Spring-初识Spring框架-IOC控制反转(DI依赖注入)

    ---恢复内容开始--- IOC :控制反转 (DI:依赖注入)使用ioc模式开发 实体类必须有无参构造方法1.搭建Spring环境下载jarhttp://maven.springframework. ...

随机推荐

  1. [BZOJ2726][SDOI2012]任务安排(DP+凸壳二分)

    2726: [SDOI2012]任务安排 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1580  Solved: 466[Submit][Statu ...

  2. 【BFS】Pots

    [poj3414]Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16925   Accepted: 7168   ...

  3. BZOJ 3632 外太空旅行(最大团)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3632 [题目大意] 求图中的最大团. [题解] 最大团问题是npc问题,因此可以考虑随 ...

  4. 【找规律】【递推】【二项式定理】Codeforces Round #419 (Div. 1) B. Karen and Test

    打个表出来看看,其实很明显. 推荐打这俩组 11 1 10 100 1000 10000 100000 1000000 10000000 100000000 1000000000 1000000000 ...

  5. 【线段树(单点修改,区间求和)】HDU1166 - 敌军布阵

    hdu1166 敌兵布阵,单点修改,区间求和. [ATTENTION]MAXN要开成节点数的4倍,开得不够会提示TLE. #include<iostream> #include<cs ...

  6. bzoj 3900: 交换茸角

    3900: 交换茸角 Description 动物园里有 n 头麋鹿.每头麋鹿有两支茸角,每支茸角有一个重量.然而,一旦某头麋鹿上 两支茸角的重量之差过大,这头麋鹿就会失去平衡摔倒.为了不然这种悲剧发 ...

  7. 找回VisualStudio异常设置中丢失的“用户未处理的(User-unhandled)”列

    今天发现我的VisualStudio中的异常设置中"用户未处理的"列丢失了 虽然我很少设置这一项,但没了还是觉得怪怪的,网上搜了一下,在文章"USER-UNHANDLED ...

  8. linux Socket send与recv函数详解

    转自:http://www.cnblogs.com/blankqdb/archive/2012/08/30/2663859.html linux send与recv函数详解   1 #include ...

  9. [转]解决Eclipse更新ADT插件时遇到的Eclipse reports rendering library more recent than ADT plug-in问题

    使用 SDK Manager 工具更新下载新版本后,无法显示可视化布局,同时提示 This version of the rendering library is more recent than y ...

  10. C++11常用特性的使用经验总结(转载)

    C++11已经出来很久了,网上也早有很多优秀的C++11新特性的总结文章,在编写本博客之前,博主在工作和学习中学到的关于C++11方面的知识,也得益于很多其他网友的总结.本博客文章是在学习的基础上,加 ...