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 实例时,传统的方法是由调用者创建被调用者的 ...
随机推荐
- [ActionScript3.0] 使用FileReference处理单个文件的上载
package { import flash.display.SimpleButton; import flash.display.Sprite; import flash.errors.Illega ...
- 4. STL编程四
1. 类模板的默认参数规则: #include <iostream> using namespace std; /* //类模板,不调用不编译(函数):变量还是规范 template< ...
- C语言实现单链表,并完成链表常用API函数
C语言实现单链表,并完成链表常用API函数: 1.链表增.删.改.查. 2.打印链表.反转打印.打印环形链表. 3.链表排序.链表冒泡排序.链表快速排序. 4.求链表节点个数(普通方法.递归方法). ...
- ownCloud问题处理server replied 423 Locked to
打开owncloud 数据库备份:oc_file_locks表(备份免错哦)然后清空该表,客户端同步一次,故障解决 owncloud大的数据无法同步..
- shell ssh远程执行命令
[root@backup shell]# vi backup.sh #!/bin/sh ipAddress=172.17.167.38 ssh -tt root@$ipAddress -p 22 &l ...
- WC2019退役记
sb题不会,暴力写不完,被全场吊着打,AFO
- webService基本概念、元素及简单编码实现
webService "网络服务"(Web Service)的本质,就是通过网络调用其他网站的资源. 网络服务是相对于本地服务来说的,本机完成本机需要完成的任务,叫"本地 ...
- windows系统PHP7开启curl_init
1.php.ini,开启extension=php_curl.dll,去掉去掉前面的“;” 2.检查php.ini的extension_dir值是哪个目录(也就是插件扩展目录,比如php_curl.d ...
- 【文档】五、Mysql Binlog事件结构
这个部分描述了事件被写入binlog或者delay log中的属性.所有的事件有相同的整体结构,也就是包含事件头和事件数据: +===================+ | event header ...
- Cloudera Manager安装之利用parcels方式(在线或离线)安装3或4节点集群(包含最新稳定版本或指定版本的安装)(添加服务)(Ubuntu14.04)(五)
前期博客 Cloudera Manager安装之Cloudera Manager 5.6.X安装(tar方式.rpm方式和yum方式) (Ubuntu14.04) (三) 如果大家,在启动的时候,比如 ...