java - Spring MVC-以编程方式从appContext.xml加载bean定义

标签 java spring spring-mvc servlets gradle

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

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

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

我浏览了google,尝试了无数种组合(我想,但我可能错过了一些东西),发现了一些可以帮助您的东西:
https://www.mkyong.com/spring/spring-mixing-xml-and-javaconfig/
...只是没有(@ImportResource(“classpath *:applicationContext”))。

请记住,以下对@ImportResource的声明使没有使用日志的 Artifact (使用Gradle构建)的部署失败:

  • “applicationContext”
  • “classpath: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 {
    }
    

    我的网络配置:

    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声明了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代码相关的所有事情显然都是正确的。

    事实是,有了Gradle,这是“ war ”插件。因此,默认情况下,它会查找* .java以外的所有内容的“resource”文件夹。

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

    enter image description here

    关于java - Spring MVC-以编程方式从appContext.xml加载bean定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39046955/

    相关文章:

    java - 使用 Java 将记录插入 Oracle DB

    spring-mvc - Spring MVC Portlet Eclipse 插件

    尽管文件存在,但 java.io.FileNotFoundException

    Java GridBagLayout 更改列大小

    java - 等待 php 脚本然后下载

    spring - 如何将 Spring Batch Admin 与 Spring 3.2 和 @Schedule 注释一起使用?

    java - 在阅读主题后异步提交消息

    java - 如何创建自定义 Web 安全表达式以在 JSP 中使用?

    java - MySQL 和 JDBC 连接池 : unclosed statements

    java - 应用程序监听器中的 Spring 加载应用程序属性