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 ...
随机推荐
- winform-实现类似QQ停靠桌面上边缘隐藏的效果
//实现类似QQ停靠桌面上边缘隐藏的效果! private void timer1_Tick(object sender, EventArgs e) { System.Drawing.Point pp ...
- Problem 1036 四塔问题
Accept: 590 Submit: 1506Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description “汉诺 ...
- 第十章 用户数据报协议和IP分片
用户数据报协议和IP分片 UDP是一种保留消息边界的简单的面向数据报的传输层协议.它仅提供差错检测.只是检测,而不是纠正,它只是把应用程序传给IP层的数据发送出去,但是并不会保证数据能够完好无损的到达 ...
- (总结)Nginx使用的php-fpm的两种进程管理方式及优化
PS:前段时间配置php-fpm的时候,无意中发现原来它还有两种进程管理方式.与Apache类似,它的进程数也是可以根据设置分为动态和静态的. php-fpm目前主要又两个分支,分别对应于php-5. ...
- Redis键管理
Redis键管理 Redis 键命令用于管理 redis 的键. 语法 Redis 键命令的基本语法如下: redis > COMMAND KEY_NAME redis > SET w3c ...
- 2013 ACMICPC 杭州现场赛 I题
#include<iostream> #include<cstring> #include<algorithm> #include<cmath> #in ...
- 洛谷 P1903 [国家集训队]数颜色 解题报告
P1903 [国家集训队]数颜色 题目描述 墨墨购买了一套\(N\)支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会向你发布如下指令: 1.Q L R代表询问你从第\(L\) ...
- [转]busybox登陆后没要求输入密码的解决办法
转自:http://blog.chinaunix.net/uid-8058395-id-65785.html 1.制作好ramdisk之后 通过串口进入系统 却发现系统直接登录进去了 并没有要求用ro ...
- 【HDU 3336 Count the string】
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- Codeforces Round #357 (Div. 2) B
B. Economy Game time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...