Spring IoC
依赖pom.xml
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.7.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency> package com.demo.ioc; public class Bean { }
spring.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交由Spring创建并管理--> <bean id="bean" class="com.demo.ioc.Bean"/> </beans>
测试
package com.demo.ioc; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class BeanTest { @Test public void testBean(){ // 获取上下文 ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); // 获取bean Bean bean = context.getBean("bean", Bean.class); System.out.println(bean); } }
实例化Bean方式
- 通过构造方法实例化Bean
- 通过静态方法实例化Bean
- 通过实例方法实例化Bean
- Bean的别名
1、通过构造方法实例化Bean
package com.demo.ioc; public class Bean { public Bean(){} }
<bean id="bean" class="com.demo.ioc.Bean"/>
2、通过静态方法实例化Bean
package com.demo.ioc; public class BeanFactory { public static Bean createBean(){ return new Bean(); } } <bean id="bean" class="com.demo.ioc.BeanFactory" factory-method="createBean"/>
3、通过实例方法实例化Bean
package com.demo.ioc; public class BeanFactory { public Bean createBean(){ return new Bean(); } } <bean id="beanFactory" class="com.demo.ioc.BeanFactory"/> <bean id="bean" class="com.demo.ioc.Bean" factory-bean="beanFactory" factory-method="createBean"/>
4、Bean的别名
<bean id="bean" class="com.demo.ioc.Bean" name="bean1,bean2"/> <!-- alias不支持逗号分隔 --> <alias name="bean" alias="bean3"/>
注入Bean方式
通过构造方法注入Bean
通过set方式注入Bean
集合类Bean的注入(List、Set、Map、Properties)
null注入
注入时创建内部Bean
1、通过构造方法注入Bean
package com.demo.ioc; public class Bean { private String name; private AnotherBean anotherBean; public Bean(String name, AnotherBean anotherBean) { this.name = name; this.anotherBean = anotherBean; } public String getName() { return name; } public void setName(String name) { this.name = name; } public AnotherBean getAnotherBean() { return anotherBean; } public void setAnotherBean(AnotherBean anotherBean) { this.anotherBean = anotherBean; } @Override public String toString() { return "Bean{" + "name='" + name + '\'' + ", anotherBean=" + anotherBean + '}'; } } package com.demo.ioc; public class AnotherBean { }
<bean id="anotherBean" class="com.demo.ioc.AnotherBean"/> <bean id="bean" class="com.demo.ioc.Bean"> <!-- index、name、type三个属性组合确定参数 --> <!-- value用于简单数据类型 --> <!-- ref用于复杂数据类型 --> <constructor-arg index="0" name="name" type="java.lang.String" value="Tom"/> <constructor-arg index="1" name="anotherBean" type="com.demo.ioc.AnotherBean" ref="anotherBean"/> </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" xmlns:c="http://www.springframework.org/schema/c" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="anotherBean" class="com.demo.ioc.AnotherBean"/> <bean id="bean" class="com.demo.ioc.Bean" c:name="Jack" c:anotherBean-ref="anotherBean"/> </beans>
2、通过set方式注入Bean
package com.demo.ioc; public class Bean { private String name; private AnotherBean anotherBean; public String getName() { return name; } public void setName(String name) { this.name = name; } public AnotherBean getAnotherBean() { return anotherBean; } public void setAnotherBean(AnotherBean anotherBean) { this.anotherBean = anotherBean; } @Override public String toString() { return "Bean{" + "name='" + name + '\'' + ", anotherBean=" + anotherBean + '}'; } } <bean id="anotherBean" class="com.demo.ioc.AnotherBean"/> <bean id="bean" class="com.demo.ioc.Bean"> <property name="name" value="Jack"/> <property name="anotherBean" ref="anotherBean"/> </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" xmlns:c="http://www.springframework.org/schema/c" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="anotherBean" class="com.demo.ioc.AnotherBean"/> <bean id="bean" class="com.demo.ioc.Bean" p:name="Jack" p:anotherBean-ref="anotherBean"/> </beans>
3、集合类Bean的注入(List、Set、Map、Properties)
package com.demo.ioc; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class Bean { private List<String> stringList; private List<AnotherBean> anotherBeanList; private Set<String> stringSet; private Set<AnotherBean> anotherBeanSet; private Map<String, String> stringMap; private Map<String, AnotherBean> anotherBeanMap; private Properties properties; private AnotherBean anotherBean1; private AnotherBean anotherBean2; public void setAnotherBean1(AnotherBean anotherBean1) { this.anotherBean1 = anotherBean1; } public void setAnotherBean2(AnotherBean anotherBean2) { this.anotherBean2 = anotherBean2; } public void setProperties(Properties properties) { this.properties = properties; } public void setStringList(List<String> stringList) { this.stringList = stringList; } public void setAnotherBeanList(List<AnotherBean> anotherBeanList) { this.anotherBeanList = anotherBeanList; } public void setStringSet(Set<String> stringSet) { this.stringSet = stringSet; } public void setAnotherBeanSet(Set<AnotherBean> anotherBeanSet) { this.anotherBeanSet = anotherBeanSet; } public void setStringMap(Map<String, String> stringMap) { this.stringMap = stringMap; } public void setAnotherBeanMap(Map<String, AnotherBean> anotherBeanMap) { this.anotherBeanMap = anotherBeanMap; } }
<bean id="anotherBean" class="com.demo.ioc.AnotherBean"/> <bean id="bean" class="com.demo.ioc.Bean"> <property name="stringList"> <list> <value>Tom</value> <value>Jack</value> </list> </property> <property name="anotherBeanList"> <list> <ref bean="anotherBean"/> <ref bean="anotherBean"/> </list> </property> <property name="stringSet"> <set> <value>Tom</value> <value>Jack</value> </set> </property> <property name="anotherBeanSet"> <set> <ref bean="anotherBean"/> <ref bean="anotherBean"/> </set> </property> <property name="stringMap"> <map> <entry key="name" value="Tom"/> <entry key="age" value="23"/> </map> </property> <property name="anotherBeanMap"> <map> <entry key="name" value-ref="anotherBean"/> <entry key="age" value-ref="anotherBean"/> </map> </property> <property name="properties"> <props> <prop key="name">Tom</prop> </props> </property> <property name="anotherBean1"> <!-- null注入 --> <null/> </property> <property name="anotherBean2"> <!-- 注入时创建内部Bean --> <bean class="com.demo.ioc.AnotherBean"/> </property> </bean>