![](/img/trans.png)
[英]Spring Dependency Injection via @Configuration, @Bean and @Component
[英]Spring configuration for dependency injection
我正在通过Struts2学习spring依赖注入,这是在一个Web项目上进行的。 在我的示例中,我创建了一个有动物的动物园 。 如果注射成功,动物会说话。 例如在控制台中,我们将看到狗的谈话:
Wowowo ฅ^•ﻌ•^ฅ
但是,如果注入失败,那么我们将看到:
zooService bean has not been injected.
这是我的应用程序的体系结构:
com.zoo.controller.ZooController
是用于接收Web操作的控制器。 com.zoo.service.ZooService
是动物对话的界面 com.zoo.service.ZooServiceForDog
是狗说话的实现 到步骤为止,一切正常。 Spring通过使用名为applicationContext.xml
的XML文件处理依赖项注入。 但是,我对此文件有2种配置类型,第一种Config 1有效,而第二种Config 2无效。
使用配置1 注入成功 。
<?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.xsd">
<bean id="zooService" class="com.zoo.service.ZooServiceForDog" />
</beans>
使用配置2 注入失败 。
<?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.xsd">
<bean id="zooController" class="com.zoo.controller.ZooController">
<property name="zooService" ref="zooServiceBean" />
</bean>
<bean id="zooServiceBean" class="com.zoo.service.ZooServiceForDog" />
</beans>
有人可以解释为什么Config 2无法正常工作吗?
这是其他可能有助于解决此问题的代码:
com.zoo.controller.ZooController
类:
package com.zoo.controller;
import com.zoo.service.ZooService;
import com.opensymphony.xwork2.ActionSupport;
public class ZooController extends ActionSupport {
private static final long serialVersionUID = 1L;
private ZooService zooService;
public String live () {
if (zooService != null) {
zooService.talk();
} else {
System.out.println("zooService bean has not been injected.");
}
return SUCCESS;
}
public ZooService getZooService() {
return zooService;
}
public void setZooService(ZooService zooService) {
this.zooService = zooService;
}
}
它不能工作,因为zooController
的范围是单例。 您应该使范围prototype
。
<bean id="zooController" class="com.zoo.controller.ZooController" scope="prototype" >
<property name="zooService" ref="zooServiceBean" />
</bean>
依赖项管理由容器定义:
如果您的操作由Struts容器管理,则Struts会在
default
范围内创建它们。 如果您的操作是由Spring容器管理的,那么您需要定义操作bean的范围,因为默认情况下,Spring使用singleton
范围,并且如果您不想在用户请求之间共享操作bean,则应该定义相应的范围。 您可以使用prototype
作用域,这意味着每次构建Struts动作实例时,Spring都会返回一个新实例。
Struts通过插件集成到Spring。 确保它有
<constant name="struts.objectFactory" value="spring" />
然后可以将动作委派给Spring
参考文献:
编辑:
在第一个配置中,您声明了一个bean zooService
,它将由Struts使用spring
对象工厂注入。
在第二个配置中,您声明了两个bean zooController
和zooServiceBean
,但是您更改了第二个bean的名称。 然后,您尝试像第一种情况一样使用spring
对象工厂来构建action bean。 并且因为没有名称为zooService
的bean,所以自动装配已失败。 因为默认情况下,Struts配置为按名称从应用程序上下文自动装配bean。
然后,您更改了struts.xml
并在操作类属性中使用了bean引用。 这意味着Struts将使用应用上下文从Spring获取Bean。 并且由于它对第二个bean有明确的依赖关系,因此将在返回该bean之前进行连接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.