Spring-注入
一.给属性注入值(四种)
1.setter方法注入(就是在bean节点下有property节点给里面的属性值赋值
<bean id="service" class="demo04.service.HelloServiceImpl">
<property name="dao" ref="dao"></property>
</bean>
2.构造注入
3.p命名空间注入(使用前要先要在Spring配置文件中引入p命名空间xmlns:p="http://www.springframework.org/schema/p")这里的两种我写在一起了
首先写一个学生类,车类
package demo05;
/**
* Created by mycom on 2018/3/5.
*/
public class Student {
private String name;
private Integer age;
private Car car;
public Student() {
}
public Student(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
}
package demo05;
/**
* Created by mycom on 2018/3/5.
*/
public class Car {
private String color;
private String brand;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
在xml中的配置
<!--构造注入-->
<!--<bean id="stu" class="demo05.Student">
<constructor-arg index="0" value="小明"></constructor-arg>
<constructor-arg index="1" value="12"></constructor-arg>
</bean>-->
<!--p命名空间注入-->
<bean id="stu" class="demo05.Student" p:name="小明" p:age="12" p:car-ref="car"></bean>
最后编写测试类
以p命名空间注入为例进行测试
import demo04.service.HelloServiceImpl;
import demo04.service.IHelloService;
import demo05.MyCollection;
import demo05.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by mycom on 2018/3/3.
*/
public class Test20180305 {
@Test
public void t1(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContextCon.xml");
Student service2 =(Student) context.getBean("stu");
System.out.println(service2.getName());
} }
4.集合注入
再写一个MyCollection类
package demo05;
import java.util.*;
/**
* Created by mycom on 2018/3/5.
*/
public class MyCollection {
private String[] array;
private List<String> list;
private Set<String> set;
private Map<String,String> map;
private Properties properties;
public MyCollection() {
System.out.println("创建对象===========");
}
public MyCollection(String[] array, List<String> list, Set<String> set, Map<String, String> map, Properties properties) {
this.array = array;
this.list = list;
this.set = set;
this.map = map;
this.properties = properties;
}
@Override
public String toString() {
return "MyCollection{" +
"array=" + Arrays.toString(array) +
", list=" + list +
", set=" + set +
", map=" + map +
", properties=" + properties +
'}';
}
public String[] getArray() {
return array;
}
public void setArray(String[] array) {
this.array = array;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public Set<String> getSet() {
return set;
}
public void setSet(Set<String> set) {
this.set = set;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
}
<!--集合注入-->
<bean id="collection" class="demo05.MyCollection" scope="prototype">
<!--Array-->
<property name="array">
<array>
<value>小明</value>
<value>小兰</value>
</array>
</property>
<!--list-->
<property name="list">
<list>
<value>list1</value>
<value>list2</value>
</list>
</property>
<!--set-->
<property name="set">
<set>
<value>set1</value>
<value>set2</value>
</set>
</property>
<!--map-->
<property name="map">
<map>
<entry key="1">
<value>01</value>
</entry>
<entry key="2">
<value>02</value>
</entry>
</map>
</property>
<!--propreties-->
<property name="properties">
<props>
<prop key="001">001</prop>
<prop key="002">002</prop>
</props>
</property>
</bean>
import demo04.service.HelloServiceImpl;
import demo04.service.IHelloService;
import demo05.MyCollection;
import demo05.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by mycom on 2018/3/3.
*/
public class Test20180305 {
@Test
public void t1(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContextCon.xml");
//返回的类型只能是接口
MyCollection service =(MyCollection) context.getBean("collection");
System.out.println(service);
}
}

浙公网安备 33010602011771号