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 ...
随机推荐
- python学习总结---面向对象1
面向对象 - 与面向过程对比 - 面向过程:数学逻辑的映射,学会做个好员工. - 面向对象:生活逻辑的映射,学会做个好领导. - 生活实例 - 类: 人 手机 电脑 - 对象: 习大大.普京 二狗的i ...
- JavaWeb笔记(九)Ajax&Json
AJAX 实现方式 原生的JS实现方式 //1.创建核心对象 var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, C ...
- web浏览器中的javascript
window 对象中其中一个最重要的属性是document,它引用document对象,后者表示显示在窗口中的文档.document对象有一些重要方法,比如getElementById(),它可以基于 ...
- npm & npm config
npm command show npm config https://docs.npmjs.com/cli/config https://docs.npmjs.com/cli/ls https:// ...
- RabbitMQ-Java客户端API指南-下
RabbitMQ-Java客户端API指南-下 使用主机列表 可以将Address数组传递给newConnection().的地址是简单地在一个方便的类com.rabbitmq.client包与主机 ...
- THUWC2018 题解
2018清华冬令营 又一次由于接连而至的玄学现象跪惨,错失良机,就不再公开提我这次惨痛的经历了,写点干货-- day1 A 零食 (1s, 1G) 试题简述 \(n\) 种物品1,\(m\) 种物品2 ...
- 决策树与随机森林Adaboost算法
一. 决策树 决策树(Decision Tree)及其变种是另一类将输入空间分成不同的区域,每个区域有独立参数的算法.决策树分类算法是一种基于实例的归纳学习方法,它能从给定的无序的训练样本中,提炼出树 ...
- Ubuntu安装完之后需要做的事情
字体推荐思源 lantern可以设置全局代理 安装好了ubuntu之后,安装gnome主题 安装Gnome之前,升级系统: $ sudo apt update $ sudo apt upgrade 1 ...
- 自适应注意力机制在Image Caption中的应用
在碎片化阅读充斥眼球的时代,越来越少的人会去关注每篇论文背后的探索和思考. 在这个栏目里,你会快速 get 每篇精选论文的亮点和痛点,时刻紧跟 AI 前沿成果. 点击本文底部的「阅读原文」即刻加入社区 ...
- hihocoder 后缀自动机五·重复旋律8 求循环同构串出现的次数
描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一段音乐旋律可以被表示为一段数构成的数列. 小Hi发现旋律可以循环,每次把一段旋律里面最前面一个音换到最后面就成为了原旋律的“循环相似旋律”,还可以 ...