dependency injection DI
IOC的作用是削减程序间的耦合关系
依赖关系的管理都交给spring管理,即当前类要用到其他对象,由spring为我们提供,只需在配置文件中说明依赖关系,称之为依赖注入
能注入的数据有三类 基本数据类型String,其他bean类型,集合类型
注入方式有三种,使用构造函数提供,使用set方法提供,使用注解注入
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 把创建对象交给spring管理 -->
<!-- 1.使用构造函数注入 type index name value ref-->
<bean id="accountService" class="com.test.AccountServiceImp">
<constructor-arg index="0" value="xiaohua"></constructor-arg>
<constructor-arg index="1" value="18"></constructor-arg>
<constructor-arg index="2" ref="now"></constructor-arg>
</bean>
<bean id="now" class="java.util.Date"/>
<!-- set方法注入 常用,要求有默认构造函数-->
<bean id="accountService2" class="com.test.AccountServiceImp">
<property name="name" value="张绍文"></property>
<property name="age" value="33"></property>
<property name="birthday" ref="now"></property>
</bean>
<!-- 注入集合
用于list的标签有list set array
用于map的标签有map props
-->
<bean id="accountService3" class="com.test.AccountServiceImp">
<property name="strs">
<set>
<value>aaa</value>
<value>bbb</value>
<value>ccc</value>
</set>
</property>
<property name="list">
<list>
<value>aaa</value>
<value>bbb</value>
<value>ccc</value>
</list>
</property>
<property name="set">
<set>
<value>aaa</value>
<value>bbb</value>
<value>ccc</value>
</set>
</property>
<property name="map">
<map>
<entry key="testA" value="aaa"></entry>
<entry key="testB" value="bbb"></entry>
<entry key="testC" value="ccc"></entry>
</map>
</property>
<property name="props">
<props>
<prop key="testC">ccc</prop>
<prop key="testD">ddd</prop>
</props>
</property>
</bean>
</beans>
/**
* 依赖注入
*/
package com.test;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test{
public static void main(String[] args){
//1.获取核心容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2.根据id获取bean对象
IAccountService as = (IAccountService)ac.getBean("accountService");
System.out.println(as);
as.saveAccount();
IAccountService as2 = (IAccountService)ac.getBean("accountService2");
as2.saveAccount();
IAccountService as3 = (IAccountService)ac.getBean("accountService3");
as3.saveAccount();
}
}
interface IAccountService{
/**
* 模拟保存账户
*/
public void saveAccount();
}
/**
* 业务层实现类
*/
class AccountServiceImp implements IAccountService{
//若是经常变化的数据,并不适用配置文件注入的方式
private String name;
private Integer age;
private Date birthday;
private String[] strs;
private List<String> list;
private Set<String> set;
private Map<String,String> map;
private Properties props;
public AccountServiceImp(String name,Integer age,Date birthday){
this.name=name;
this.age=age;
this.birthday=birthday;
}
public AccountServiceImp(){
}
public void saveAccount(){
System.out.println("save a account"+name+" "+age+" "+birthday);
System.out.println(Arrays.toString(strs));
System.out.println(list);
System.out.println(set);
System.out.println(map);
System.out.println(props);
}
public void setAge(Integer age) {
this.age = age;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public void setName(String name) {
this.name = name;
}
public void setList(List<String> list) {
this.list = list;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public void setProps(Properties props) {
this.props = props;
}
public void setSet(Set<String> set) {
this.set = set;
}
public void setStrs(String[] strs) {
this.strs = strs;
}
}
2020-3-24 23:43:55 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1ef9f1d: display name [org.springframework.context.support.ClassPathXmlApplicationContext@1ef9f1d]; startup date [Tue Mar 24 23:43:55 CST 2020]; root of context hierarchy
2020-3-24 23:43:55 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [bean.xml]
2020-3-24 23:43:55 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@1ef9f1d]: org.springframework.beans.factory.support.DefaultListableBeanFactory@bc8e1e
2020-3-24 23:43:55 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@bc8e1e: defining beans [accountService,now,accountService2,accountService3]; root of factory hierarchy
com.test.AccountServiceImp@b9e45a
save a accountxiaohua 18 Tue Mar 24 23:43:55 CST 2020
null
null
null
null
null
save a account张绍文 33 Tue Mar 24 23:43:55 CST 2020
null
null
null
null
null
save a accountnull null null
[aaa, bbb, ccc]
[aaa, bbb, ccc]
[aaa, bbb, ccc]
{testA=aaa, testB=bbb, testC=ccc}
{testD=ddd, testC=ccc}