SpringIOC注入参数04

一、基本类型注入

创建项目,导入Spring4核心包,Junit4测试工具

1.fe23ca27eb42ae56d148654a774516dfdb5.jpg

2.ee100b459d165533cd47a6841a689270df6.jpg

d69ca38e110044888ef835ea4a4215c664c.jpg

People.java

package com.java.entity;
public class People {
	private int id;
	private int age;
	private String name;
	
	//默认构造函数
	public People() {
		super();

	}
	//带参构造函数
	public People(int id, int age, String name) {
		super();
		this.id = id;
		this.age = age;
		this.name = name;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return "People [id=" + id + ", age=" + age + ", name=" + name + "]";
	}
	
	
}

bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

  
    
      <!-- 属性注入 -->
      <bean id="people1" class="com.java.entity.People"> 
      <property name="id" value="1"></property>
       <property name="age" value="18"></property>
        <property name="name" value="威震天"></property>
      </bean>
      
  
</beans>

创建测试类

212096871375c58b8758ac51134669ca617.jpg

 

package com.java.test;

import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.java.entity.People;

public class T {
	
	private ApplicationContext ac;
	
	@Before
	public void setUp() throws Exception {
		ac=new ClassPathXmlApplicationContext("beans.xml");
	}

	@Test
	public void test1() {
		People people=(People)ac.getBean("people1");
		System.out.println(people);
	}

}

结果:

f0e456ed23f3eb9b6408a6d3064a8125ad6.jpg

 

二、注入bean

9fa50cf9e8d17020b6d77bfe541ba37b5c3.jpg

将Dog注入People类

Dog.java

package com.java.entity;

public class Dog {
	private String name;

	public String getName() {
		return name;
	}

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

People.java

package com.java.entity;
public class People {
	private int id;
	private int age;
	private String name;
	private Dog dog;
	//默认构造函数
	public People() {
		super();

	}
	//带参构造函数
	public People(int id, int age, String name) {
		super();
		this.id = id;
		this.age = age;
		this.name = name;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public Dog getDog() {
		return dog;
	}
	public void setDog(Dog dog) {
		this.dog = dog;
	}
	@Override
	public String toString() {
		return "People [id=" + id + ", age=" + age + ", name=" + name + ", dog=" + dog.getName() + "]";
	}
	
	
	
	
}

bean.xml注入bean

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

      <!--注入bean-->
	<bean id="dog1" class="com.java.entity.Dog">
		<property name="name" value="Jack"></property>
	</bean>
	
	<bean id="people2" class="com.java.entity.People">
		<property name="id" value="1"></property>
		<property name="name" value="张三"></property>
		<property name="age" value="11"></property>
		<property name="dog" ref="dog1"></property>
	</bean>

</beans>

测试结果Test2

24641a529866f239963c012e6af04b7e87a.jpg

三、内部bean

在bean内部在设置一个bean

 

a82c0e7f831692c9303cda677d005018256.jpg

四、null值

	<bean id="people4" class="com.java.entity.People">
		<property name="id" value="1"></property>
		<property name="name" value="张三"></property>
		<property name="age" value="11"></property>
		<property name="dog">
			<null></null>
		</property>
	</bean>

people.java toString()的getName()去掉,因为配置文件为空,dog.getName()会报空指针错误

@Override

