2016-08-19 39 views
0

我一直在這一個上拉我的頭髮數小時。Spring MVC - 以編程方式從appContext.xml加載bean定義

我正在建立一個新的Spring MVC REST servlet,我一直在試圖避免XML定義,並以編程方式進行。

問題:我正在尋找一種方法來從applicationContext.xml加載bean定義,同時仍然啓用@ComponentScan(...)(@ Autowire/context.getBean(...)在後者中工作) 。

我通過谷歌試圖無數的組合(我想,但我可能錯過了一些東西)看了一下,發現這一點有助於: https://www.mkyong.com/spring/spring-mixing-xml-and-javaconfig/ ...只有它沒有(@ImportResource(「類路徑*:的applicationContext 「))。

請記住以下聲明,以@ImportResource沒有任何日誌失敗神器(與搖籃建)的部署:

  • 「ApplicationContext」 中
  • 「類路徑:ApplicationContext」 中

應用程序上下文位於'java'文件夾旁邊(Gradle文件夾結構[src/main/java]): enter image description here

我的根配置:

import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.ComponentScan.Filter; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.FilterType; 
import org.springframework.context.annotation.ImportResource; 
import org.springframework.stereotype.Controller; 

@Configuration 
@ImportResource("classpath:applicationContext.xml") 
@ComponentScan(
     basePackages = { "com.storfoome.backend" }, 
     excludeFilters = {@Filter(classes = { Controller.class }, type = FilterType.ANNOTATION)} 
     ) 
public class ServiceRootConfig { 
} 

我的web配置:

import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.ComponentScan.Filter; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.FilterType; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.servlet.config.annotation.EnableWebMvc; 
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; 
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 

@EnableWebMvc 
@Configuration 
@ComponentScan(
     basePackages = { "com.storfoome.backend" }, 
     useDefaultFilters = false, 
     includeFilters = { @Filter(classes = { Controller.class }, type = FilterType.ANNOTATION) } 
     ) 
public class ServiceWebConfig extends WebMvcConfigurerAdapter { 

    @Override 
    public void addResourceHandlers(ResourceHandlerRegistry registry) { 
     registry.addResourceHandler("*/resources/**").addResourceLocations("/resources/"); 
    } 
} 

我的servlet初始化:

import com.storfoome.backend.framework.rest.config.ServiceRootConfig; 
import com.storfoome.backend.framework.rest.config.ServiceWebConfig; 
import org.springframework.context.ApplicationContextInitializer; 
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; 

public class SofomeServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { 

    private static final String DEFAULT_SERVLET_MAPPING = "/sofome/*"; 

    @Override 
    protected Class<?>[] getRootConfigClasses() { 
     return new Class<?>[] { ServiceRootConfig.class }; 
    } 

    @Override 
    protected Class<?>[] getServletConfigClasses() { 
     return new Class<?>[] {ServiceWebConfig.class}; 
    } 

    @Override 
    protected String[] getServletMappings() { 
     return new String[] { DEFAULT_SERVLET_MAPPING }; 
    } 
} 

從XML與@Autowire bean的聲明豆:

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Component; 

@Component("autowireTest") 
public class AutowireTest { 
    @Autowired 
    private SofomePropertyResource propertyResource; 

    public void printProperty() { 
     System.out.println(propertyResource.getProperty("helloWorld")); 
    } 

    public AutowireTest() { 
    } 

    private String test; 

    public String getTest() { 
     return test; 
    } 

    public void setTest(String test) { 
     this.test = test; 
    } 
} 

我的applicationContext.xml:

<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="sofomeProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> 
     <property name="locations"> 
      <list> 
       <value>classpath*:sofome.properties</value> 
      </list> 
     </property> 
    </bean> 
</beans> 

回答

0

所以,當我發佈這個時,我也解決了這個問題。所以任何人都會爲此而苦苦掙扎。

顯然,與Java代碼相關的所有內容都是正確的。

事情是,與Gradle和它的「戰爭」插件。所以,默認情況下它會查看'resource'文件夾中的所有內容,而不是* .java [需要的引用]。

我感動的applicationContext.xml爲「資源/配置」,推出了搖籃生成過程(注意:我沒有爲戰爭一代編寫任何自定義腳本只是「應用插件:‘戰爭’」):

enter image description here