spring di,即依赖注入,从应用的浅显意义来讲就是对属性赋值

1.用setter赋值,在spring的applicationContext.xml配置文件的bean下的property标签

属性name指定属性名,属性value指定值,一般用于基本数据 类型的包装类型

属性ref指定值,一般用于引用类型,还有list标签,下面value标签,

set标签下面value标签,map标签下面entry,下面分别有key,value,

还有props,下面prop,key

public class Student {
public void say(){
System.out.println("student");
}
}
public class Person {
private Long pid;//包装类型
private String pname;//String类型
private Student student;//引用类型 private List lists; 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 List getLists() {
return lists;
} public void setLists(List lists) {
this.lists = lists;
} public Set getSets() {
return sets;
} public void setSets(Set sets) {
this.sets = sets;
} 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 Map map; private Properties properties;
}

  

<?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" class="cn.di.xml.set.Person">
<!--
property就是代表属性
在spring中基本类型(包装类型和String)都可以用value来赋值
引用类型用ref赋值
-->
<property name="pid" value="5"></property>
<property name="pname" value="王五"></property>
<property name="student">
<ref bean="student"/>
</property>
<property name="lists">
<list>
<value>list1</value>
<value>list2</value>
<ref bean="student"/>
</list>
</property>
<property name="sets">
<set>
<value>set1</value>
<value>set2</value>
<ref bean="student"/>
</set>
</property>
<property name="map">
<map>
<entry key="map1">
<value>map1</value>
</entry>
<entry key="map2">
<value>map2</value>
</entry>
<entry key="map3">
<ref bean="student"/>
</entry>
</map>
</property>
<property name="properties">
<props>
<prop key="prop1">
prop1
</prop>
</props>
</property>
</bean>
<bean id="student" class="cn.di.xml.set.Student"></bean>
</beans>

  

@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = (Person)context.getBean("person");
person.getStudent().say();
System.out.println(person.getPid());
System.out.println(person.getPname());
List lists = person.getLists();
for(int i=0;i<lists.size();i++){
System.out.println(lists.get(i).toString());
}
}

  

2.利用构造函数赋值

在bean下有constructor-arg标签,里面有index,type,ref,value属性

public class Person {
private Long pid;
public Long getPid() {
return pid;
} public String getPname() {
return pname;
} public Student getStudent() {
return student;
} private String pname;
private Student student; public Person(Long pid,String pname){
this.pid = pid;
this.pname = pname;
} public Person(String pname,Student student){
this.pname = pname;
this.student = student;
}
}

  

<?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" class="cn.di.xml.constructor.Person">
<!--
构造函数的参数
index 第几个参数,下标从0开始
type 参数的类型
ref 如果类型是引用类型,赋值
value 如果类型是基本类型,赋值
说明:
只能指定一个构造函数
-->
<constructor-arg index="0" type="java.lang.String" value="王五"></constructor-arg>
<constructor-arg index="1" ref="student"></constructor-arg>
</bean> <bean id="student" class="cn.di.xml.constructor.Student"></bean>
</beans>

  

4.spring di的更多相关文章

  1. Spring DI

    一.   Spring DI 依赖注入 利用spring IOC实例化了对象,而DI将实例化的对象注入到需要对象的地方,完成初始化任务. 对象由spring创建,之后再由spring给属性赋值 spr ...

  2. Spring DI使用详解

    Spring DI使用详解 一.介绍 DI的定义:依赖注入,为类里面的属性设值.例如,我们之前的setName方法就是在为name属性设值. IOC与DI的关系:IOC进行对象的创建,DI进行值的注入 ...

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

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

  4. Java Spring DI之旅

    做过.NET的人很多都用过Microsoft Enterprise Library,里面有一个Dependency injection工具Unity,我们可以使用它来实现依赖注入:什么是依赖注入呢?我 ...

  5. Spring DI模式 小样例

           今儿跟同事讨论起来spring早期的,通过大篇幅xml的配置演变到今天annotation的过程,然后随手写了个小样例,感觉还不错,贴到这里留个纪念. 样例就是用JAVA API的方式, ...

  6. Spring DI - 依赖注入

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

  7. spring Di依赖注入

    依赖注入有两种方式 通过 get   set 方法 Person.java package cn.itcast.spring.sh.di.set; import java.util.List; imp ...

  8. Spring知识点总结(三)之Spring DI

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

  9. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring DI(依赖注入)的实现方式属性注入和构造注入

    依赖注入(Dependency Injection,DI)和控制反转含义相同,它们是从两个角度描述的同一个概念. 当某个 Java 实例需要另一个 Java 实例时,传统的方法是由调用者创建被调用者的 ...

随机推荐

  1. php对ip地址的处理

    public function actions() { $url = "http://ip.taobao.com/service/getIpInfo.php?ip=".self:: ...

  2. ElasticSearch5.0——IK词库加载

    Dictionary ConfigurationIKAnalyzer.cfg.xml can be located at {conf}/analysis-ik/config/IKAnalyzer.cf ...

  3. itemgetter和groupby

    一. itemgetter的使用方法 itemgetter()返回一个指定列表下标或者字典键的函数,通过这个返回的函数作用到对象上,获得才能取得相应的值 1. 排序字典列表的一个例子 from ope ...

  4. 在eclipse中,用maven创建web项目

    备注:该文档是之前学习时,根据网上其他童鞋的经验自己测试后梳理,如有侵权,请勿怪,感谢! 1.在eclipse中用maven创建项目,右键new>>Maven Project 2.点击ne ...

  5. SaltStack Pillar 详解

    简介 grains用于存储静态不易变更的数据,而pillar一般用于存储动态, 敏感的数据,通过minion和master设置或获取grains信息,而pillar信息只能在master端配置,在到m ...

  6. 【Quartz】工作原理

    本文参考至http://www.cnblogs.com/davidwang456/p/4205237.html和https://blog.csdn.net/guolong1983811/article ...

  7. 关于Nginx启动成功,浏览器不能访问的解决办法

    本人初学Nginx,第一天配置成功并能通过浏览器进行访问. 第二天重新打开,将Nginx启动,但是浏览器却访问不了. 执行 ps aux|grep nginx ,执行结果如下,的确Nginx服务已经启 ...

  8. 解决FTPClient linux环境下FTPClient调用retrieveFileStream导致线程挂起(防火墙问题);下载文件小于实际文件问题

    FTPClient调用retrieveFileStream导致线程挂起(防火墙问题):下载文件小于实际文件问题解决 实际是因为FTP的两种传输模式:主动模式和被动模式的不同而导致的 FTPClient ...

  9. [Xamarin.Android]使用SqliteNET (转帖)

    Xamarin除了提供ADO.NET方式操作Sqlite外, 也提供了一個類似Entity Framework的SqliteNET, 可至官網提供的連結下載Source, 或點選這裡下載. 以下範例使 ...

  10. (转).NET技术大系概览 (迄今为止最全的.NET技术栈)

    前言 .Net推出13年了,Visual Studio 2015 / .NET Framework 4.6昨天也发布了. 从2002年的.NET 1.0开始,1.1,2.x,3.x,4.x,每个新版本 ...