Anupam Pawar
Anupam Pawar

Reputation: 231

importing XML config into java config based (annotation) project

I'm working on Spring MVC annotation based application. I have web.xml file entry as follows (using WebConfig.java for configuration):

<servlet>
    <servlet-name>sdsdispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </init-param>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>com.conf.WebConfig</param-value>
    </init-param>            

    <load-on-startup>1</load-on-startup>
</servlet>

Now when I try to integrate security related XML file I'm facing following error

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'springSecurityFilterChain' is defined at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:638)

I tried to import xml file as follows:

@Configuration
@EnableWebMvc
@ComponentScan("com.stk.controller")
@ImportResource({"securityContext.xml"})
public class WebConfig extends WebMvcConfigurerAdapter {

Screenshot

enter image description here https://i.sstatic.net/BosWG.jpg

Upvotes: 1

Views: 4218

Answers (1)

Karthik Prasad
Karthik Prasad

Reputation: 10004

Add classpath as shown below, provided its in classpath

@ImportResource("classpath:securityContext.xml")

If you have multiple configuration files then

@ImportResource(locations={"classpath:securityContext.xml","file://c:/test-config.xml"})

You can access the file from WEB-INF directory using

@ImportResource("file:**/WEB-INF/securityContext.xml")

However I would recommend you to move configuration file to src/main/resource directory and use the file line that is loading file using classpath. These files will be copied to WEB-INF/classes directory during packing of war by maven which is classpath

Upvotes: 2

Related Questions