版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
目录
扫描二维码关注公众号,回复:
7634232 查看本文章
一、实现功能
通过五种方式,注入到对应的Bean实例。通过构造方法、set方法、集合类的注入、null值注入、注入时创建内部Bean。
二、准备环境
1.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.12</version>
<scope>test</scope>
</dependency>
2.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"
default-autowire="byName"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
</beans>
三、五种注入方法
1.构造函数注入
(1)添加类以及对应构造函数-》Bean.java
package com.spring.ioc;
/**
* Created by Administrator on 2019/10/25.
*/
public class Bean {
private AnotherBean anotherBean;
private String string;
/**
* 构造方法
* @param anotherBean
* @param string
*/
public Bean(AnotherBean anotherBean, String string) {
this.anotherBean = anotherBean;
this.string = string;
}
public AnotherBean getAnotherBean() {
return anotherBean;
}
public void setAnotherBean(AnotherBean anotherBean) {
this.anotherBean = anotherBean;
}
public String getString() {
return string;
}
public void setString(String string) {
this.string = string;
}
@Override
public String toString() {
return "Bean{" +
"anotherBean=" + anotherBean +
", string='" + string + '\'' +
'}';
}
}
-》AnotherBean.java依赖类
package com.spring.ioc.c2;
public class AnotherBean {
}
(2)spring.xml
<bean class="com.spring.ioc.AnotherBean" id="anotherBean"/>
<bean class="com.spring.ioc.Bean" id="bean">
<constructor-arg index="0" name="anotherBean" type="com.spring.ioc.AnotherBean"
ref="anotherBean"/>
<constructor-arg index="1" name="string" type="java.lang.String"
value="aaa123"/>
</bean>
(3)测试结果
package com.spring.ioc;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by Administrator on 2019/10/25.
*/
public class testcode {
@Test
public void test(){
ApplicationContext context =new ClassPathXmlApplicationContext("spring.xml");
Bean bean=context.getBean("bean",Bean.class);
System.out.println("bean = " + bean);
}
}
结果:
bean = Bean{anotherBean=com.spring.ioc.AnotherBean@11438d26, string='aaa123'}
2.set注入
(1)代码
-》修改Bean类,添加测试set方法注入
package com.spring.ioc;
/**
* Created by Administrator on 2019/10/25.
*/
public class Bean {
//测试构造函数注入
private AnotherBean anotherBean;
private String string;
//测试set方法注入
private AnotherBean anotherBean1;
private String string1;
public AnotherBean getAnotherBean1() {
return anotherBean1;
}
public void setAnotherBean1(AnotherBean anotherBean1) {
this.anotherBean1 = anotherBean1;
}
public String getString1() {
return string1;
}
public void setString1(String string1) {
this.string1 = string1;
}
/**
* 构造方法
* @param anotherBean
* @param string
*/
public Bean(AnotherBean anotherBean, String string) {
this.anotherBean = anotherBean;
this.string = string;
}
public AnotherBean getAnotherBean() {
return anotherBean;
}
public void setAnotherBean(AnotherBean anotherBean) {
this.anotherBean = anotherBean;
}
public String getString() {
return string;
}
public void setString(String string) {
this.string = string;
}
@Override
public String toString() {
return "Bean{" +
"anotherBean=" + anotherBean +
", string='" + string + '\'' +
", anotherBean1=" + anotherBean1 +
", string1='" + string1 + '\'' +
'}';
}
}
(2)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"
default-autowire="byName"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<bean class="com.spring.ioc.AnotherBean" id="anotherBean"/>
<bean class="com.spring.ioc.Bean" id="bean">
<constructor-arg index="0" name="anotherBean" type="com.spring.ioc.AnotherBean"
ref="anotherBean"/>
<constructor-arg index="1" name="string" type="java.lang.String"
value="aaa123"/>
<!--set注入方法-->
<property name="anotherBean1" ref="anotherBean"/>
<property name="string1" value="bbbb2"/>
</bean>
</beans>
(3)测试代码
package com.spring.ioc;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by Administrator on 2019/10/25.
*/
public class testcode {
@Test
public void test(){
ApplicationContext context =new ClassPathXmlApplicationContext("spring.xml");
Bean bean=context.getBean("bean",Bean.class);
System.out.println("bean = " + bean);
}
}
结果:
bean = Bean{anotherBean=com.spring.ioc.AnotherBean@7a1ebcd8, string='aaa123', anotherBean1=com.spring.ioc.AnotherBean@7a1ebcd8, string1='bbbb2'}
3.构造函数方法和set注入方法的简单写法
(1)只是修改spring.xml文件,添加c和p标签
和
结果
<?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"
default-autowire="byName"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<bean class="com.spring.ioc.AnotherBean" id="anotherBean"/>
<!--<bean class="com.spring.ioc.Bean" id="bean">-->
<!--<constructor-arg index="0" name="anotherBean" type="com.spring.ioc.AnotherBean"-->
<!--ref="anotherBean"/>-->
<!--<constructor-arg index="1" name="string" type="java.lang.String"-->
<!--value="aaa123"/>-->
<!--<!–set注入方法–>-->
<!--<property name="anotherBean1" ref="anotherBean"/>-->
<!--<property name="string1" value="bbbb2"/>-->
<!--</bean>-->
<bean class="com.spring.ioc.Bean" id="bean"
c:anotherBean-ref="anotherBean" c:string="5555"
p:anotherBean1-ref="anotherBean" p:string1="6666"/>
</beans>
(2)测试代码
package com.spring.ioc;
import com.spring.ioc.c2.Bean;
import com.sun.javaws.security.AppPolicy;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test2 {
@Test
public void test(){
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
Bean bean=context.getBean("bean",Bean.class);
System.out.println("bean = " + bean);
// System.out.println("bean = " + bean.getAnotherBean());
}
}
4.集合类型的bean注入
(1)修改Bean类,添加几类属性以及get/set方法,同时修改tostring
//测试集合类型数据注入
private List<String> stringList;
private List<AnotherBean> anotherBeanList;
private Set<String> stringSet;
private Set<AnotherBean> anotherBeanSet;
private Map<String,String> stringMap;
private Map<AnotherBean,AnotherBean> beanAnotherBeanMap;
private Properties properties;
(2)修改spring.xml文件
<bean class="com.spring.ioc.AnotherBean" id="anotherBean"/>
<bean class="com.spring.ioc.Bean" id="bean">
<constructor-arg index="0" name="anotherBean" type="com.spring.ioc.AnotherBean"
ref="anotherBean"/>
<constructor-arg index="1" name="string" type="java.lang.String"
value="aaa123"/>
<!--set注入方法-->
<property name="anotherBean1" ref="anotherBean"/>
<property name="string1" value="bbbb2"/>
<!-- list-->
<property name="stringList">
<list>
<value>aaaa</value>
<value>bbbb</value>
</list>
</property>
<property name="anotherBeanList">
<list>
<ref bean="anotherBean"/>
<ref bean="anotherBean"/>
</list>
</property>
<!-- set-->
<property name="stringSet">
<set>
<value>aaaset</value>
<value>bbbset</value>
</set>
</property>
<property name="anotherBeanSet">
<set>
<ref bean="anotherBean"/>
<ref bean="anotherBean"/>
</set>
</property>
<!-- map-->
<property name="stringMap">
<map>
<entry key="aaa" value="aaavalue"/>
<entry key="aaa2" value="aaavalue2"/>
</map>
</property>
<property name="beanAnotherBeanMap" >
<map>
<entry key-ref="anotherBean" value-ref="anotherBean"/>
</map>
</property>
<!-- properties-->
<property name="properties">
<props>
<prop key="aaaproperty">bbb</prop>
</props>
</property>
</bean>
(3)测试代码
package com.spring.ioc;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by Administrator on 2019/10/25.
*/
public class testcode {
@Test
public void test(){
ApplicationContext context =new ClassPathXmlApplicationContext("spring.xml");
Bean bean=context.getBean("bean",Bean.class);
// System.out.println("bean = " + bean);
System.out.println("bean.getAnotherBeanList"+bean.getAnotherBeanList());
System.out.println("bean.getAnotherBeanList"+bean.getAnotherBeanSet());
System.out.println("bean.getAnotherBeanList"+bean.getBeanAnotherBeanMap());
System.out.println("bean.getAnotherBeanList"+bean.getStringList());
System.out.println("bean.getAnotherBeanList"+bean.getStringSet());
System.out.println("bean.getAnotherBeanList"+bean.getStringMap());
System.out.println("bean.getAnotherBeanList"+bean.getProperties());
// System.out.println("bean.getAnotherBeanList"+bean.getAnotherBean2());
}
}
结果:
bean.getAnotherBeanList[com.spring.ioc.AnotherBean@69a3d1d, com.spring.ioc.AnotherBean@69a3d1d]
bean.getAnotherBeanList[com.spring.ioc.AnotherBean@69a3d1d]
bean.getAnotherBeanList{com.spring.ioc.AnotherBean@69a3d1d=com.spring.ioc.AnotherBean@69a3d1d}
bean.getAnotherBeanList[aaaa, bbbb]
bean.getAnotherBeanList[aaaset, bbbset]
bean.getAnotherBeanList{aaa=aaavalue, aaa2=aaavalue2}
bean.getAnotherBeanList{aaaproperty=bbb}
5.null注入
(1)Bean类中声明anotherBean2属性,添加get/set方法以及toString方法
(2)spring.xml添加spring依赖
<!-- 空值null注入-->
<property name="anotherBean2">
<null/>
</property>
(3)测试代码
package com.spring.ioc;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by Administrator on 2019/10/25.
*/
public class testcode {
@Test
public void test(){
ApplicationContext context =new ClassPathXmlApplicationContext("spring.xml");
Bean bean=context.getBean("bean",Bean.class);
System.out.println("bean.getAnotherBeanList"+bean.getAnotherBean2());
}
}
结果:
bean.getAnotherBeanListnull
6.注入时创建内部bean
(1)Bean类中添加属性,并且设置get/set方法
//bean注入时创建内部bean
private AnotherBean anotherBean3;
(2)添加spring.xml
<!-- 注入时创建内部bean-->
<property name="anotherBean3">
<bean class="com.spring.ioc.AnotherBean"/>
</property>
(3)测试
package com.spring.ioc;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by Administrator on 2019/10/25.
*/
public class testcode {
@Test
public void test(){
ApplicationContext context =new ClassPathXmlApplicationContext("spring.xml");
Bean bean=context.getBean("bean",Bean.class);
// System.out.println("bean = " + bean);
System.out.println("bean.getAnotherBeanList"+bean.getAnotherBean3());
}
}
四、总结
1.上传代码到github~