依赖注入的Spring配置

时间:2016-03-06 11:13:30

标签: java spring dependency-injection struts2

我在Struts2上学习弹簧依赖注入,基于web项目。在我的例子中,我创建了一个有动物的动物园。如果注射成功,动物会说话。例如。在控制台中,我们将看到狗的谈话:

Wowowo ฅ^•ﻌ•^ฅ

但是,如果注射失败,那么我们会看到:

zooService bean has not been injected.

这是我的应用程序的架构:

application architecture

  • com.zoo.controller.ZooController是接收网络操作的控制器。
  • com.zoo.service.ZooService是动物谈话的界面
  • com.zoo.service.ZooServiceForDog是狗的谈话实施

问题

直到步骤,一切正常。 Spring使用名为applicationContext.xml的XML文件处理依赖注入。但是,我为此文件配置了两种类型,第一种配置1 有效但第二种配置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;
    }
}

1 个答案:

答案 0 :(得分:0)

它无法工作,因为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范围,如果你不想在用户和#39;之间共享动作bean。请求您应该定义相应的范围。您可以使用prototype范围,这意味着每次Struts构建一个动作实例时,Spring都会返回一个新实例。

Struts通过插件集成到Spring。确保它有

<constant name="struts.objectFactory" value="spring" />

然后你可以将动作委托给Spring

<强>参考文献:

修改

在您的第一个配置中,您声明了一个bean zooService,它将由Struts使用spring对象工厂注入。

在第二个配置中,您声明了两个bean zooControllerzooServiceBean,但您更改了第二个bean的名称。然后你尝试使用spring对象工厂构建动作bean,就像在第一种情况下一样。因为没有名称为zooService的bean,自动装配失败了。因为默认情况下,Struts配置为按名称从应用程序上下文自动装配bean。

然后你改变了struts.xml并在action class属性中使用了bean引用。这意味着Struts将使用app context从Spring获取bean。并且因为它具有对第二个bean的显式依赖性,所以在返回bean之前它将被连线。