當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring学习笔记-构造和Set方法注入Bean及集合和null值的注入
生活随笔
收集整理的這篇文章主要介紹了
Spring学习笔记-构造和Set方法注入Bean及集合和null值的注入
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
?
?
理論
代碼及演示
?
理論
注入Bean:可以通過構造方法注入Bean,通過Set方法注入Bean;
集合類型有:List、Set、Map、Properties;
以及特殊的null值的注入;
?
通過構造方法注入Bean
<bean id="bean" class="com...Bean"><constructor-arg index="0" name="anotherBean" type="com...AnotherBean" ref="anotherBean /><constructor-arg index="1" name="stringValue" type="java.lang.String" value="stringValue1" /> </bean>?
通過set方法注入Bean
<bean id="bean" class="com...Bean"><property name="anotherBean" ref="anotherBean" /><property name="string" value="setValue1"/> </bean>?
集合類型Bean注入
<bean id="bean" class="com...Bean"><property name="litOrSet"><list> <!-- <set> --><value>aaaa</value><value>bbbb</value></list> <!-- </set> --></property> </bean> <bean id="bean" class="com...Bean"><property name="mapOrProp"><map> <!-- <props> --><entry key="aaa" value="111" /><prop key="aaa">111</prop></map> <!-- </props> --></property> </bean>注入null值
<bean id="bean" class="com..Bean"><property name="nullValue"><null/></property> </bean>?
代碼及演示
程序運行截圖如下:
下面給出txt文本:
bean = Bean{anotherBean=injectbean.demo.AnotherBean@5c84624f, string='通過構造方法注入Bean', anotherBean1=injectbean.demo.AnotherBean@5c84624f, string1='通過set方法注入Bean', stringList=[第一個List值, 第二個List值, 第三個List值], anotherBeanList=[injectbean.demo.AnotherBean@5c84624f, injectbean.demo.AnotherBean@5c84624f], stringMap={key1=value1, key2=value2}, anotherBeanMap={injectbean.demo.AnotherBean@5c84624f=injectbean.demo.AnotherBean@5c84624f}, stringSet=[第一個Set值, 第二個Set值, 第三個Set值], anotherBeanSet=[injectbean.demo.AnotherBean@5c84624f], properties={heheda=dadada}} ---------華麗的分割線--------- bean.getStringList() = [第一個List值, 第二個List值, 第三個List值] bean.getStringSet() = [第一個Set值, 第二個Set值, 第三個Set值] bean.getStringMap() = {key1=value1, key2=value2} bean.getAnotherBeanList() = [injectbean.demo.AnotherBean@5c84624f, injectbean.demo.AnotherBean@5c84624f] bean.getAnotherBeanSet() = [injectbean.demo.AnotherBean@5c84624f] bean.getAnotherBeanMap() = {injectbean.demo.AnotherBean@5c84624f=injectbean.demo.AnotherBean@5c84624f} bean.getProperties() = {heheda=dadada} bean.getAnotherBean2() = null程序結構如下:
源碼如下:
AnotherBean.java
package injectbean.demo;public class AnotherBean {}Bean.java
package injectbean.demo;import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set;public class Bean {@Overridepublic String toString() {return "Bean{" +"anotherBean=" + anotherBean +", string='" + string + '\'' +", anotherBean1=" + anotherBean1 +", string1='" + string1 + '\'' +", stringList=" + stringList +", anotherBeanList=" + anotherBeanList +", stringMap=" + stringMap +", anotherBeanMap=" + anotherBeanMap +", stringSet=" + stringSet +", anotherBeanSet=" + anotherBeanSet +", properties=" + properties +'}';}//通過構造方法注入Beanprivate AnotherBean anotherBean;private String 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;}//通過set方法注入Beanprivate 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;}//集合類型Bean的注入private List<String> stringList;private List<AnotherBean> anotherBeanList;private Map<String, String> stringMap;private Map<AnotherBean, AnotherBean> anotherBeanMap;private Set<String> stringSet;private Set<AnotherBean> anotherBeanSet;private Properties properties;public List<String> getStringList() {return stringList;}public void setStringList(List<String> stringList) {this.stringList = stringList;}public List<AnotherBean> getAnotherBeanList() {return anotherBeanList;}public void setAnotherBeanList(List<AnotherBean> anotherBeanList) {this.anotherBeanList = anotherBeanList;}public Map<String, String> getStringMap() {return stringMap;}public void setStringMap(Map<String, String> stringMap) {this.stringMap = stringMap;}public Map<AnotherBean, AnotherBean> getAnotherBeanMap() {return anotherBeanMap;}public void setAnotherBeanMap(Map<AnotherBean, AnotherBean> anotherBeanMap) {this.anotherBeanMap = anotherBeanMap;}public Set<String> getStringSet() {return stringSet;}public void setStringSet(Set<String> stringSet) {this.stringSet = stringSet;}public Set<AnotherBean> getAnotherBeanSet() {return anotherBeanSet;}public void setAnotherBeanSet(Set<AnotherBean> anotherBeanSet) {this.anotherBeanSet = anotherBeanSet;}public Properties getProperties() {return properties;}public void setProperties(Properties properties) {this.properties = properties;}//null值注入private AnotherBean anotherBean2;public AnotherBean getAnotherBean2() {return anotherBean2;}public void setAnotherBean2(AnotherBean anotherBean2) {this.anotherBean2 = anotherBean2;} }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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean class="injectbean.demo.AnotherBean" id="anotherBean" /><bean class="injectbean.demo.Bean" id="bean"><constructor-arg index="0" type="injectbean.demo.AnotherBean" ref="anotherBean"/><constructor-arg index="1" type="java.lang.String" value="通過構造方法注入Bean" /><property name="anotherBean1" ref="anotherBean" /><property name="string1" value="通過set方法注入Bean" /><property name="stringList"><list><value>第一個List值</value><value>第二個List值</value><value>第三個List值</value></list></property><property name="anotherBeanList"><list><ref bean="anotherBean" /><ref bean="anotherBean" /></list></property><property name="stringSet"><set><value>第一個Set值</value><value>第二個Set值</value><value>第三個Set值</value></set></property><property name="anotherBeanSet"><set><ref bean="anotherBean" /><ref bean="anotherBean" /></set></property><property name="stringMap"><map><entry key="key1" value="value1" /><entry key="key2" value="value2" /></map></property><property name="anotherBeanMap"><map><entry key-ref="anotherBean" value-ref="anotherBean" /></map></property><property name="properties"><props><prop key="heheda">dadada</prop></props></property><property name="anotherBean2"><null/></property></bean> </beans>DemoApplicationTests.java
package injectbean.demo;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class) @SpringBootTest public class DemoApplicationTests {@Testpublic void contextLoads() {ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");Bean bean = context.getBean("bean", Bean.class);System.out.println("bean = " + bean);System.out.println("---------華麗的分割線---------");System.out.println("bean.getStringList() = " + bean.getStringList());System.out.println("bean.getStringSet() = " + bean.getStringSet());System.out.println("bean.getStringMap() = " + bean.getStringMap());System.out.println("bean.getAnotherBeanList() = " + bean.getAnotherBeanList());System.out.println("bean.getAnotherBeanSet() = " + bean.getAnotherBeanSet());System.out.println("bean.getAnotherBeanMap() = " + bean.getAnotherBeanMap());System.out.println("bean.getProperties() = " + bean.getProperties());System.out.println("bean.getAnotherBean2() = " + bean.getAnotherBean2());}}?
總結
以上是生活随笔為你收集整理的Spring学习笔记-构造和Set方法注入Bean及集合和null值的注入的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux学习笔记-生成动态库(补充说明
- 下一篇: Java笔记-Java通过JNI调用Li