【问题标题】:Spring + Spring MVC dependency injection without using @Autowire不使用 @Autowire 的 Spring + Spring MVC 依赖注入
【发布时间】:2014-12-03 06:31:01
【问题描述】:

我正在使用 Spring 和 Spring MVC 构建应用程序。对我来说,使用 @Autowired 不是一种选择(是一种要求)。

我已经构建了一个 web.xml 文件,例如:

<!-- Spring context -->
<context-param>
<param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Spring MVC -->
<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>namespace</param-name>
        <param-value>mvc-dispatcher-context</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

所以我加载了 Spring 上下文和 Spring MVC Servlet。

我的 mvc-dispatcher-context.xml 文件看起来像:

<beans ...>

    <mvc:annotation-driven />

    <context:component-scan base-package="com.pacage.controller"/>

    <mvc:resources mapping="/resources/**" location="/WEB-INF/static/" />

    <bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/pages/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
    </bean>
</beans>

所以我可以在我的控制器中使用@Controller 注解...

在我的 applicationContext.xml 上我定义了我的 Spring bean,例如我的服务在这种情况下是“OneService.java”

我有一个控制器:

@Controller
@RequestMapping("/main")
public class MainController {

    private OneService oneService;
    //getters and setters
    ...
}

我想注入在 applicationContext.xml 文件中定义的 OneService。 我尝试了几种方法,但无法正常工作,在控制器中使用我的服务时总是收到 NullPointerException。

有没有办法在这个 mvc servlet 中使用 applicationContext.xml 中定义的 bean?

[更新]

如果我在我的 servlet 中定义了所有的 applicatonContext,我就无法让它工作。有没有办法让 servlet 上下文成为应用程序上下文的子级,以便能够使用应用程序上下文 bean?

【问题讨论】:

  • “是否有要求”?如果您使用的是 @Controller,那么您已经在使用 Spring 特定的注释,但当然 @Inject 始终是可接受的替代方案。
  • 我可以不使用注释来做到这一点吗?
  • 理论上可以,但您也可以用 C 编写应用程序。注解是 Web 开发中的最佳实践,而且有充分的理由。
  • 我知道这是最好的做法,但使用注释并不取决于我...
  • 您需要在控制器中为您的服务定义 getter/setter。如果正确定义了 bean,则应使用 Setter 进行接线。另外我不明白为什么不应该使用@Autowire?去@Resource然后...

标签: spring spring-mvc dependency-injection


【解决方案1】:

应用程序上下文已经是“父亲”,因此在其中定义的 bean 可用于在其子项中定义的 bean。但如果需要,您可以在应用程序上下文中定义两者。 问题可能是您尝试注入服务的方式。

如果您将@Autowired 添加到您的服务中,它将起作用,因为该bean 在您的控制器中可用,但是如果您不想使用注释,那么您需要在您的xml 上下文中显式创建和注入bean。 首先,您需要在控制器中添加一个 setter:

public void setOneService(OneService oneService) {
    this.oneService = oneService;
}

然后在你的应用程序上下文中你会这样做:

<bean id="mainController" class="com.package.controller.MainController">
    <property name="oneService" ref="oneService" />
</bean>
<bean id="oneService" class="com.package.service.OneService">
</bean>

您仍然可以这样做,并将服务声明保留在应用程序上下文中,并将控制器保留在 servlet 上下文中。

【讨论】:

    猜你喜欢
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多