	public String toString() {
		return "People [id=" + id + ", age=" + age + ", name=" + name + ", dog=" + dog.getName() + "]";
	}

修改后

package com.java.entity;
public class People {
	private int id;
	private int age;
	private String name;
	private Dog dog;
	//默认构造函数
	public People() {
		super();

	}
	//带参构造函数
	public People(int id, int age, String name) {
		super();
		this.id = id;
		this.age = age;
		this.name = name;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public Dog getDog() {
		return dog;
	}
	public void setDog(Dog dog) {
		this.dog = dog;
	}
	@Override
	public String toString() {
		return "People [id=" + id + ", age=" + age + ", name=" + name + ", dog=" + dog+ "]";
	}
	
	
	
	
}

测试

8787ff5045764c7b3cb2354057429ef9dc4.jpg

五、级联属性(不常用)

people.java

package com.java.entity;
public class People {
	private int id;
	private int age;
	private String name;
	//注意要这样实例化,不然级联报错
	private Dog dog=new Dog();
	//默认构造函数
	public People() {
		super();

	}
	//带参构造函数
	public People(int id, int age, String name) {
		super();
		this.id = id;
		this.age = age;
		this.name = name;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public Dog getDog() {
		return dog;
	}
	public void setDog(Dog dog) {
		this.dog = dog;
	}
	@Override
	public String toString() {
		return "People [id=" + id + ", age=" + age + ", name=" + name + ", dog=" + dog.getName()+ "]";
	}
	
	
	
	
}

bean.xml级联

28c8597005b9f2ec97774b52a50d75d3f02.jpg

六、集合类型属性

people.java

package com.java.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class People {
	private int id;
	private int age;
	private String name;

	private Dog dog=new Dog();
	
	private List<String> hobbies=new ArrayList<String>();
	private Set<String> loves=new HashSet<String>();
	private Map<String,String> works=new HashMap<String,String>();
	private Properties addresses=new Properties();
	
	//默认构造函数
	public People() {
		super();

	}
	//带参构造函数
	public People(int id, int age, String name) {
		super();
		this.id = id;
		this.age = age;
		this.name = name;
	}
	public int getId() {
		return id;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Dog getDog() {
		return dog;
	}
	public void setDog(Dog dog) {
		this.dog = dog;
	}
	public List<String> getHobbies() {
		return hobbies;
	}
	public void setHobbies(List<String> hobbies) {
		this.hobbies = hobbies;
	}
	public Set<String> getLoves() {
		return loves;
	}
	public void setLoves(Set<String> loves) {
		this.loves = loves;
	}
	public Map<String, String> getWorks() {
		return works;
	}
	public void setWorks(Map<String, String> works) {
		this.works = works;
	}
	public Properties getAddresses() {
		return addresses;
	}
	public void setAddresses(Properties addresses) {
		this.addresses = addresses;
	}
	public void setId(int id) {
		this.id = id;
	}
	@Override
	public String toString() {
		return "People [id=" + id + ", age=" + age + ", name=" + name + ", dog=" + dog.getName() + ", hobbies=" + hobbies
				+ ", loves=" + loves + ", works=" + works + ", addresses=" + addresses + "]";
	}
	
	
	
	
}

bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

      <!-- 属性注入 -->
      <bean id="people1" class="com.java.entity.People"> 
      <property name="id" value="1"></property>
       <property name="age" value="18"></property>
        <property name="name" value="威震天"></property>
      </bean>
      
      
	<bean id="dog1" class="com.java.entity.Dog">
		<property name="name" value="Jack"></property>
	</bean>
	
	<bean id="people2" class="com.java.entity.People">
		<property name="id" value="1"></property>
		<property name="name" value="张三"></property>
		<property name="age" value="11"></property>
		<property name="dog" ref="dog1"></property>
	</bean>
	
	<bean id="people3" class="com.java.entity.People">
		<property name="id" value="1"></property>
		<property name="name" value="张三"></property>
		<property name="age" value="11"></property>
		<property name="dog">
			<bean class="com.java.entity.Dog">
				<property name="name" value="Tom"></property>
			</bean>
		</property>
	</bean>
	
	<bean id="people4" class="com.java.entity.People">
		<property name="id" value="1"></property>
		<property name="name" value="张三"></property>
		<property name="age" value="11"></property>
		<property name="dog">
			<null></null>
		</property>
		
	</bean>
	<!-- 
		 <bean id="people5" class="com.java.entity.People">
		<property name="id" value="1"></property>
		<property name="name" value="张三"></property>
		<property name="age" value="11"></property>
		<property name="dog.name" value="Jack2"></property>
	</bean> 
	 -->
	 
	 <bean id="people6" class="com.java.entity.People">
		<property name="id" value="1"></property>
		<property name="name" value="张三"></property>
		<property name="age" value="11"></property>
		<property name="dog" ref="dog1"></property>
		<property name="hobbies">
			<list>
				<value>唱歌</value>
				<value>跳舞</value>
			</list>
		</property>
		<property name="loves">
			<set>
				<value>唱歌2</value>
				<value>跳舞2</value>
			</set>
		</property>
		<property name="works">
			<map>
				<entry>
					<key><value>上午</value></key>
					<value>写代码</value>
				</entry>
				<entry>
					<key><value>下午</value></key>
					<value>测试代码</value>
				</entry>
			</map>
		</property>
		<property name="addresses">
			<props>
				<prop key="address1">aaaaa</prop>
				<prop key="address2">bbbbb</prop>
			</props>
		</property>
	</bean>
	

</beans>

test6测试

package com.java.test;

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

import com.java.entity.People;

public class T {
	
	private ApplicationContext ac;
	
	@Before
	public void setUp() throws Exception {
		ac=new ClassPathXmlApplicationContext("beans.xml");
	}

	@Test
	public void test1() {
		People people=(People)ac.getBean("people1");
		System.out.println(people);
	}
	@Test
	public void test2() {
		People people=(People)ac.getBean("people2");
		System.out.println(people);
	}
	@Test
	public void test3() {
		People people=(People)ac.getBean("people3");
		System.out.println(people);
	}
	@Test
	public void test4() {
		People people=(People)ac.getBean("people4");
		System.out.println(people);
	}
	@Test
	public void test5() {
		People people=(People)ac.getBean("people5");
		System.out.println(people);
	}
	@Test
	public void test6() {
		People people=(People)ac.getBean("people6");
		System.out.println(people);
	}

}

结果41c11cdf21c5371621954570e1f0af1baea.jpg

转载于:https://my.oschina.net/u/3848699/blog/2244697

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值