gpt4 book ai didi

java - 向旧版 Spring 应用程序添加了组件扫描

转载 作者:行者123 更新时间:2023-12-02 02:55:44 25 4
gpt4 key购买 nike

我有一个遗留的 Spring 应用程序,无论上下文是否完全使用 XML 配置。我想使用 @Controller 注释向应用程序添加一个新 Controller ,以便我开始迁移到使用注释。

作为测试,我已将以下 Controller 添加到应用程序

package uk.co.onevisionconsulting.engineermanager.webapp.controller2;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {

@RequestMapping("/hello")
public String hello(){
return "Hello";
}
}

我还在我的 spring 上下文中添加了 context:component-scan 标签

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans” xmlns:context="http://www.springframework.org/schema/context"
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.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">

<context:component-scan base-package="uk.co.onevisionconsulting.engineermanager" />

<bean name="jsonView"
class="uk.co.onevisionconsulting.engineermanager.webapp.view.JsonView" />

<bean name="csvView"
class="uk.co.onevisionconsulting.engineermanager.webapp.view.CsvView" />

<bean name="ajaxView"
class="uk.co.onevisionconsulting.engineermanager.webapp.view.AjaxView" />

<bean name="dataTablesView"
class="uk.co.onevisionconsulting.engineermanager.webapp.view.DataTablesView" />

<bean id="exceptionResolver"
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="org.springframework.dao.DataAccessException">
dataAccessFailure
</prop>
</props>
</property>
</bean>

<bean id=“someController"
class="uk.co.onevisionconsulting.engineermanager.webapp.controller.SomeController" >
<property name=“someManager" ref=“someManager" />
</bean>

<!-- Loads more controllers -->

<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="2097152" />
</bean>

<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="ApplicationResources" />
<property name="useCodeAsDefaultMessage" value="true" />
</bean>

<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
<!— Some URLs —>
</value>
</property>
<property name="order" value="0" />
</bean>

<bean
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
<property name="order" value="1" />
</bean>

<bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
<property name="order" value="2" />
</bean>

<!-- View Resolver for JSPs -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="requestContextAttribute" value="rc" />
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>

尽管类路径扫描器 (ClassPathScanningCandidateComponentProvider) 拾取了 HelloController,但当我尝试导航到 localhost:8080/hello 时,我得到了 404

谁能看到我还需要做什么?这是我的 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">

<display-name>Engineer Manager</display-name>

<!-- Define the default CSS Theme -->
<context-param>
<param-name>csstheme</param-name>
<param-value>engineermanager</param-value>
</context-param>

<!-- Define the basename for a resource bundle for I18N -->
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>ApplicationResources</param-value>
</context-param>

<!-- Fallback locale if no bundles found for browser's preferred locale -->
<!-- Force a single locale using param-name 'javax.servlet.jsp.jstl.fmt.locale' -->
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.fallbackLocale</param-name>
<param-value>en</param-value>
</context-param>

<!-- Context Configuration locations for Spring XML files -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:/applicationContext-resources.xml
classpath:/applicationContext-dao.xml
classpath:/applicationContext-service.xml
classpath*:/applicationContext.xml
/WEB-INF/applicationContext*.xml
/WEB-INF/security.xml
</param-value>
</context-param>

<filter>
<filter-name>localeFilter</filter-name>
<filter-class>org.appfuse.webapp.filter.LocaleFilter</filter-class>
</filter>
<filter>
<filter-name>securityFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetBeanName</param-name>
<param-value>springSecurityFilterChain</param-value>
</init-param>
</filter>
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
</filter>
<filter>
<filter-name>staticFilter</filter-name>
<filter-class>org.appfuse.webapp.filter.StaticFilter</filter-class>
<init-param>
<param-name>servletName</param-name>
<param-value>dispatcher</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>securityFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>localeFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>*.html</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>*.jsp</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<filter-mapping>
<filter-name>staticFilter</filter-name>
<url-pattern>*.html</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<listener>
<listener-class>org.appfuse.webapp.listener.StartupListener</listener-class>
</listener>
<listener>
<listener-class>org.appfuse.webapp.listener.UserCounterListener</listener-class>
</listener>

<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.json</url-pattern>
</servlet-mapping>

<session-config>
<session-timeout>3600</session-timeout>
</session-config>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>
<error-page>
<error-code>400</error-code>
<location>/index.jsp</location>
</error-page>
<error-page>
<error-code>403</error-code>
<location>/403.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/404.jsp</location>
</error-page>
</web-app>

最佳答案

既然您提到这是一个遗留应用程序,我相信它可以使用现有的配置。

此外,由于您正在尝试使用新 Controller 的注释进行迁移,因此这与注释处理有关。

您需要在 spring 上下文 xml 文件的根 beans 标记中添加下面提到的注释以及 mvc 命名空间配置。

<mvc:annotation-driven/>

配置的部分代码片段如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">

<context:component-scan base-package="uk.co.onevisionconsulting.engineermanager" />
<mvc:annotation-driven/>

...

</beans>

详细原因可以参见Spring MVC: difference between <context:component-scan> and <annotation-driven /> tags?

关于java - 向旧版 Spring 应用程序添加了组件扫描,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43124208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com