Java的Spring框架中bean的继承与内部bean的注入
bean的定义继承
bean定义可以包含很多的配置信息,包括构造函数的参数,属性值,比如初始化方法,静态工厂方法名等容器的具体信息。
子bean定义从父定义继承配置数据。子的定义可以覆盖一些值,或者根据需要添加其他。
spring bean定义继承无关,与java类的继承,但继承的概念是一样的。你可以定义一个父bean定义为模板和其他孩子bean可以从父bean继承所需的配置。
当使用基于xml的配置元数据,指明一个子bean定义使用所在的当前属性指定的父bean作为这个属性的值。
例如:
让我们使用eclipse ide,然后按照下面的步骤来创建一个spring应用程序:
以下是我们定义的“helloworld”豆里面有两个属性message1和message2配置文件beans.xml中。下一步“helloindia”豆已经被定义为“helloworld”的子bean使用parent属性。该子bean继承message2属性原状,并覆盖message1 属性,并引入多一个属性message3。
<?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-3.0.xsd"> <bean id="helloworld" class="com.yiibai.helloworld"> <property name="message1" value="hello world!"/> <property name="message2" value="hello second world!"/> </bean> <bean id="helloindia" class="com.yiibai.helloindia" parent="helloworld"> <property name="message1" value="hello india!"/> <property name="message3" value="namaste india!"/> </bean> </beans>
这里是helloworld.java 文件的内容:
package com.yiibai; public class helloworld { private string message1; private string message2; public void setmessage1(string message){ this.message1 = message; } public void setmessage2(string message){ this.message2 = message; } public void getmessage1(){ system.out.println("world message1 : " + message1); } public void getmessage2(){ system.out.println("world message2 : " + message2); } }
这里是helloindia.java文件的内容:
package com.yiibai; public class helloindia { private string message1; private string message2; private string message3; public void setmessage1(string message){ this.message1 = message; } public void setmessage2(string message){ this.message2 = message; } public void setmessage3(string message){ this.message3 = message; } public void getmessage1(){ system.out.println("india message1 : " + message1); } public void getmessage2(){ system.out.println("india message2 : " + message2); } public void getmessage3(){ system.out.println("india message3 : " + message3); } }
以下是mainapp.java文件的内容:
package com.yiibai; import org.springframework.context.applicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext; public class mainapp { public static void main(string[] args) { applicationcontext context = new classpathxmlapplicationcontext("beans.xml"); helloworld obja = (helloworld) context.getbean("helloworld"); obja.getmessage1(); obja.getmessage2(); helloindia objb = (helloindia) context.getbean("helloindia"); objb.getmessage1(); objb.getmessage2(); objb.getmessage3(); } }
创建完成源代码和bean配置文件,让我们运行应用程序。如果一切顺利,这将打印以下信息:
world message1 : hello world! world message2 : hello second world! india message1 : hello india! india message2 : hello second world! india message3 : namaste india!
如果你在这里看到,我们没有通过message2同时创建“helloindia”的bean,但它通过了,因为bean定义的继承。
bean定义模板:
您可以创建可以在不会花太多功夫被其他子bean定义的bean定义模板。在定义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" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="beanteamplate" abstract="true"> <property name="message1" value="hello world!"/> <property name="message2" value="hello second world!"/> <property name="message3" value="namaste india!"/> </bean> <bean id="helloindia" class="com.yiibai.helloindia" parent="beanteamplate"> <property name="message1" value="hello india!"/> <property name="message3" value="namaste india!"/> </bean> </beans>
父bean不能被实例化它自己,因为它是不完整的,而且它也明确地标记为抽象。当一个定义是抽象的这个样子,它只是作为一个纯粹的模板bean定义,充当子定义的父定义使用。
注入内部bean
正如你所知道的java内部类是其他类的范围内定义的,同样,内部bean是被其他bean的范围内定义的bean。因此<property/>或<constructor-arg/>元素内<bean/>元件被称为内部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" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="outerbean" class="..."> <property name="target"> <bean id="innerbean" class="..."/> </property> </bean> </beans>
例如:
我们使用eclipse ide,然后创建一个spring应用程序,
这里是texteditor.java文件的内容:
package com.yiibai; public class texteditor { private spellchecker spellchecker; // a setter method to inject the dependency. public void setspellchecker(spellchecker spellchecker) { system.out.println("inside setspellchecker." ); this.spellchecker = spellchecker; } // a getter method to return spellchecker public spellchecker getspellchecker() { return spellchecker; } public void spellcheck() { spellchecker.checkspelling(); } }
下面是另外一个相关的类文件spellchecker.java内容:
package com.yiibai; public class spellchecker { public spellchecker(){ system.out.println("inside spellchecker constructor." ); } public void checkspelling(){ system.out.println("inside checkspelling." ); } }
以下是mainapp.java文件的内容:
package com.yiibai; import org.springframework.context.applicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext; public class mainapp { public static void main(string[] args) { applicationcontext context = new classpathxmlapplicationcontext("beans.xml"); texteditor te = (texteditor) context.getbean("texteditor"); te.spellcheck(); } }
以下是配置文件beans.xml文件里面有配置为基于setter 注入,但使用内部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" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- definition for texteditor bean using inner bean --> <bean id="texteditor" class="com.yiibai.texteditor"> <property name="spellchecker"> <bean id="spellchecker" class="com.yiibai.spellchecker"/> </property> </bean> </beans>
创建源代码和bean配置文件来完成,让我们运行应用程序。如果一切顺利,这将打印以下信息:
inside spellchecker constructor. inside setspellchecker. inside checkspelling.