簡體   English   中英

Spring MVC-以編程方式從appContext.xml加載bean定義

[英]Spring MVC - Load bean definitions from appContext.xml programatically

我已經在這上面拉了好幾個小時了。

我正在設置一個新的Spring MVC REST servlet,並且這次我一直在嘗試避免XML定義並以編程方式進行。

問題所在:我正在尋找一種從applicationContext.xml加載bean定義的方法,同時仍啟用@ComponentScan(...)(@ Autowire / context.getBean(...)在后者中起作用)。

我瀏覽了Google,嘗試了無數種組合(我想,但我可能錯過了一些東西),發現了一些可以幫助您的東西: https : //www.mkyong.com/spring/spring-mixing-xml-and-javaconfig/ .. 。只有它沒有(@ImportResource(“ classpath *:applicationContext”))。

請記住,以下對@ImportResource的聲明使沒有任何日志的工件(由Gradle構建)的部署失敗:

  • “ applicationContext”
  • “類路徑: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 };
    }
}

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,這是“戰爭”插件。 因此,默認情況下,它會查找* .java以外的所有內容的“ resource”文件夾。

我將applicationContext.xml移至“ resource / config”,啟動了Gradle構建過程(注意:我沒有為WAR生成編寫任何自定義腳本。只需“ apply plugin:'war' “):

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM