Setter注入:最常用的注入方法,需要在xml配置文件中进行手动的配置.
要求:
对象中的每个属性必须要有setter 方法
如何配置:
需要为对象中的每个属性配置一个"property"标签:
<property name="" />
其中name属性的值为对象中属性的名字.如何给属性注入值时,需要先确定该属性的类型
对象中的属性分为以下三种类型,不同的类型使用不同的注入方式
1):简单数据类型(八大基本类型,String,BigDecimal,Date等). 使用value.
2):引用数据类型. 使用ref.
3):集合数据类型. 使用集合的元素.
代码:
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>text</groupId>
<artifactId>Spring02</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<properties>
<!--
定义全局变量:变量名为project.spring.version
-->
<project.spring.version>5.0.0.RELEASE</project.spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${project.spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${project.spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${project.spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${project.spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${project.spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${project.spring.version}</version>
</dependency>
</dependencies>
<build>
<!--加载资源文件-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
</project>
App-context.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">
<!--集合数据类型,使用property标签,在双标签中使用各个集合对应的标签-->
<bean id="collectionBean" class="text.spring._01_setter.CollectionBean">
<property name="set">
<set>
<value>set1</value>
<value>18</value>
<value>1000</value>
<ref bean="person"></ref>
</set>
</property>
<property name="list">
<list>
<value>list1</value>
<value>list2</value>
<value>list3</value>
</list>
</property>
<property name="array">
<array>
<value>array1</value>
<value>array2</value>
<value>array3</value>
</array>
</property>
<property name="map">
<map>
<entry key="k1" value="v1"></entry>
<entry key="k2" value="v2"></entry>
<entry key="k3" value="v3"></entry>
<!--<entry key="k4" value-ref="person"></entry>-->
</map>
</property>
<property name="prop">
<props>
<prop key="p1">v1</prop>
<prop key="p2">v2</prop>
</props>
</property>
</bean>
<!--引用数据类型,使用ref引用当前容器当中的对象-->
<bean id="dog" class="text.spring._01_setter.Dog">
<property name="name" value="汪汪"/>
</bean>
<!--ref:引用当前容器当中的bean-->
<bean id="person" class="text.spring._01_setter.Person">
<property name="dog" ref="dog"/>
</bean>
<!--简单数据类型-->
<bean id="employee" class="text.spring._01_setter.Employee">
<!--
setter方式注入,本质是在执行对象中的set方法
Spring容器在注入值时,隐含着类型转换
-->
<property name="name" value="rose"/>
<property name="age" value="20"/>
<property name="birthday" value="2019/09/03"/>
<property name="salary" value="1000"/>
</bean>
</beans>
App.java:
package text.spring._01_setter;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Created by thinkpad on 2019/9/3.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class App {
@Autowired
private Employee employee;
@Autowired
private Person person;
@Autowired
private CollectionBean collectionBean;
@Test
public void testEmployee() throws Exception {
System.out.println(employee);
}
@Test
public void testPerson() throws Exception {
System.out.println(person);
}
@Test
public void testCollectionBean() throws Exception {
System.out.println(collectionBean);
}
}
简单数据类型:
Employee.java:
package text.spring._01_setter;
import java.math.BigDecimal;
import java.util.Date;
/**
* Created by thinkpad on 2019/9/3.
*/
public class Employee {
private String name;
private Integer age;
private Date birthday;
private BigDecimal salary;
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public void setSalary(BigDecimal salary) {
this.salary = salary;
}
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", age=" + age +
", birthday=" + birthday +
", salary=" + salary +
'}';
}
}
引用数据类型:
Person.java:
package text.spring._01_setter;
/**
* Created by thinkpad on 2019/9/3.
*/
public class Person {
private Dog dog;
public void setDog(Dog dog) {
this.dog = dog;
}
public String toString() {
return "Person{" +
"dog=" + dog +
'}';
}
}
Dog.java:
package text.spring._01_setter;
/**
* Created by thinkpad on 2019/9/3.
*/
public class Dog {
private String name;
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
'}';
}
}
集合数据类型:
CollectionBean.java:
package text.spring._01_setter;
import java.util.*;
/**
* Created by thinkpad on 2019/9/3.
*/
public class CollectionBean {
private Set set;
private List<String> list;
private String[] array;
private Map<String, String> map;
private Properties prop;
public void setSet(Set set) {
this.set = set;
}
public void setList(List<String> list) {
this.list = list;
}
public void setArray(String[] array) {
this.array = array;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public void setProp(Properties prop) {
this.prop = prop;
}
@Override
public String toString() {
return "CollectionBean{" +
"set=" + set +
", list=" + list +
", array=" + Arrays.toString(array) +
", map=" + map +
", prop=" + prop +
'}';
}
}

本文详细介绍了Spring框架中Setter注入的使用,包括如何配置XML以实现简单数据类型、引用数据类型和集合数据类型的注入。针对不同类型的属性,如八大基本类型、String、BigDecimal、Date等,可以使用`value`标签注入;对于引用数据类型,使用`ref`标签进行注入;而对于集合数据类型,如List、Set等,需要利用集合元素进行注入。
798

被折叠的 条评论
为什么被折叠?



