构造函数注入
构造函数注入,类中需要提供一个对应参数列表的构造函数,并且xml中要给类里的每一个成员变量赋值,不然会注入失败d
xml涉及的标签及属性
标签
constructor-age
属性:
index:指定参数在构造函数参数列表的索引位置
type:指定参数在构造函数中的数据类型
name:指定参数在构造函数中的名称 用这个找给谁赋值
==上面三个都是找给谁赋值,下面两个指的是赋什么值的=
value:它能赋的值是基本数据类型和 String 类型
ref:它能赋的值是其他 bean 类型,也就是说,必须得是在配置文件中配置过的 bean
- AccountServiceImpl.java实现类
package com.cenzn.service.impl;
import com.cenzn.service.AccountService;
import java.util.Date;
public class AccountServiceImpl implements AccountService {
private String name ;
private Date date;
private Double money;
public AccountServiceImpl(String name, Date date, Double money) {
this.name = name;
this.date = date;
this.money = money;
}
public void save() {
System.out.println("保存了账户"+name+" "+date+" "+money);
}
}
- 配置文件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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="now" class="java.util.Date"></bean>
<bean id="accountService" class="com.cenzn.service.impl.AccountServiceImpl">
<constructor-arg name="name" value="张三"></constructor-arg>
<constructor-arg name="date" ref="now"></constructor-arg>
<constructor-arg name="money" value="10d"></constructor-arg>
</bean>
</beans>
- 测试类ConstructorTest.java
package test;
import com.cenzn.service.AccountService;
import com.cenzn.service.impl.AccountServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ConstructorTest {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
AccountService accountServiceImpl = (AccountService) ac.getBean("accountService");
accountServiceImpl.save();
}
}
- 测试结果
保存了账户张三 Mon Apr 20 22:11:16 CST 2020 10.0
set方法注入(实际开发中,此种方式用的较多)
涉及标签及属性
标签
property
属性
name:找的是类中 set 方法后面的部分
name:ref:给属性赋值是其他 bean 类型的
ref:给属性赋值是其他 bean 类型的
- 实现类AccountServiceImpl2.java
package com.cenzn.service.impl;
import com.cenzn.service.AccountService;
import java.util.Date;
public class AccountServiceImpl2 implements AccountService {
private String name ;
private Date date;
private Double money;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
public void save() {
System.out.println("保存了账户"+name+" "+date+" "+money);
}
}
- 配置类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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="now" class="java.util.Date"></bean>
<bean id="AccountServiceImpl2" class="com.cenzn.service.impl.AccountServiceImpl2">
<property name="name" value="李四"></property>
<property name="date" ref="now"></property>
<property name="money" value="1000d"></property>
</bean>
</beans>
- 测试类SetTest.java
package test;
import com.cenzn.service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SetTest {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
AccountService accountServiceImpl = (AccountService) ac.getBean("AccountServiceImpl2");
accountServiceImpl.save();
}
}
- 测试结果:
保存了账户李四 Mon Apr 20 22:15:27 CST 2020 1000.0
注入集合属性
涉及标签及属性
标签
property
属性
name:找的是类中 set 方法后面的部分
name:ref:给属性赋值是其他 bean 类型的
ref:给属性赋值是其他 bean 类型的
在注入集合数据时,只要结构相同,标签可以互换
List 标签
array,list,set
Map 标签
map,entry,props,prop
- 实现类AccountServiceImpl3.java
package com.cenzn.service.impl;
import com.cenzn.service.AccountService;
import java.util.*;
public class AccountServiceImpl3 implements AccountService {
private String[] myStrs;
private List<String> myList;
private Set<String> mySet;
private Map<String,String> myMap;
private Properties myProps;
public String[] getMyStrs() {
return myStrs;
}
public void setMyStrs(String[] myStrs) {
this.myStrs = myStrs;
}
public List<String> getMyList() {
return myList;
}
public void setMyList(List<String> myList) {
this.myList = myList;
}
public Set<String> getMySet() {
return mySet;
}
public void setMySet(Set<String> mySet) {
this.mySet = mySet;
}
public Map<String, String> getMyMap() {
return myMap;
}
public void setMyMap(Map<String, String> myMap) {
this.myMap = myMap;
}
public Properties getMyProps() {
return myProps;
}
public void setMyProps(Properties myProps) {
this.myProps = myProps;
}
public void save() {
System.out.println(Arrays.toString(myStrs));
System.out.println(myList);
System.out.println(mySet);
System.out.println(myMap);
System.out.println(myProps);
}
}
- 配置类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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 注入集合数据 在注入集合数据时,只要结构相同,标签可以互换
List 结构的:
array,list,set
Map 结构的
map,entry,props,prop
-->
<bean id="accountServiceImpl3" class="com.cenzn.service.impl.AccountServiceImpl3">
<!-- 给数组注入数据 -->
<property name="myStrs">
<array>
<value>myStrsA</value>
<value>myStrsB</value>
<value>myStrsC</value>
</array>
</property>
<!-- 注入 list 集合数据 -->
<property name="myList">
<list>
<value>ListA</value>
<value>ListB</value>
<value>ListC</value>
</list>
</property>
<!-- 注入 list 集合数据 -->
<property name="mySet">
<set>
<value>ListA</value>
<value>ListB</value>
<value>ListC</value>
</set>
</property>
<!-- 注入 Map 数据 -->
<property name="myMap">
<map>
<entry key="MapKey1" value="MapValue1" ></entry> <!-- 赋值方法一 -->
<entry key="MapKey2"> <!-- 赋值方法二 -->
<value>MapValue2</value>
</entry>
</map>
</property>
<!-- 注入 properties 数据 -->
<property name="myProps">
<props>
<prop key="PropsA" >PropsValue1</prop>
<prop key="PropsB" >PropsValue2</prop>
</props>
</property>
</bean>
</beans>
- 测试类Test3.java
package test;
import com.cenzn.service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test3 {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
AccountService accountServiceImpl = (AccountService) ac.getBean("accountServiceImpl3");
accountServiceImpl.save();
}
}
- 测试结果:
[myStrsA, myStrsB, myStrsC]
[ListA, ListB, ListC]
[ListA, ListB, ListC]
{MapKey1=MapValue1, MapKey2=MapValue2}
{PropsA=PropsValue1, PropsB=PropsValue2}