2014-01-13 23 views
1

我在我的代碼庫中有一個控制器,它的軟件包沒有被組件掃描。這個控制器也沒有在任何XML中的bean中定義。定義了沒有組件掃描或bean定義的控制器

不知何故,控制器正在工作。我猜這是因爲有一些方法可以在Spring中定義一個沒有組件掃描或在bean中定義它的控制器。然而,這個控制器實現了一個名爲AbstractControllerImpl的類,並且Helper實現類正在獲取組件掃描。

幫助程序獲取組件掃描的事實是否意味着控制器也被掃描?或者,如果不是,這個控制器怎麼可能工作?

@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> 

助手impl類(這是得到comp onent掃描):

@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> 
+1

讓我們來看看你的web.xml。你有沒有其他的spring xml上下文文件?你有任何'@ Configuration'類嗎? –

+0

@SotiriosDelimanolis我添加了我的web.xml,刪除了一些servlet映射。如果有什麼特定的東西,我認爲我應該去尋找那些不在其中的東西,讓我知道,我會看看。在我的整個應用程序中沒有一個'@ Configuration'註釋。 – user886596

+0

你能告訴我們你的班級路徑掃描配置嗎? –

回答

2

幫助程序獲取組件掃描是否意味着 控制器也被掃描了嗎?或者,如果沒有,這個 控制器如何工作?

不,那是不可能的。

這是不完全清楚你的情況下配置是什麼樣子給你已經

除去幾個servlet映射,並改變了一些名字

但你@Controller類必須在

rest-servlet.xml: 

具有

被裝入上下文某處,可能
<context:component-scan base-package="com.dw.spring3.rest.controller" /> 

這是由DispatcherServlet加載的名爲`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> 

您可以添加一個無參數的構造你的@Controller類,放在一個斷點,看堆棧跟蹤並確定在上下文中被初始化。


請注意,AnnotationMethodHandlerAdapter已在Spring 3.2中棄用。考慮使用<mvc:annotation-driven>來配置您的MVC環境。

+0

好吧,我只是採取了你的建議,並添加了斷點。以下是堆棧跟蹤的樣子:http://i.imgur.com/mMoFBrK.png。這是否意味着它必須在XML中定義,因爲它包含XMLWebApplicationContext調用? – user886596

+0

@ user886596是的,那是你的第一個提示。我相信你可以選擇'XMLWebApplicationContext'方法,它會帶你到那個方法被阻塞的地方。您可以檢查該對象的屬性是否包含「位置」,它會告訴您它是哪個文件。 –

相關問題