如果你使用Spring MVC,想必你已經在web.xml中定義一個servlet來處理請求,如:
<servlet>
<description></description>
<display-name>dispatcher</display-name>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
在這種情況下,你應該有一個名爲像一個Spring配置文件你的web-inf目錄中的dispatcher-servlet.xml。把你的bean定義放在那裏,它們將被定義並在servlet啓動時可用。
編輯:
導入一個豆configuation文件到另一個,從春參考3.2.2.1節:
<beans>
<import resource="services.xml"/>
<import resource="resources/messageSource.xml"/>
<import resource="/resources/themeSource.xml"/>
<bean id="bean1" class="..."/>
<bean id="bean2" class="..."/>
</beans>
自動裝配bean的實例在控制器:
@Controller
public class MyController {
@Autowired
private MyBeanClass myBeanName;
...
}
什麼技術做你用於控制器? – chalimartines 2012-04-19 13:53:10
@Stuttering John,就像GriffeyDog所說的,你不應該使用beanfactory /應用程序上下文來獲得對bean的引用,你應該期待依賴bean被注入到你的控制器中。如果你絕對需要訪問beanfactory,你可以實現org.springframework.context.ApplicationContextAware接口。 – 2012-04-19 14:58:53