Spring 简明教程
Spring - Injecting Inner Beans
众所周知,Java 内部类在其他类的范围内内定义,类似地, inner beans 是在另一个 bean 的范围内内定义的 bean。因此,<property/> 或 <constructor-arg/> 元素中的 <bean/> 元素被称为内部 bean,如下所示。
As you know Java inner classes are defined within the scope of other classes, similarly, inner beans are beans that are defined within the scope of another bean. Thus, a <bean/> element inside the <property/> or <constructor-arg/> elements is called inner bean and it is shown below.
<?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>
Example
让我们启动 Eclipse IDE 并按照以下步骤创建一个 Spring 应用程序 −
Let us have working Eclipse IDE in place and follow the following steps to create a Spring application −
Steps |
Description |
1 |
Create a project with a name SpringExample and create a package com.tutorialspoint under the src folder in the created project. |
2 |
Add required Spring libraries using Add External JARs option as explained in the Spring Hello World Example chapter. |
3 |
Create Java classes TextEditor, SpellChecker and MainApp under the com.tutorialspoint package. |
4 |
Create Beans configuration file Beans.xml under the src folder. |
5 |
The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below. |
以下为 TextEditor.java 文件的内容 −
Here is the content of TextEditor.java file −
package com.tutorialspoint;
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 的内容 −
Following is the content of another dependent class file SpellChecker.java −
package com.tutorialspoint;
public class SpellChecker {
public SpellChecker(){
System.out.println("Inside SpellChecker constructor." );
}
public void checkSpelling(){
System.out.println("Inside checkSpelling." );
}
}
以下是 MainApp.java 文件的内容−
Following is the content of the MainApp.java file −
package com.tutorialspoint;
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 的注入但使用 inner beans 进行了配置 −
Following is the configuration file Beans.xml which has configuration for the setter-based injection but using inner beans −
<?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.tutorialspoint.TextEditor">
<property name = "spellChecker">
<bean id = "spellChecker" class = "com.tutorialspoint.SpellChecker"/>
</property>
</bean>
</beans>
完成源文件和 Bean 配置文件创建后,我们运行该应用程序。如果您的应用程序一切正常,它将打印以下消息−
Once you are done creating the source and bean configuration files, let us run the application. If everything is fine with your application, it will print the following message −
Inside SpellChecker constructor.
Inside setSpellChecker.
Inside checkSpelling.