Spring的学习之路(三)的Spring详细配置属性注入

1 传统的的属性设置

1.1 构造方法

public class User{
    private String name;
    private String password;
    public void user(String name , String password){
        this.name = name;
        this.password = password;
    }
}

1.2 set方法

public void class User{
    private String name;
    public void setName(String name){
        this.name = name;
    }

}

1.3 接口注入的方式

public interface object(){
    public void setName(String name);
}
public class user implement object{
    private String name;
    public void setName(){
        this.name = name;
    }
}

Spring支持1.2和1.3的方法

2 Spring的属性注入

2.1 构造方法属性注入

2.1.1 创建一个类具有构造方法

package com.heshihua.spring.demo4;
/*
 * 构造方法
 * */
public class Car {
	private String name;
	private Double price;
	public Car(String name , Double price) {
		super();
		this.name = name;
		this.price = price;
	}
	@Override
	public String toString() {
		return "car [name=" + name + ", price=" + price + "]";
	}
	
}

2.1.2 配置bean

	<!-- 构造方法的方式 -->
	<bean id="car" class="com.heshihua.spring.demo4.Car">
		<constructor-arg name="name" value="宝马"/>
		<constructor-arg name="price" value="800000"/>
	</bean>

2.1.3 demo运行代码以及结果

	/*
	 * 构造方法的属性注入
	 * */
	@Test
	public void demo1() {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		Car car = (Car) applicationContext.getBean("car");
		System.out.println(car);
	}

2.2 set方法属性注入

2.2.1 创建一个具有set方法的类

package com.heshihua.spring.demo4;
/*
 * set方法的属性注入
 * */
public class Car2 {
	private String name;
	private Double price;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Double getPrice() {
		return price;
	}
	public void setPrice(Double price) {
		this.price = price;
	}
	@Override
	public String toString() {
		return "Car2 [name=" + name + ", price=" + price + "]";
	}
	
	
}

2.2.2 配置bean

	<!-- set方法的方式 -->
	<bean id = "car2" class = "com.heshihua.spring.demo4.Car2">
		<property name="name" value = "奔驰"></property>
		<property name = "price" value = "1000000"></property>
	</bean>

2.2.3 demo运行代码以及结果

/*
	 * set方法的属性注入
	 * */
	@Test
	public void demo2() {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		Car2 car2 = (Car2) applicationContext.getBean("car2");
		System.out.println(car2);
	}

2.3 set方法对象注入

2.3.1创建一个具有对象属性的类

package com.heshihua.spring.demo4;

public class Employee {
	private String name;
	private Car2 car2;
	public void setName(String name) {
		this.name = name;
	}
	public void setCar2(Car2 car2) {
		this.car2 = car2;
	}
	@Override
	public String toString() {
		return "Employee [name=" + name + ", car2=" + car2 + "]";
	}
}

2.3.2 配置bean

	<!-- set方法对象类型的属性注入 -->
	<bean id = "employee" class = "com.heshihua.spring.demo4.Employee">
		<!-- value设置普通类型的值;ref设置其他类的id或者name -->
		<property name="name" value = "赵星标"></property>
		<property name="car2" ref = "car2"></property>
	</bean>

2.3.3 demo运行代码以及结果

	/*
	 * set方法对象注入
	 * */
	@Test
	public void demo3() {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		Employee employee = (Employee) applicationContext.getBean("employee");
		System.out.println(employee);
	}

2.4 P名称空间完成属性注入

2.4.1 配置文件先得引入p

不带对象属性的配置

	<!-- 改为p名称空间的方式 -->
	<!-- <bean id = "car2" class = "com.heshihua.spring.demo4.Car2" p:name = "奇瑞QQ" p:price = "30000"></bean> -->
	

带对象数属性的配置

<!-- 改为p名称空间的方式带对象属性的 -->
<bean id = "employee" class = "com.heshihua.spring.demo4.Employee" p:name = "王东" p:car2-ref="car2"></bean>

运行代码和上边是一样的

2.4 集合属性注入

建立一个含有集合属性的类

package com.heshihua.spring.demo5;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

/*
 * 集合属性的注入
 * 
 * */
public class CollectionBean {
	private String[] arrs;
	private List<String> list;
	private Set<String> set;
	private Map<String,String> map;
	public void setArrs(String[] arrs) {
		this.arrs = arrs;
	}
	public void setList(List<String> list) {
		this.list = list;
	}
	public void setSet(Set<String> set) {
		this.set = set;
	}
	public void setMap(Map<String, String> map) {
		this.map = map;
	}
	@Override
	public String toString() {
		return "CollectionBean [arrs=" + Arrays.toString(arrs) + ", list=" + list + ", set=" + set + ", map=" + map
				+ "]";
	}
	
	
	
	
}

bean配置

<bean id = "collectionBean" class = "com.heshihua.spring.demo5.CollectionBean">
		<!-- 注入数组类型 -->
		<property name="arrs">
			<list>
				<value>王东</value>
				<value>赵红</value>
				<value>李冠西</value>
			</list>
		</property>
		<!-- 注入list集合 -->
		<property name="list">
			<list>
				<value>1</value>
				<value>2</value>
				<value>3</value>
				<value>4</value>
			</list>
		</property>
		<!-- 注入set集合 -->
		<property name="set">
			<set>
				<value>a</value>
				<value>b</value>
				<value>c</value>
				<value>d</value>
			</set>
		</property>
				<!-- 注入list集合 -->
		<property name="map">
			<map>
				<entry key="111" value= "aaa"></entry>
				<entry key="222" value= "bbb"></entry>
				<entry key="333" value= "ccc"></entry>
				<entry key="444" value= "ddd"></entry>
			</map>
		</property>
	</bean>

demo代码运行

package com.heshihua.spring.demo5;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/*
 * 集合类型的属性注入
 * 
 * */
public class SpringDemo5 {
	@Test
	public void demo1() {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		CollectionBean collectionBean = (CollectionBean) applicationContext.getBean("collectionBean");
		System.out.println(collectionBean);
	}
}

运行结果如下图

 

假如说是类似与list类型的属性 也可以用上述方法如果list的数据类型是对象把value换成ref即可

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值