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 实例时,传统的方法是由调用者创建被调用者的 ...
随机推荐
- jmeter进行https协议的测试
一.HTTPS和HTTP的区别 超文本传输协议HTTP协议被用于在Web浏览器和网站服务器之间传递信息.HTTP协议以明文方式发送内容,不提供任何方式的数据加密,如果攻击者截取了Web浏览器和 ...
- Linux中一些 不是很常用的配置修改
1,让虚拟机屏幕最大化 :查看-->自动调整大小-->自动适应客户机 2,让虚拟机取消屏保: system --> preferences --> Screensaver
- [Flex] 组件Tree系列 —— 运用variableRowHeight和wordWrap设置可变行高
mxml: <?xml version="1.0" encoding="utf-8"?> <!--功能描述:运用variableRowHeig ...
- clang命令理解程序
Xcode 创建一个mac OS command Line Tool程序 步骤打开终端 cd + 工程路径(绝对路径)(注:拖拽main.m文件到终端) input —preprocessor— ...
- axios跨域问题
最近遇到一个很奇怪的问题,在帮助测试妹子做一个小项目的时候,遇到了一个很棘手的问题,axios请求的时候报404,请求type是options,我当时的第一反应就是跨域问题,果然在console里面还 ...
- ubuntu配置实验
实验:ubuntu配置 需求: caterpillar公司管理员小李需要将公司系统由windows全部更换为ubuntu,并制定SOP(操作指导书) 环境:vmware workstation 1 ...
- Vue 父子组件传递方式
问题: parent.vue <template> <div> 父组件 <child :childObject="asyncObject">&l ...
- QuantLib 金融计算——随机过程之一般 Black Scholes 过程
目录 QuantLib 金融计算--随机过程之一般 Black Scholes 过程 一般 Black Scholes 过程 如果未做特别说明,文中的程序都是 Python3 代码. QuantLib ...
- node.js常用命令
安装node 验证是否安装node $node -v $npm -v npm node package manager , Node 的包管理器 安装 包 # 安装到当前目录 $ npm instal ...
- Linux RPM和YUM
rpm包的管理:sealed 介绍: 一种用于互联网下载包的打包及安装工具,它包含在某些Linux分发版中.它生成具有.RPM扩展名的文件. RPM是RedHat Package Manager(Re ...