Can We Have Multiple Spring Configuration Files in One Project? - java

Can We Have Multiple Spring Configuration Files in One Project? If yes, can someone provide a working example to support this concept?

Yes, in large projects, having multiple Spring configurations increase maintainability and modularity.
You can load multiple files like this:-
#Configuration
#Import({MainConfig.class, SchedulerConfig.class})
public class AppConfig {
You can also upload one XML file that will contain all configs.
ApplicationContext context = new ClassPathXmlApplicationContext("spring-all.xml");
inside the XML file:-
<import resource="main.xml"/>
<import resource="scheduler.xml"/>

Related

Use several ehcache XML files in same project

I have a project done with Spring framework that uses ehcache. This project has a resource file named xxxx_ehcache.xml . Now this project has a dependency with a library that is in turn using ehcache also, and this library provides a file called yyyy_ehcache.xml with some content that can be embedded in the main xxxx_ehcache.xml.
I want to embed yyyy_ehcache.xml in xxxx_ehcache.xml. In Spring we have this tag:
<import resource="classpath:/someFile.xml" />
But I dont know how to do this using ehcache XSD grammar.

hibernate entity discovery only works inside the same folder

I have a Spring Boot project with Hibernate.
The project does not have a hibernate.cfg.xml file.
The project also does not have an applicationContext.xml file.
Nevertheless, all works well.
However, when I start adding new hibernate entities, then things go wrong. For some reason, the system only finds them when I put them inside the package of the other hibernate entities.
So, this leads me to believe that I do need additional configuration to help the auto-discovery mechanism. But what is the state-of-art in 2020 ? (I assume that the above xml files are now deprecated).
If you use spring + hibernate then it solved by #ComponenScan annotation.
If pure hibernate then I think you need persistence.xml
EntityManager is the class that performs database interactions in JPA.
It is initialized through a configuration file named persistence.xml.
This file is found in the META-INF folder in your CLASSPATH, which is
typically packaged in your JAR or WAR file. The persistence.xml file
contains:
The named "persistence unit," which specifies the persistence framework you're using, such as Hibernate or EclipseLink.
A collection of properties specifying how to connect to your database, as well as any customizations in the persistence
framework
A list of entity classes in your project
I totally overlooked these annotations which were present on the SpringBootApplication class.
#SpringBootApplication(scanBasePackages = {"com.domain.foo.bar.*"})
#EnableJpaRepositories(basePackages ={"com.domain.foo.bar.*"})
#EntityScan(basePackages ={"com.domain.foo.bar.*"})
public class SpringBootApplication extends SpringBootServletInitializer {
}
I needed to add my packages here.

how to load application-context.xml using #ContextConfiguration annotation

My project structure is as below:
src/main/java -> contains java classes
src/main/resources/spring/context/application-context.xml
src/test/java -> contains J-unit test
I would like to use #ContextConfiguration annotation to load my application-context.xml
How can I load this file and how can I make sure that all beans are loaded?
I tried it using classpath and file. But nothing works for me.
I am confused when to use classpath and file. Some one please help me with this.
Thanks in advance.
#ContextConfiguration("classpath:/spring/context/application-context.xml") should work.
In conventional Maven project layout, src/main/resources contains classpath resources, therefore you should use classpath: or no prefix at all, because classpath: should be a default one in this case.
If it still doesn't work, perhaps something is wrong with your project configuration and files from src/main/resources doesn't appear in the classpath.
If context loads successfully, all beans in it should be loaded as well, otherwise context will fail to load.
Try with:
ApplicationContext APPLICATION_CONTEXT = new ClassPathXmlApplicationContext("/spring/context/application-context.xml");
If it does not work, try putting application-context.xml directly in src/main/resources and then load it with
ApplicationContext APPLICATION_CONTEXT = new ClassPathXmlApplicationContext("application-context.xml");

Spring Maven unitTest applicationContext loading wrong file

.I have a project that has a spring-config.xml file in src/main/webapp/WEB-INF and an applicationContext.xml file in src/test/resources. I also have an abstract test base class for my unit tests in src/test/java looks something like:
#ContextConfiguration(locations = {"classpath:/applicationContext.xml"})
public abstract class AbstractTestBase extends AbstractTransactionalJUnit4SpringContextTests {
//Common code and fields
}
All my unit tests extends this AbstractTestBase which points to the context within the src/test/resources or should. The problem arises when running my unit tests it is pulling in the spring-config.xml file.
There are other projects my team is working on that have the same file structure, same app context setup, and run as intended, but even when I have each file in the project side by side I don't see where their file runs and this one doesn't.
I am new to spring so I don't know what it is I should be looking for.
Are there any situations where Spring or Maven would not take the app context I'm handing it given all files exist? Is there anything I might be missing?
EDIT: corrected to reflect that one file is a spring-config file.
"classpath:/applicationContext.xml" should look under src/test/resources.
But it should be noticed that using that syntax will load the first one it finds and then stop as mentioned by '#chrylis'.
I once had similar problem.
You must have been using an IDE. There must have been applicationContext.xml file in your target/test-classes/ (in Eclipse IDE) in your project directory that is a copy of your xml file under src/main/webapp/WEB-INF or xml file like it.

Discover Plugin-JARS in Classpath in Spring

Is there a way in Spring to discover a
"plugin"-JAR from the classpath,
and load its applicationContext.xml dynamicaly?
I have achieved a plugin-like system with Spring by following this approach:
Each plug-in must contain a spring-context file with a specific name and package prefix (for example, com.example.myApp.whatever containing plugin.xml, or applicationContext.xml if you prefer).
For the plug-in to be detected in the classpath, the host application should dynamically import all the context files contributed by any jar following the previos scheme. This is achieved with a wildcard-based import in spring config:
<import resource="classpath*:/com/example/myApp/**/plugin.xml" />
Provided that each plug-in defines beans of a known interface (e.g., MyInterface). The host application can define a property of type List <MyInterface> and define the bean as autowire="byType" in order to retrieve all the beans of the MyInterfaceType in a list.

Categories