在spring的xml配置文件中咱們能夠經過<bean>標籤 進行java bean的注入java
基礎bean類web
package com.zhu.example.beantest; /** * <p>Title: HelloWorldBean</p> * <p>Description: </p> * @author zhukai */ public class HelloWorldBean { private String messageString="Hello World"; HelloWorldBean(){ } HelloWorldBean(String message){ this.messageString=message; } public String getMessageString() { return messageString; } public void setMessageString(String messageString) { this.messageString = messageString; } }
xml文件bean配置spring
<!-- 最基礎的bean配置 --> <bean id="HelloWorldBean" class="com.zhu.example.beantest.HelloWorldBean" ></bean>
這種bean注入採用的是注入類的無參數構造函數進行類的實例化,而且是單例模式的session
<!-- 多實例模式 每次getbean有一個新的實例生成--> <bean id="mutiHelloWorldBean" class="com.zhu.example.beantest.HelloWorldBean" scope="prototype"></bean>
針對web應用程序,還能夠配置爲request、session等範圍。app
注入類函數
package com.zhu.example.beantest; import java.util.Date; /** * <p>Title: ParamConstructorBean</p> * <p>Description:這個類沒有空方法 </p> * @author zhukai */ public class ParamConstructorBean { private String name; private Date date; ParamConstructorBean(String name){ this.name=name; } ParamConstructorBean(Date date){ this.date=date; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } }
這個類有兩個構造函數。測試
xml文件配置this
<!-- 帶參數的構造函數 --> <bean id="ParamConstructorBean" class="com.zhu.example.beantest.ParamConstructorBean"> <constructor-arg> <value>hello</value> </constructor-arg> </bean>
這個注入使用的構造函數是 String 類型爲參數的spa
<!-- 帶參數的構造函數(參數爲一個bean) --> <bean id="bean_date" class="java.util.Date" /> <bean id="ParamConstructorBean" class="com.zhu.example.beantest.ParamConstructorBean"> <constructor-arg index="0" ref="bean_date"> </constructor-arg> </bean>
這個注入 使用構造函數參數爲date的prototype
使用靜態工廠和實例工廠,兩種工廠來注入HelloWorldBean類
靜態工廠
package com.zhu.example.beantest; /** * <p>Title: StaticBeanFactory</p> * <p>Description: </p> * @author zhukai */ public class StaticBeanFactory { public static HelloWorldBean getHelloBean() { return new HelloWorldBean("sta hello world"); } }
實例工廠
package com.zhu.example.beantest; /** * <p>Title: InstanceBeanFactory</p> * <p>Description: </p> * @author zhukai */ public class InstanceBeanFactory { public HelloWorldBean getHelloBean() { return new HelloWorldBean("ins hello world"); } }
xml文件配置
<!-- 工廠模式配置bean --> <!-- 靜態工廠 --> <bean id="HelloWorldBeanfac" class="com.zhu.example.beantest.StaticBeanFactory" factory-method="getHelloBean"/> <!-- 實例工廠 --> <bean id="bean_factory" class="com.zhu.example.beantest.InstanceBeanFactory"/> <bean id="HelloWorldBeanfacIns" factory-bean="bean_factory" factory-method="getHelloBean"/>
<!-- 屬性注入 --> <bean id="HelloWorldBeanPro" class="com.zhu.example.beantest.HelloWorldBean" > <property name="messageString" value="pro 測試 你好!" /> </bean>
只要實現了相似這種get、set方法就等實現成員變量的注入
public String getMessageString() { return messageString; } public void setMessageString(String messageString) { this.messageString = messageString; }
注入完成後的bean取用
首先初始化context
@Before public void setUp() throws Exception { context = new ClassPathXmlApplicationContext(new String[]{"classpath:applicationContext.xml","applicationContext-beantest.xml"}); }
測試類
@Test public void testRegBean() throws Exception { //基礎bean HelloWorldBean basicHelloBean = (HelloWorldBean) context.getBean("HelloWorldBean"); //基礎帶參數構造函數bean ParamConstructorBean basicParamBean = (ParamConstructorBean) context.getBean("ParamConstructorBean"); //靜態工廠模式bean HelloWorldBean facHelloBean = (HelloWorldBean) context.getBean("HelloWorldBeanfac"); //實例工廠模式bean HelloWorldBean facInsHelloBean = (HelloWorldBean) context.getBean("HelloWorldBeanfacIns"); System.out.println(basicHelloBean.getMessageString()); System.out.println(basicParamBean.getDate()); System.out.println(facHelloBean.getMessageString()); System.out.println(facInsHelloBean.getMessageString()); }
輸出以下
Hello World Thu Aug 11 11:11:04 CST 2016 sta hello world ins hello world