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属性注入的更多相关文章

  1. Spring 属性注入(一)JavaBean 内省机制在 BeanWrapper 中的应用

    Spring 属性注入(一)JavaBean 内省机制在 BeanWrapper 中的应用 Spring 系列目录(https://www.cnblogs.com/binarylei/p/101174 ...

  2. Spring 属性注入(二)BeanWrapper 结构

    Spring 属性注入(二)BeanWrapper 结构 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html) BeanWrap ...

  3. Spring 属性注入(三)AbstractNestablePropertyAccessor

    Spring 属性注入(三)AbstractNestablePropertyAccessor Spring 系列目录(https://www.cnblogs.com/binarylei/p/10117 ...

  4. Spring 属性注入(四)属性键值对 - PropertyValue

    Spring 属性注入(四)属性键值对 - PropertyValue Spring 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html) P ...

  5. spring 属性注入

    Spring的核心技术室依赖注入,下面是依赖注入之属性注入的实现过程,牛刀小试,请看效果. 1.首先添加Spring.Web引用.本例中是使用分层思想来演示的,下面是项目的结构和UserModel类的 ...

  6. Spring属性注入、构造方法注入、工厂注入以及注入参数(转)

    Spring 是一个开源框架. Spring 为简化企业级应用开发而生(对比EJB2.0来说). 使用 Spring 可以使简单的 JavaBean 实现以前只有 EJB 才能实现的功能.Spring ...

  7. 六 Spring属性注入的四种方式:set方法、构造方法、P名称空间、SPEL表达式

    Spring的属性注入: 构造方法的属性注入 set方法的属性注入

  8. java spring属性注入

    一.创建对象时候,向类里面属性设置值:一般有三个方式 1) .有参构造, 2). set**** 3).接口注入 二. 在spring框架里面,支持前面两种方式: 1).有参构造方法  用constr ...

  9. spring属性注入DI

    spring setter方式注入: 注入对象属性: 前提: 在bean对应实体中有对应的setter方法. 基础代码: 在bean中有另一个bean属性的setter方法. package cn.i ...

随机推荐

  1. 孤荷凌寒自学python第三天 初识序列

    孤荷凌寒自学python第三天 初识序列 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) Python的序列非常让我着迷,之前学习的其它编程语言中没有非常特别关注过序列这种类型的对象,而pyt ...

  2. FOJ Problem 1016 无归之室

     Problem 1016 无归之室 Accept: 926    Submit: 7502Time Limit: 1000 mSec    Memory Limit : 32768 KB  Prob ...

  3. CSLA多语言设置

    1.在程序运行文件夹例如“\Bin\Debug\”中包含csla生成的资源文件: 2.在程序运行时,设置CSLA的当前语言为你想要的语言,例如:Csla.Properties.Resources.Cu ...

  4. springmvc和struts2的区别(转)

    1.客户端浏览器发出HTTP请求.   2.根据web.xml配置,该请求被FilterDispatcher接收.   3.根据struts.xml配置,找到需要调用的Action类和方法, 并通过I ...

  5. P3531 [POI2012]LIT-Letters

    题目描述 Little Johnny has a very long surname. Yet he is not the only such person in his milieu. As it ...

  6. redis常用监控命令

    redis常用监控命令 1.实时监控redis服务收到来自应用的所有命令 1 2 3 4 5 6 7 redis-cli   127.0.0.1:6379>monitor   150996415 ...

  7. 2017 多校2 hdu 6053 TrickGCD

    2017 多校2 hdu 6053 TrickGCD 题目: You are given an array \(A\) , and Zhu wants to know there are how ma ...

  8. 第二届360杯全国大学生信息安全技术大赛部分解题思路(WEB安全)

    第一题如下: 用burpsuit设置好代理后,点击发送验证码,可以看到如下: 然后go之后可以看到如下的验证码: 提交验证码后即可获得key 第二题如下: 通过/data/mysql_error_tr ...

  9. Linux装软件

    一.rpm包安装方式步骤: 1.找到相应的软件包,比如soft.version.rpm,下载到本机某个目录: 2.打开一个终端,su -成root用户: 3.cd soft.version.rpm所在 ...

  10. linux缺页异常处理--用户空间【转】

    转自:http://blog.csdn.net/vanbreaker/article/details/7870769 版权声明:本文为博主原创文章,未经博主允许不得转载. 用户空间的缺页异常可以分为两 ...