在没有组件扫描或bean定义的情况下定义的控制器



我的代码库中有一个控制器,它的包没有经过组件扫描。该控制器也没有在任何XML中的bean中定义。

不知怎么的,控制器在工作。我猜这是因为有某种方法可以在Spring中定义控制器,而无需扫描组件或在bean中定义它。然而,这个控制器实现了一个名为AbstractControllerImpl的类,Helper实现类正在扫描组件。

Helper正在扫描组件这一事实是否意味着Controller也会被扫描?或者,如果没有,这个控制器怎么可能工作?

@Controller
@RequestMapping("/something")
public class SomeController extends AbstractControllerImpl<SomeControllerHelper> {  
//Some request mappings here
}

它扩展的抽象控制器类:

public abstract class AbstractControllerImpl<H extends Helper>
implements Controller<H>
{    
private H helper;    
private BaseValidator validator;
public H getHelper()
{
return helper;
}
public void setHelper(H helper)
{
this.helper = helper;
}   
public void setValidator(BaseValidator validator)
{
this.validator = validator;
}
public BaseValidator getValidator()
{
return validator;
}
public Errors doValidation(Object obj)
{
Errors validationErrors = new BindException(this, "");
if (validator != null)
{
validator.validate(obj, validationErrors);
}
return validationErrors;
}   
}

控制器接口:

public interface Controller<H extends Helper>
{
H getHelper();
}

助手定义:

<?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 id="someHelper" class="com.controller.SomeHelperImpl" />   
</beans>

helper impl类(正在扫描组件):

@Component("SomeControllerHelper")
public class ASomeControllerHelperImpl implements SomeControllerHelper {
//Some methods here
}

编辑:我的web.xml。我删除了一些servlet映射并更改了一些名称,但它看起来是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>SomeApp</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>
com.Log4jLoaderServlet
</listener-class>
</listener>
<context-param>
<param-name>crossContext</param-name>
<param-value>true</param-value>
</context-param>
<!-- This listener will load other application context file in addition to springweb-servlet.xml -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- The context params that read by ContextLoaderListener  -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-application-context.xml</param-value>
</context-param>
<!-- Spring security filter -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> 
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
<servlet>
<servlet-name>rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/servlets/rest-servlet.xml</param-value>
</init-param>       
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rest</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>appservlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/servlets/app-servlet.xml</param-value>
</init-param>       
<load-on-startup>1</load-on-startup>
</servlet>
<error-page>
<error-code>404</error-code>        
<location>/portal/error/notfound/</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/portal/error/internalsystem/</location>
</error-page>
<jsp-config>
<taglib>
<taglib-uri>http://java.sun.com/jsp/jstl/core</taglib-uri>
<taglib-location>/WEB-INF/tags/c.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://jakarta.apache.org/taglibs/unstandard-1.0</taglib-uri>
<taglib-location>/WEB-INF/tld/unstandard.tld</taglib-location>
</taglib>
</jsp-config>   

这是app-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView" />
<property name="order" value="0" />
</bean>
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="order" value="1" />
</bean> 
<bean  class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
<property name="basename" value="views"/>
</bean>
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>                                 
<value>/WEB-INF/views/something/views.xml</value>
<value>/WEB-INF/views/views.xml</value>
</list>
</property>
<property name="preparerFactoryClass"
value="org.springframework.web.servlet.view.tiles2.SpringBeanPreparerFactory"/>
</bean>     
</beans>

rest-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.dw.spring3.rest.controller" />
<!-- To enable @RequestMapping process on type level and method level -->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonConverter" />
</list>
</property>
</bean>
<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json" />
</bean>
</beans>

root-application-cnntext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan
base-package="com.base.notcontroller" />
<import resource="/something/common-context.xml" />          
</beans>

Helper正在扫描组件这一事实是否意味着控制器也被扫描了?或者如果没有,这怎么可能控制器工作?

不,这是不可能的。

考虑到您有,还不完全清楚您的上下文配置是什么样子的

删除了一些servlet映射,并更改了一些名称

但您的@Controller类必须加载到上下文中的某个位置,可能是中

rest-servlet.xml:

其具有

<context:component-scan base-package="com.dw.spring3.rest.controller" />

由名为CCD_ 3的CCD_。

<servlet>
<servlet-name>rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/servlets/rest-servlet.xml</param-value>
</init-param>       
<load-on-startup>1</load-on-startup>
</servlet>

您可以向@Controller类添加一个无参数构造函数,并在中放置一个断点,查看堆栈跟踪并确定它是在哪个上下文中初始化的。


请注意,AnnotationMethodHandlerAdapter在Spring 3.2中已被弃用。考虑使用<mvc:annotation-driven>来配置MVC环境。

相关内容

最新更新