spring属性注入
1,set方法注入
(1)对于值类型的属性:
在对象中一定要有set方法
package com.songyan.demo1; import com.songyan.injection.Car; /**
* 要创建的对象类
* @author sy
*
*/
public class User {
private String name;
private int age ;/**
* 重写toString方法在输出对象时将属性值输出
*/
@Override
public String toString() { return "name:"+this.name+",age:"+this.age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
} }
在配置文件中进行配置
<!--在user对象中为属性名为name的属性赋值为“tom” -->
<property name="name" value="tom"></property>
<property name="age" value="18"></property>
</bean>
测试
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("com/songyan/injection/beans5.xml");
User user=(User)applicationContext.getBean("user");
System.out.println(user);
(2)对于引用类型的属性
package com.songyan.injection; public class Car {
private String name;
private String color;
@Override
public String toString() { return "【carname:"+this.name+",carcolor:"+this.color+"】";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
} }
package com.songyan.demo1; import com.songyan.injection.Car; /**
* 要创建的对象类
* @author sy
*
*/
public class User {
private String name;
private int age ;
private Car car; public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
/**
* 重写toString方法在输出对象时将属性值输出
*/
@Override
public String toString() { return "name:"+this.name+",age:"+this.age+",car:"+this.car;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
} }
package com.songyan.injection; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.songyan.demo1.User; public class Client {
public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("com/songyan/injection/beans5.xml");
User user=(User)applicationContext.getBean("user");
System.out.println(user);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
<!-- 引用类型的属性注入需要先将属性配置 -->
<bean name="car" class="com.songyan.injection.Car">
<property name="name" value="宝马"></property>
<property name="color" value="red"></property>
</bean>
<!--set 方法注入值类型 -->
<bean name="user" class="com.songyan.demo1.User">
<!--在user对象中为属性名为name的属性赋值为“tom” -->
<property name="name" value="tom"></property>
<property name="age" value="18"></property>
<!--引用类型的赋值使用ref属性 -->
<property name="car" ref="car"></property>
</bean> </beans>
2,构造方法注入
package com.songyan.injection; public class Car {
private String name;
private String color; // 通过构造函数对属性赋值
public Car(String name, String color) {
super();
this.name = name;
this.color = color;
} public Car() { } @Override
public String toString() { return "【carname:" + this.name + ",carcolor:" + this.color + "】";
} }
package com.songyan.demo1; import com.songyan.injection.Car; /**
* 要创建的对象类
* @author sy
*
*/
public class User {
private String name;
private int age ;
private Car car; /**
* 通过构造函数对属性赋值
* @param name
* @param age
* @param car
*/
public User(String name, int age, Car car) {
super();
this.name = name;
this.age = age;
this.car = car;
}
public User()
{ } /**
* 重写toString方法在输出对象时将属性值输出
*/
@Override
public String toString() { return "name:"+this.name+",age:"+this.age+",car:"+this.car;
} }
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
<!-- 引用类型的属性注入需要先将属性配置 -->
<bean name="car" class="com.songyan.injection.Car">
<constructor-arg name="name" value="baoma"></constructor-arg>
<constructor-arg name="color" value="red"></constructor-arg>
</bean>
<!--构造方法注入值类型 -->
<bean name="user" class="com.songyan.demo1.User">
<!--在user对象中为属性名为name的属性赋值为“tom” -->
<constructor-arg name="name" value="tom"></constructor-arg>
<constructor-arg name="age" value="18"></constructor-arg>
<constructor-arg name="car" ref="car"></constructor-arg> </bean> </beans>
package com.songyan.injection; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.songyan.demo1.User; public class Client {
public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("com/songyan/injection/beans5.xml");
User user=(User)applicationContext.getBean("user");
System.out.println(user);
}
}
3,p名称空间注入
4,spel注入
5,复杂类型注入
package com.songyan.injection; public class Car {
private String name;
private String color; // 通过构造函数对属性赋值
public Car(String name, String color) {
super();
this.name = name;
this.color = color;
} public Car() { } @Override
public String toString() { return "【carname:" + this.name + ",carcolor:" + this.color + "】";
} }
package com.songyan.complex; import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties; public class Complex {
private Object[] obj;//数组类型注入
private List list;//list类型注入
private Map map;//map类型注入
private Properties properties;//文件类型注入 @Override
public String toString() { return "array:"+Arrays.toString(obj)+" \nlist:"+this.list+" \nproperties:"+this.properties+"\nmap:"+this.map;
}
public Object[] getObj() {
return obj;
}
public void setObj(Object[] obj) {
this.obj = obj;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
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;
} }
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
<!-- 引用类型的属性注入需要先将属性配置 -->
<bean name="car" class="com.songyan.injection.Car">
<constructor-arg name="name" value="baoma"></constructor-arg>
<constructor-arg name="color" value="red"></constructor-arg>
</bean> <bean name="complex" class="com.songyan.complex.Complex">
<!--数组类型-->
<!--数组中只有一个值类型 -->
<!-- <property name="obj" value="tom"></property> -->
<!--数组中只有一个引用类型 -->
<!-- <property name="obj" ref="car"></property> -->
<!--数组中有多个值 -->
<property name="obj" >
<array>
<value>tom</value>
<value>jack</value>
<ref bean="car" />
</array>
</property>
<!--map类型 -->
<property name="map">
<map>
<entry key="name" value="zhangsan"></entry>
<entry key="car" value-ref="car"></entry>
</map>
</property>
<!--list类型 -->
<!--一个值 -->
<!-- <property name="list" value="jack"></property> -->
<!--多个值 -->
<property name="list">
<list>
<value>jack1</value>
<value>jack2</value>
<ref bean="car"/>
</list>
</property> <!--properties -->
<property name="properties">
<props>
<prop key="name">user</prop>
<prop key="pass">123</prop>
<prop key="id">192.168.1.1</prop>
</props>
</property>
</bean> </beans>
package com.songyan.complex; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.songyan.demo1.User; public class Cliect {
public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("com/songyan/complex/beans.xml");
Complex complex=(Complex)applicationContext.getBean("complex");
System.out.println(complex);
}
}
spring属性注入的更多相关文章
- Spring 属性注入(一)JavaBean 内省机制在 BeanWrapper 中的应用
Spring 属性注入(一)JavaBean 内省机制在 BeanWrapper 中的应用 Spring 系列目录(https://www.cnblogs.com/binarylei/p/101174 ...
- Spring 属性注入(二)BeanWrapper 结构
Spring 属性注入(二)BeanWrapper 结构 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html) BeanWrap ...
- Spring 属性注入(三)AbstractNestablePropertyAccessor
Spring 属性注入(三)AbstractNestablePropertyAccessor Spring 系列目录(https://www.cnblogs.com/binarylei/p/10117 ...
- Spring 属性注入(四)属性键值对 - PropertyValue
Spring 属性注入(四)属性键值对 - PropertyValue Spring 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html) P ...
- spring 属性注入
Spring的核心技术室依赖注入,下面是依赖注入之属性注入的实现过程,牛刀小试,请看效果. 1.首先添加Spring.Web引用.本例中是使用分层思想来演示的,下面是项目的结构和UserModel类的 ...
- Spring属性注入、构造方法注入、工厂注入以及注入参数(转)
Spring 是一个开源框架. Spring 为简化企业级应用开发而生(对比EJB2.0来说). 使用 Spring 可以使简单的 JavaBean 实现以前只有 EJB 才能实现的功能.Spring ...
- 六 Spring属性注入的四种方式:set方法、构造方法、P名称空间、SPEL表达式
Spring的属性注入: 构造方法的属性注入 set方法的属性注入
- java spring属性注入
一.创建对象时候,向类里面属性设置值:一般有三个方式 1) .有参构造, 2). set**** 3).接口注入 二. 在spring框架里面,支持前面两种方式: 1).有参构造方法 用constr ...
- spring属性注入DI
spring setter方式注入: 注入对象属性: 前提: 在bean对应实体中有对应的setter方法. 基础代码: 在bean中有另一个bean属性的setter方法. package cn.i ...
随机推荐
- Mysql 查询—按位运算
前言:虽说这是件小事儿,但本宝宝思前想后,还是为它留下一笔,嘿嘿.反正写博客不浪费纸和笔!好久没有开启我的逗比模式了,我亲爱的乖徒弟DBA,DBB,DBAA等,好久不见你们,遥祝幸福快乐+DB. 整个 ...
- typescript语言
百度百科:2013年6月19日,在经历了一个预览版之后微软正式发布了正式版TypeScript 0.9
- ftrace 简介
ftrace 简介 ftrace 的作用是帮助开发人员了解 Linux 内核的运行时行为,以便进行故障调试或性能分析. 最早 ftrace 是一个 function tracer,仅能够记录内核的函数 ...
- P4342 [IOI1998]Polygon
题意翻译 题目可能有些许修改,但大意一致 多边形是一个玩家在一个有n个顶点的多边形上的游戏,如图所示,其中n=4.每个顶点用整数标记,每个边用符号+(加)或符号*(乘积)标记. 第一步,删除其中一条边 ...
- 牛客 NOIp模拟1 T1 中位数 解题报告
中位数 题目描述 小\(N\)得到了一个非常神奇的序列\(A\).这个序列长度为\(N\),下标从\(1\)开始.\(A\)的一个子区间对应一个序列,可以由数对\([l,r]\)表示,代表\(A[l] ...
- java.net.BindException: Address already in use: JVM_Bind <null>:8080错误
今天打开myeclipse出现java.net.BindException: Address already in use: JVM_Bind <null>:8080错误 从网上搜了一下大 ...
- spring in action 学习笔记十:用@PropertySource避免注入外部属性的值硬代码化
@PropertySource的写法为:@PropertySource("classpath:某个.properties文件的类路径") 首先来看一下这个案例的目录结构,重点看带红 ...
- Iterator pattern 及其在java API中的运用
1.问题: 当我们看到java中的Collection,List,Set,Map等集合类时都可以用Iterator进行遍历元素时,我们是否感到很神奇.我们不禁要问java是如何实现这一目标的.这就是我 ...
- 口红游戏 插口红游戏 h5页面开发
目前火热的口红机游戏,需要在设备前参与,然后成功后即可赢得口红,作为平台运营者来说还是比较重资产的,目前我们将它搬到了线上.每个人都可以远程玩这样的口红机游戏了.直接在手机微信里试玩,成功后,后台即可 ...
- Java中同一个类中不同的synchronized方法是否可以并发执行?
答案是: 不可以,因为都是获取到对象本身的锁. 多个线程访问同一个类的synchronized方法时, 都是串行执行的 ! 就算有多个cpu也不例外 ! synchronized方法使用了类java的 ...