xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"加入可以简化
注入Bean的方式:
通过构造方法注入Bean
通过set方法注入Bean
通过注解注入(@Autowired、@Resource)
1、构造方式注入
类中有带参数构造方法
xml中配置bean,子标签constructor-arg标签,配置属性 index、name、type确定构造参数,配置value,ref给属性赋值(ref用用,value基本)
还要简便写法如图,要改一下文件上面头部申明
2、集合类型注入
类中get/set方法
xml中配置property标签,标签下子标签list,list下用value/ref标签赋值;set用set标签,下面也用value/ref标签赋值;map用Map标签,entry标签赋值;properties,子标签props子标签prop赋值
第一种:通过构造方法注入Bean
#<bean>标签管理class对象。
#bean的构造方法参数标签 constructor-arg。
#index 表示构造方法的第几个参数。
#name 表示构造方法的参数名。
#type 表示对应的参数类型。
#value 表示为参数提供赋值,一般用于String等简单常用对象。
#ref 表示为参数提供赋值,一般是复杂点的依赖其它 class的对象(取bean的id)。
注意:只要通过index、name、type的组合明确表示出构造函数的那一个参数即可,
不一定全用,只用index也可以。
<bean class="com.imooc.spring.ioc.class06.Bean">
<constructor-arg index="0" name="anotherBean"
type="com.imooc.spring.ioc.class06.AnotherBean">
</constructor-arg>
</bean>
通过构造方法注入Bean,输出结果:
成功的给属性赋值。
第一种:通过构造方法注入Bean
#<bean>标签管理class对象。
#bean的构造方法参数标签 constructor-arg。
#index 表示构造方法的第几个参数。
#name 表示构造方法的参数名。
#type 表示对应的参数类型。
#value 表示为参数提供赋值,一般用于String等简单常用对象。
#ref 表示为参数提供赋值,一般是复杂点的依赖其它 class的对象(取bean的id)。
注意:只要通过index、name、type的组合明确表示出构造函数的那一个参数即可,
不一定全用,只用index也可以。
<bean class="com.imooc.spring.ioc.class06.Bean">
<constructor-arg index="0" name="anotherBean"
type="com.imooc.spring.ioc.class06.AnotherBean">
</constructor-arg>
</bean>
通过构造方法注入Bean,输出结果:
成功的给属性赋值。
总结:
第一种:构造方法注入bean
第二种:通过set方法注入Bean
第三种:集合类型Bean注入
List和Set
Map和Propertyes
第四种:注入null值
第五点:注入内部Bean
注入bean
这是注入方式展示:PojoBean{ name='gavin', bean=com.example.spring.ioc.Bean@4c39bec8, num=0, bean1=com.example.spring.ioc.Bean@4c39bec8, listStr=[str1, str2], listBean=[com.example.spring.ioc.Bean@4c39bec8], setStr=[setStr1, setStr2], setBean=[com.example.spring.ioc.Bean@4c39bec8], map={key1=com.example.spring.ioc.Bean@4c39bec8}, mapBean={com.example.spring.ioc.Bean@4c39bec8=com.example.spring.ioc.Bean@4c39bec8}, pro={proKey=proValue} }
package com.example.spring.ioc; import org.junit.jupiter.api.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class BeanTests { @Test public void Test(){ //通过构造方法实例化bean ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml"); System.out.println("这是注入方式展示:" + ac.getBean("pojoBean").toString()); } }
package com.example.spring.ioc; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class PojoBean { private String name; private Bean bean; private Integer num; private Bean bean1; private List<String> listStr; private List<Bean> listBean; private Set<String> setStr; private Set<Bean> setBean; private Map<String,Bean> map; private Map<Bean,Bean> mapBean; private Properties pro; public PojoBean(String name, Bean bean) { this.name = name; this.bean = bean; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Bean getBean() { return bean; } public void setBean(Bean bean) { this.bean = bean; } public Integer getNum() { return num; } public void setNum(Integer num) { this.num = num; } public Bean getBean1() { return bean1; } public void setBean1(Bean bean1) { this.bean1 = bean1; } public List<String> getListStr() { return listStr; } public void setListStr(List<String> listStr) { this.listStr = listStr; } public List<Bean> getListBean() { return listBean; } public void setListBean(List<Bean> listBean) { this.listBean = listBean; } public Set<String> getSetStr() { return setStr; } public void setSetStr(Set<String> setStr) { this.setStr = setStr; } public Set<Bean> getSetBean() { return setBean; } public void setSetBean(Set<Bean> setBean) { this.setBean = setBean; } public Map<String, Bean> getMap() { return map; } public void setMap(Map<String, Bean> map) { this.map = map; } public Map<Bean, Bean> getMapBean() { return mapBean; } public void setMapBean(Map<Bean, Bean> mapBean) { this.mapBean = mapBean; } public Properties getPro() { return pro; } public void setPro(Properties pro) { this.pro = pro; } @Override public String toString() { return "PojoBean{" + "name='" + name + '\'' + ", bean=" + bean + ", num=" + num + ", bean1=" + bean1 + ", listStr=" + listStr + ", listBean=" + listBean + ", setStr=" + setStr + ", setBean=" + setBean + ", map=" + map + ", mapBean=" + mapBean + ", pro=" + pro + '}'; } }
<?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--> <bean id = "bean" class = "com.example.spring.ioc.Bean" name = "bean_1,bean_2"/> <!--别名--> <alias name="bean" alias="bean1" /> <!--通过静态方法实例化bean--> <bean id = "bean2" class = "com.example.spring.ioc.Bean2Factory" factory-method="getBean" /> <!--通过实例方法实例化bean--> <bean id = "bean3Factory" class="com.example.spring.ioc.Bean3Factory" /> <bean id = "bean3" class="com.example.spring.ioc.Bean3" factory-bean="bean3Factory" factory-method="getBean" /> <!--注入bean的方式--> <bean id = "pojoBean" class="com.example.spring.ioc.PojoBean"> <constructor-arg index="0" value="gavin" type="java.lang.String" name="name"/> <constructor-arg index="1" ref="bean" type="com.example.spring.ioc.Bean" name="bean"/> <property name="bean1" ref="bean" /> <property name="num" value="0" /> <property name="listBean" > <list> <ref bean="bean"/> </list> </property> <property name="listStr"> <list> <value>str1</value> <value>str2</value> </list> </property> <property name="setBean"> <set> <ref bean="bean"/> </set> </property> <property name="setStr"> <set> <value>setStr1</value> <value>setStr2</value> </set> </property> <property name="map"> <map> <entry key="key1" value-ref="bean" /> </map> </property> <property name="mapBean"> <map> <entry key-ref="bean" value-ref="bean"/> </map> </property> <property name="pro"> <props> <prop key="proKey" >proValue</prop> </props> </property> </bean> </beans>
注入bean的方式
注入Bean方式介绍
代码回顾:
注入内部Bean
代码回顾:
注入null值
代码回顾:
集合类型Bean注入,Map和Propertyes
代码回顾:
集合类型Bean注入,List和Set
代码回顾:
通过Set方法注入Bean,包括p标识
代码回顾:
通过构造方法注入Bean,用c标识
代码回顾:
通过构造方法注入Bean
注入时创建内部Bean:
即不另外设立<bean></bean>创建
<bean class="com.imooc.spring.ioc.class006.AnotherBean" id="anotherBean"/>
而是在设值的时候加入<bean />创建
<property name="anotherBean1"> <bean class="com.imooc.spring.ioc.class006.AnotherBean"/> </property>
null值注入:
<property name="anotherBean2"> <null/> </property>
集合类Bean的型注入(Properties):
在原有的<bean></bean>中加入
<property name="properties"> <props> <prop key="aaaaa">bbbbb</prop> </props> </property>
集合类Bean的型注入(Map):
在原有的<bean></bean>中加入
<property name="stringMap"> <map> <entry key="ccccc" value="ddddd"/> <entry key="eeeee" value="fffff"/> </map> </property> <property name="AnotherBeanMap"> <map> <entry key-ref="anotherBean" value-ref="anotherBean"/> </map> </property>
注意:当输入是特殊值时,需要在标签加上“-ref”
集合类Bean的型注入(Set):
在原有的<bean></bean>中加入
<property name="stringSet"> <set> <value>aaaaa</value> <value>bbbbb</value> </set> </property> <property name="AnotherBeanSet"> <set> <ref bean="anotherBean"/> <ref bean="anotherBean"/> </set> </property> <property name="AnotherBeanList"> <list> <ref bean="anotherBean"/> <ref bean="anotherBean"/> </list> </property>
集合类Bean的型注入:
在类中创建需要用到的集合。
集合类Bean的型注入(List):
在原有的<bean></bean>中加入
<property name="stringList"> <list> <value>aaaaa</value> <value>bbbbb</value> </list> </property> <property name="AnotherBeanList"> <list> <ref bean="anotherBean"/> <ref bean="anotherBean"/> </list> </property>
通过Set方法注入Bean(简单写法):
在spring.xml的头的xmlns:xsi下方加入(使用c、p命名空间)
xmlns:c="http://www.springframework.org/schema/c" xmlns:p="http://www.springframework.org/schema/p"
然后,将原有的<bean />修改为
<bean class="com.imooc.spring.ioc.class006.Bean" id="bean" c:anotherBean-ref="anotherBean" c:string="ccccc" p:anotherBean1-ref="anotherBean" p:string="ddddd"/>
通过Set方法注入Bean:
在<bean class="com.imooc.spring.ioc.class006.Bean" id="bean"></bean>中加入<property />标签。自动调用类的set方法,进行设置。如:
<property name="anotherBean1" ref="anotherBean"/> <property name="string1" value="bbbb"/>
注意:使用该方法,必须先有构造函数,可以保留上一种方法中的构造方法,也可在Bean的类中添加空的构造方法。
通过构造方法注入Bean:
由于没有默认的构造方法,需要按照现有的构造方法填充参数。
<construcrtor-arg index="第几个参数" name="当前参数的参数名"
type="参数类型(需要从包名开始)"
value="针对简单的数据类型" 或 ref="复杂类型,此处填需要的Bean的BeanId" 两者其中一种>
本节课课程内容
通过构造方法注入Bean
通过Set方法注入Bean
集合类Bean的型注入
null值注入
注入时创建内部Bean
set方法注入bean,包括简写方式
构造方法注入的简单写法:
备注:要在头部地区增加C标识和P标识
xmlns:c="http://www.springframework.org/scheam/c"
构造方法注入bean