spring Di依赖注入
依赖注入有两种方式 通过 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依赖注入的更多相关文章
- 手写Spring DI依赖注入,嘿,你的益达!
目录 提前实例化单例Bean DI分析 DI的实现 构造参数依赖 一:定义分析 二:定义一个类BeanReference 三:BeanDefinition接口及其实现类 四:DefaultBeanFa ...
- Spring DI - 依赖注入
1.IOC(DI) - 控制反转(依赖注入) 所谓的IOC称之为控制反转,简单来说就是将对象的创建的权利及对象的生命周期的管理过程交由Spring框架来处理,从此在开发过程中不再需要关注对象的创建和生 ...
- 初识Spring框架实现IOC和DI(依赖注入)
学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的, IoC是 ...
- Spring的依赖注入(DI)三种方式
Spring依赖注入(DI)的三种方式,分别为: 1. 接口注入 2. Setter方法注入 3. 构造方法注入 下面介绍一下这三种依赖注入在Spring中是怎么样实现的. 首先我们需要以下几个 ...
- Spring详解(三)------DI依赖注入
上一篇博客我们主要讲解了IOC控制反转,也就是说IOC 让程序员不在关注怎么去创建对象,而是关注与对象创建之后的操作,把对象的创建.初始化.销毁等工作交给spring容器来做.那么创建对象的时候,有可 ...
- Spring:(二)DI依赖注入方式
DI 依赖注入 DI(Dependency Injection)依赖注入,说简单一点就将类里面的属性在创建类的过程中给属性赋值,即将对象依赖属性(简单值,集合,对象)通过配置设值给该对象. 属性注入的 ...
- 一) Spring 介绍、IOC控制反转思想与DI依赖注入
一.spring介绍1.IOC反转控制思想(Inversion of Control)与DI依赖注入(Dependency Injection)2.AOP面向切面的编程思想与动态代理3.作用:项目的粘 ...
- 【Spring】Spring之依赖注入(DI)传递参数的方式
DI依赖注入传入参数的方式,这里介绍了基本数据类型,集合,符合数据类型的传递(String类型比较特殊,其传递值和基本数据类型的方法一致) 注入基本数据类型和String类型 通过setter方法注入 ...
- Spring-初识Spring框架-IOC控制反转(DI依赖注入)
---恢复内容开始--- IOC :控制反转 (DI:依赖注入)使用ioc模式开发 实体类必须有无参构造方法1.搭建Spring环境下载jarhttp://maven.springframework. ...
随机推荐
- AtCoder - 4162 Independence
Problem Statement In the State of Takahashi in AtCoderian Federation, there are N cities, numbered 1 ...
- BZOJ 1430 小猴打架(prufer编码)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1430 [题目大意] 一开始森林里面有N只互不相识的小猴子,它们经常打架, 但打架的双方 ...
- BZOJ 1098 [POI2007]办公楼biu(反向图bfs+并查集优化)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1098 [题目大意] 现在有一张图,要求将这张图的点划分为尽量多的分组,对于不同分组的两 ...
- [ARC097F]Monochrome Cat
题意:一棵树,每个节点是黑色或白色,你可以从任意节点开始进行一些操作并在任意节点结束,如果当前在$x$,那么一次操作可以是:1.走到相邻节点$y$并翻转$y$的颜色,2.翻转$x$的颜色,问把所有节点 ...
- 【高斯消元】CDOJ1783 曜酱的线性代数课堂(一)
高斯消元求逆矩阵板子. #include<cstdio> #include<cmath> #include<algorithm> #include<cstri ...
- 【动态规划】 Codeforces Round #416 (Div. 2) C. Vladik and Memorable Trip
划分那个序列,没必要完全覆盖原序列.对于划分出来的每个序列,对于某个值v,要么全都在该序列,要么全都不在该序列. 一个序列的价值是所有不同的值的异或和.整个的价值是所有划分出来的序列的价值之和. ...
- bzoj 4506: [Usaco2016 Jan]Fort Moo
4506: [Usaco2016 Jan]Fort Moo Description Bessie is building a fort with her friend Elsie. Like any ...
- Problem B: 年龄问题
#include<stdio.h> int xx(int n,int m,int k) { int a; ) { a=k; } else { a=xx(n-,m,k)+m; } retur ...
- Problem F: 调用函数,判断各位数字立方和是否等于它本身
#include<stdio.h> #include<math.h> int is(int number)//定义函数 { ; ) { s=number%; sum=sum+p ...
- Codeforces Round #344 (Div. 2) B. Print Check 水题
B. Print Check 题目连接: http://www.codeforces.com/contest/631/problem/B Description Kris works in a lar ...