【问题标题】:Spring MVC 3.0.5.RELEASE get bean from the controller without scanSpring MVC 3.0.5.RELEASE 从控制器获取 bean 而无需扫描
【发布时间】:2016-07-08 12:06:15
【问题描述】:

我已经定义了这个bean:

@org.springframework.stereotype.Service(value = "deviceService")
public class DeviceServiceImpl implements DeviceService {
...
}

还有这个配置文件

<?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 name="/hello" class="com.tdk.DeviceController"></bean>
   <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/jsp/" />
      <property name="suffix" value=".jsp" />
   </bean>
</beans>

有没有办法在不扫描的情况下从控制器获取 deviceService bean?

【问题讨论】:

    标签: spring spring-mvc spring-ioc


    【解决方案1】:

    如果没有组件扫描,deviceService 将永远不会在应用程序上下文中加载,并且您无法注入甚至不存在的 bean。

    您已经有了一个 dispatcher-servlet.xml,因此您可以使用基于 xml 的方法来定义 deviceService bean。

    <bean name="deviceService" class="com.*.DeviceServiceImpl"></bean>
    

    然后为你的控制器使用属性注入

    <bean name="/hello" class="com.tdk.DeviceController">
    <property name="deviceService" ref="deviceService" />
    </bean>
    

    希望,这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2020-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-27
      • 2013-05-08
      • 1970-01-01
      • 1970-01-01
      • 2020-10-13
      相关资源
      最近更新 更多