一、基本类型注入
创建项目,导入Spring4核心包,Junit4测试工具
1.
2.
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>
创建测试类
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);
}
}
结果:
二、注入bean
将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
三、内部bean
在bean内部在设置一个bean
四、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+ "]";
}
}
测试
五、级联属性(不常用)
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级联
六、集合类型属性
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);
}
}
结果