我的代码库中有一个控制器,其包不是组件扫描的。此控制器也未在任何XML中的bean中定义。
不知何故,控制器正在工作。我猜这是因为有一些方法可以在Spring中定义一个控制器而无需组件扫描或在bean中定义它。但是,此控制器实现了一个名为AbstractControllerImpl的类,并且Helper实现类IS正在扫描组件。
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;
}
}
Controller接口:
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>
休息-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>
根应用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>
答案 0 :(得分:2)
Helper正在扫描组件的事实是否意味着 控制器也被扫描了?或者如果没有,这怎么可能呢 控制器有效吗?
不,这是不可能的。
鉴于您已经
,您的上下文配置看起来并不完全清楚删除了一些servlet映射并更改了一些名称
但是你的@Controller
类必须加载到某个上下文中,可能在
rest-servlet.xml:
有
<context:component-scan base-package="com.dw.spring3.rest.controller" />
由名为DispatcherServlet
的{{1}}加载。
`rest
您可以在<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>
类中添加一个无参数构造函数并放入一个断点,查看堆栈跟踪并确定它在哪个上下文中被初始化。
请注意,{3.2}已弃用@Controller
。考虑使用AnnotationMethodHandlerAdapter
配置MVC环境。