Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@ImportResource not working

I am new to Spring boot and I want to implement simple web application which renders UI using some Mustache templates.

I have written some Spring components (controllers,DAO,Services) whose package structure is different (not falling under same or sub directory as Spring boor runner class). So I am using @ImportResource annotation to declare spring configuration. But my application is not reading any configuration files.

Please refer below code snippet for more details

  1. Project Structure :

enter image description here

Here not that, Spring boot application launcher classes written in package SpringPropertyReaderApplication.

ApplicationConfiguration class is written under same hierarchical package structure which defined few more configuration using @ImportResource annotation.

  1. Spring boot application launcher class.

package com.example.launcher;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class SpringPropertyReaderApplication {

  public static void main(String[] args) {


      ApplicationContext applicationContext = SpringApplication.run(SpringPropertyReaderApplication.class, args);

      System.out.println("------------------------------------------------");
      System.out.println("-----Printing Bean Definition Names ------------");
      System.out.println("------------------------------------------------");
      for (String name : applicationContext.getBeanDefinitionNames()) {
          System.out.println(name);
      }
      System.out.println("------------------------------------------------");
      System.out.println("--------------------END------------------------");
      System.out.println("------------------------------------------------");
  }
}
  1. ApplicationConfiguration class where I have shared location of other Spring configuration files.

package com.example.launcher.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@Configuration
@ImportResource(locations = {"classpath*:application-context.xml"})
public class ApplicationConfiguration {

  @Bean
  public String someDummyBean1() {
      return "someDummyBean1";
  }
}
  1. Spring Application configuration which is not getting loaded by the container.

Here, I have added component scan annotation to scan my services and controllers which were not in the same package as Spring launcher class.

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-2.5.xsd">


  <context:component-scan
      base-package="com.example" />

  <bean id="xmlStringBean1" class="java.lang.String">
      <constructor-arg value="stringBean1" />
  </bean>

</beans>

I have added code to log of all bean names which are loaded by Spring boot. But I did not find any bean with name "xmlStringBean1" and "myController".

Also note that, this application works fine If i put my Rest controller and other classes under the same package hierarchy as Spring boot launcher class.

like image 623
Gunjan Shah Avatar asked Oct 31 '25 13:10

Gunjan Shah


2 Answers

Since, application-context.xml resides inside the spring folder under src/main/resources. But the @ImportResource annotation defined in ApplicationConfiguration.java tries to scan it just inside the root path. Hence, the file was not detected.

@ImportResource(locations = {"classpath*:application-context.xml"})

to be replaced with

@ImportResource(locations = {"classpath:spring/application-context.xml"}) . 

This will enforce framework to search for application-context.xml inside spring directory (starting from root level).

like image 154
Himanshu Bhardwaj Avatar answered Nov 02 '25 04:11

Himanshu Bhardwaj


I think your spring boot application is not detecting the class annotated with @Configuration, because by default it will try to find the Configuration classes in the same or child packages as the main class. Annotate your SpringPropertyReaderApplication with @ComponentScan, like this:

@SpringBootApplication
@ComponentScan(basePackageClasses = {ApplicationConfiguration.class})
public class SpringPropertyReaderApplication {
    // your code here
}
like image 37
Master Chief Avatar answered Nov 02 '25 05:11

Master Chief



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!