4.spring di
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的更多相关文章
- Spring DI
一. Spring DI 依赖注入 利用spring IOC实例化了对象,而DI将实例化的对象注入到需要对象的地方,完成初始化任务. 对象由spring创建,之后再由spring给属性赋值 spr ...
- Spring DI使用详解
Spring DI使用详解 一.介绍 DI的定义:依赖注入,为类里面的属性设值.例如,我们之前的setName方法就是在为name属性设值. IOC与DI的关系:IOC进行对象的创建,DI进行值的注入 ...
- 手写Spring DI依赖注入,嘿,你的益达!
目录 提前实例化单例Bean DI分析 DI的实现 构造参数依赖 一:定义分析 二:定义一个类BeanReference 三:BeanDefinition接口及其实现类 四:DefaultBeanFa ...
- Java Spring DI之旅
做过.NET的人很多都用过Microsoft Enterprise Library,里面有一个Dependency injection工具Unity,我们可以使用它来实现依赖注入:什么是依赖注入呢?我 ...
- Spring DI模式 小样例
今儿跟同事讨论起来spring早期的,通过大篇幅xml的配置演变到今天annotation的过程,然后随手写了个小样例,感觉还不错,贴到这里留个纪念. 样例就是用JAVA API的方式, ...
- Spring DI - 依赖注入
1.IOC(DI) - 控制反转(依赖注入) 所谓的IOC称之为控制反转,简单来说就是将对象的创建的权利及对象的生命周期的管理过程交由Spring框架来处理,从此在开发过程中不再需要关注对象的创建和生 ...
- spring Di依赖注入
依赖注入有两种方式 通过 get set 方法 Person.java package cn.itcast.spring.sh.di.set; import java.util.List; imp ...
- Spring知识点总结(三)之Spring DI
1. IOC(DI) - 控制反转(依赖注入) 所谓的IOC称之为控制反转,简单来说就是将对象的创建的权利及对象的生命周期的管理过程交由Spring框架来处理,从此在开发过程中不再需要关注对象的创建和 ...
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring DI(依赖注入)的实现方式属性注入和构造注入
依赖注入(Dependency Injection,DI)和控制反转含义相同,它们是从两个角度描述的同一个概念. 当某个 Java 实例需要另一个 Java 实例时,传统的方法是由调用者创建被调用者的 ...
随机推荐
- HBase二级索引的设计
摘要 最近做的一个项目涉及到了多条件的组合查询,数据存储用的是HBase,恰恰HBase对于这种场景的查询特别不给力,一般HBase的查询都是通过RowKey(要把多条件组合查询的字段都拼接在RowK ...
- [JS] js 判断用户是否在浏览当前页面
var hiddenProperty = 'hidden' in document ? 'hidden' : 'webkitHidden' in document ? 'webkitHidden' : ...
- Oracle数据库PL/SQL那点事情---修改过电脑的用户名
在安装Oracle数据库的PL/SQL工具时候,电脑名称是重装系统后自动生成的用户名名称,作为程序员,有很强的强迫症,就想利用自己的英文名称作为自己电脑的名称,所以就修改了电脑的名称:结果PL/SQL ...
- Mutation Observer
MutationEvent Mutation Observer 变动观察器, 等待所有脚本任务完成后,才会运行(即异步触发方式) 把DOM变动记录封装成一个数组进行处理,而不是一条条个别处理DOM变动 ...
- 我把阿里云centos gcc从4.4.7升级到4.8.2的经历
我有试着去手动编译安装gcc,可是make的速度实在太慢,最后还直接失败了. 最后在csdn找到了个博客,说是使用yum来安装,网址为: http://blog.csdn.net/ppdouble/a ...
- re 模块 常规方法使用
前情提要: re模块主要用于正则,用的好了秒杀一切匹配的规则,这里主要是介绍基本用法 一:元字符 1:\w 匹配字符,包含中文,数字或下划线 l ='早乙女露依 123 是我的 321 心目中的 22 ...
- bzoj4998: 星球联盟(link-cut-tree)
题面 bzoj 题解 bzoj2959: 长跑的弱化版 产生了环就并查集维护一下 Code #include<bits/stdc++.h> #define LL long long #de ...
- win10 压缩包安装mysql8.0.11报错:Access denied for user 'root'@'localhost'
按这篇:https://blog.csdn.net/Myuhua/article/details/84792121#commentsedit 这里精简下,还有update语句中authenticati ...
- JS框架设计之对象扩展一种子模块
对象扩展 说完了,对象的创建(框架的命名空间的创建)以及如何解决多库之间的命名空间冲突问题之后,接下来,就是要扩展我们的对象,来对框架进行扩展,我们需要一种新功能,将新添加的功能整合到我们定义的对象中 ...
- Fedora 24 python3.5 安装M2Crypto
安装M2Crypto#python3 -m pip install M2Crypto 出现错误 gcc: /usr/lib/rpm/redhat/redhat-hardened-cc1:Nosuch ...