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
- Project Structure :
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.
- 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("------------------------------------------------"); } }
- 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"; } }
- 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.
