【问题标题】:Spring MVC - Load bean definitions from appContext.xml programaticallySpring MVC - 以编程方式从 appContext.xml 加载 bean 定义
【发布时间】:2016-08-19 20:08:00
【问题描述】:

我已经为这个头发拉了几个小时了。

我正在设置一个新的 Spring MVC REST servlet,这次我一直在尝试避免 XML 定义并以编程方式进行。

解决问题:我正在寻找一种从 applicationContext.xml 加载 bean 定义的方法,同时仍然启用 @ComponentScan(...) (@Autowire/context.getBean(...) 在后者中工作)。

我浏览了谷歌,尝试了无数的组合(我想,但我可能错过了一些东西)并找到了一些可以帮助的东西: https://www.mkyong.com/spring/spring-mixing-xml-and-javaconfig/ ...只有它没有 (@ImportResource("classpath*:applicationContext"))。

请记住,以下对 @ImportResource 的声明会导致工件(使用 Gradle 构建)的部署失败,并且没有任何日志:

  • “应用程序上下文”
  • “类路径:applicationContext”

应用程序上下文位于“java”文件夹旁边(Gradle 文件夹结构 [src/main/java]):

我的根配置:

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 {
}

我的网络配置:

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 };
    }
}

带有 @Autowire 的 bean 来自 XML 声明的 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>

【问题讨论】:

    标签: java spring spring-mvc servlets gradle


    【解决方案1】:

    所以,当我发布这个时,我也解决了这个问题。所以对于任何会为此苦苦挣扎的人。

    问题中与 Java 代码相关的所有内容显然都是正确的。

    问题是,使用 Gradle 和它的“战争”插件。因此,默认情况下,它会查看“资源”文件夹中的所有内容,而不是 *.java [需要引用]。

    我将 applicationContext.xml 移至“resource/config”,启动 Gradle 构建过程(注意:我没有为 WAR 生成编写任何自定义脚本。只需“apply plugin: 'war' "):

    【讨论】:

      猜你喜欢
      • 2015-01-31
      • 2016-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-11
      • 2015-09-28
      • 2015-04-07
      相关资源
      最近更新 更多