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. ASP.NET Core 2.1 源码学习之 Options[3]:IOptionsMonitor 【转】

    原文链接:https://www.cnblogs.com/RainingNight/p/strongly-typed-options-ioptions-monitor-in-asp-net-core. ...

  2. leetcode 174. 地下城游戏 解题报告

    leetcode 174. 地下城游戏 一些恶魔抓住了公主(P)并将她关在了地下城的右下角.地下城是由 M x N 个房间组成的二维网格.我们英勇的骑士(K)最初被安置在左上角的房间里,他必须穿过地下 ...

  3. VMware下Linux配置局域网和外网访问

    我想尝试的是利用本机的ip+port来访问虚拟机上的web服务器,因为这样的话,我就能够将我的web服务器部署成为一个能让外网访问的服务器了,首先说下我的环境: 主机:系统win7,ip地址172.1 ...

  4. 那些牛掰的 HTML5的API(二)

    1:视频播放器 2:地理定位 我们的支持html5 的浏览器给我们提供一个接口(api),可以用来获取你当前的位置. 主要是通过geolocation(地理位置),对象 ,去访问硬件,来获取到经纬度. ...

  5. Linux下性能测量和调试诊断工具Systemtap

    一.简介 SystemTap是一个诊断Linux系统性能或功能问题的开源软件.它使得对运行时的Linux系统进行诊断调式变得更容易.更简单.有了它,开发者或调试人员不再需要重编译.安装新内核.重启动等 ...

  6. cd,PATH,alias,man,快捷键

    5. cd命令cd 后面不加东西,就是进入到当前用户的家目录cd ~ 这里的~符号也表示用户的家目录cd - 切换到上一次所在的目录cd . .. 其中.表示当前目录, ..表示上一级目录注意区分绝对 ...

  7. Scala 基础(5)—— 构建函数式对象

    有了 Scala 基础(4)—— 类和对象 的前提,现在就可以来构建一个基于 Scala 的函数式对象. 下面开始构造一个有理数对象 Rational. 1. 主构造方法和辅助构造方法 对于每一个类的 ...

  8. Dubbo基础介绍

    基础知识 Dubbo是什么:Dubbo是一个分布式的服务框架,提供高性能和透明化的RPC远程调用方案,以及SOA服务治理方案 Dubbo涉及的知识: 远程调用:RMI.hassion.webservi ...

  9. theMatrix代码雨效果

    做了一个代码雨效果放在个人主页:  https://lanleilin.github.io/lanGallery/index.html 代码: <!DOCTYPE html> <ht ...

  10. HDU 1711 Number Sequence---KMP原始

    #include<stdio.h> #include<string.h> #include<math.h> #include<stdlib.h> #in ...