当前位置:首页 > 编程笔记 > 正文
已解决

Spring 依赖注入Spring注入内部bean

来自网友在路上 171871提问 提问时间:2023-10-09 09:01:26阅读次数: 71

最佳答案 问答题库718位专家为你答疑解惑

Spring 依赖注入

 

所谓依赖注入,是指在程序运行过程中,如果需要调用另一个对象协助时,无须在代码中创建按被调用者,而是依赖外部注入。

Spring的依赖注入对调用者和被调用者几乎没有任何要求,完全支持对POJO之间依赖关系的管理。

依赖注入的两种方式:

1)设值注入

设值注入是指通过setter方法传入被调用者的实例。这种注入方式简单直观,在Spring的依赖注入大量使用。

代码示例如下:

public interface Person{//使用斧子的方法public void useAxe();
}public interface Axe
{//砍方法public void chop();
}
//Person的实现类代码
public class Chinese implements Person
{//面向Axe接口编程private Axe axe;public Chinese(){}//设值注入所需的setter方法public void setAxe(Axe axe){this.axe = axe;}public void useAxe(){System.out.println(axe.chop());}
}//Axe的第一个实现类
public class StoneAxe implements Axe
{public StoneAxe(){}//实现Axe接口的chop方法public String chop(){retutn "石斧砍...";}
}//下面采用Spring的配置文件(关键代码)将Person实例和Axe实例组织在一起
<beans><bean id="chinese" class="Chinese"><property name="axe"><ref local="stoneAxe"/></property></bean><!--定义stoneAxe bean--><bean id="stoneAxe" class="StoneAxe"/>
</beans>//主程序部分
public class BeanTest
{public static void main(String[] args) throws Exception{ApplicationContext ctx = new FileSystemXmlApplicationContext("bean.xml");Person p = (Person)ctx.getBean("chinese");p.useAxe();}
}

 

2)构造注入

所谓构造注入,指通过构造函数来完成依赖关系的设定,而不是通过setter方法。

//对前面Chinese类做相应的修改
public class Chinese implements Person
{private Axe axe;public Chinese(){}//构造注入所需的带参数的构造器public Chinese(Axe axe){this.axe = axe;}public void useAxe(){System.out.println(axe.chop());}
}//此时无须Chinese类里的setAxe方法,在构造Person实例时,Spring为Person实例注入所依赖的Axe实例//以下是构造注入的配置文件的关键代码
<beans><bean id="chinese" class="Chinese"><constructor-arg><ref bean="stoneAxe"/></constructor-arg></bean><bean id="stoneAxe" class="StoneAxe"/>
</beans>

前面2中方式的区别在于创建Person实例中Axe属性的时机不同————设值注入时先创建一个默认的bean实例,然后调用对应的setter方法注入依赖关系;而构造注入则在创建bean实例时,已经完成了依赖关系的注入。建议对于依赖关系无须变化的注入,尽量采用构造注入,而其他的依赖关系的注入,尽量采用设值注入。

 

 

 

Spring注入内部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/beanshttp://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应用程序:

步骤描述1Create a project with a name SpringExample and create a package com.manongjc under the src folder in the created project.2Add required Spring libraries using Add External JARs option as explained in the Spring Hello World Example chapter.3Create Java classes TextEditorSpellChecker and MainApp under the com.manongjcpackage.4Create Beans configuration file Beans.xml under the src folder.5The 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文件的内容:

package com.manongjc;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 spellCheckerpublic SpellChecker getSpellChecker() {return spellChecker;}public void spellCheck() {spellChecker.checkSpelling();}
}

下面是另外一个相关的类文件SpellChecker.java内容:

package com.manongjc;public class SpellChecker {public SpellChecker(){System.out.println("Inside SpellChecker constructor." );}public void checkSpelling(){System.out.println("Inside checkSpelling." );}}

以下是MainApp.java文件的内容:

package com.manongjc;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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><!-- Definition for textEditor bean using inner bean --><bean id="textEditor" class="com.manongjc.TextEditor"><property name="spellChecker"><bean id="spellChecker" class="com.manongjc.SpellChecker"/></property></bean></beans>

创建源代码和bean配置文件来完成,让我们运行应用程序。如果一切顺利,这将打印以下信息:

Inside SpellChecker constructor.
Inside setSpellChecker.
Inside checkSpelling.

 

查看全文

99%的人还看了

猜你感兴趣

版权申明

本文"Spring 依赖注入Spring注入内部bean":http://eshow365.cn/6-17711-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!