How to override spring context place holder of dependency jar? - java

I have the below configuration in xml of an maven dependency jar.
context:property-placeholder location="file:/dir/project.properties"/>
Unfortunately I can't create that folder in my machine and can't modify dependency code too. So when I am running the parent app, I want to override the location of properties file or provide external properties file.
Both parent and child are developed using Spring Core. (It's not Spring Boot). Parent is War and child is jar.

According to Spring Context lifecycle, if it uses Spring Cloud Context, the Spring application first loads configuration from bootstrap.properties, then starts and then loads application.properties (in your case project.properties).
You can add to the project dependencies the Spring Cloud dependency. Specifying classpath (or using default config folder) you can have properties in another place.

Related

Using multiple log4j configuration files

I have an application which is deployed as jar and is executed by some other parent application. The parent application has defined a log4j configuration file and the file is mentioned while starting the parent application.
When I am trying to use log4j instance for child application, its configuration file is never picked. How can I use a different configuration file for my application without impacting configuration file of the parent application?

Dependency Jar not referring to its own application.properties

I created 2 spring boot applications.
I used 1 of these jars in the other as a dependency.
Now, both these applications have their own db configurations in their application.properties file respectively.
But when I run the parent project and access classes which use the dependency jar.
The properties defined in the dependency project are not found and the dependency project refers the parent project's (application in which the dependency is used) application.properties file.
How can I make the dependency maven project to use its own application.properties file.
If you are using these dependencies as normal jars, you are probably misunderstanding the way classloaders work.
At any given time you can consider all files provided by the jars on the classpath to be thrown in together in a single large pool (scope/namespace). This mean that if you have TWO "application.properties" files from two different jars, only one of them will be visible.
You need to rethink your deployment scheme so your dependencies does not bring in their own "application.properties" files but they are baked in as part of your deployment packaging.

How does Karaf know which spring configuraton files to load

I have been working on OSGI and Spring based applications for last few months and only now I have noticed that I don't specify which XML files to load for spring configuration. So how does Karaf know which files to load?
My project is Maven based and it uses maven-bundle-plugin to build OSGI bundle. As project is maven based it follows simple maven directory structure like below.
src/
main/java/
main/resources/ ---/here I'm keeping my XML configuratoin files.
pom.xml
Karaf uses spring dynamic modules (spring dm) internally to work with spring based bundles. It looks at following locations in your bundle to check for configuration files.
META-INF/MANIFEST.MF (if Spring-Context attribute is present)
META-INF/spring (if it contains XML files)
Reference http://uniqueexperiments.blogspot.com/2015/09/spring-osgi-bundle-how-does-karaf-know.html
So short answer is it has predefined locations and if you don't put files in there it won't load them.

test folder configuration in web project

I have a spring web project in eclipse which has folder structure as
src/main/java
src/main/resources
src/test/java
src/test/resources
For some time my eclipse project used to work fine. But recently, when i start the application my eclipse project loads spring annotated beans from
src/test/java
folder.Because of this i am getting
org.springframework.beans.factory.NoUniqueBeanDefinitionException
from spring as there are two implementations of the same spring bean.
When i remove this folder from build path my application works well. Can someone please help with the reason for such behavioral change in my project?
we have two types of ioc containers in spring BeanFactory,ApplicationContext.when a request comes bean factory creates the instance which we declare a bean in xml. but application context creates all bean instances at the time of loading....

Add shared project jars in websphere shared library

We can add project dependency jars (like spring jars, logging etc) in shared library, can we add shared project jars (like one that communicate with Database) in shared library?
Scenario:
I have these projects
WebApp
WebAppEAR
CommonDB
I am using IBM Websphere server, hibernate, spring mvc
I added all spring, hibernate jars including CommonDB.jar in Websphere shared library. WebApp Application is running successfully with spring controllers but when my application try to hit DB it throws exception.
Caused by: java.lang.IllegalStateException:
Actually WebApp is not loading <bean> DataSource. DataSource class is located in CommonDB project.
When I include CommonDB.jar in Deployment assembly of EAR WebApp runs successfully and fetches data from DB.
I want to exclude CommonDB.jar from Deployment assembly and only want to add it in shared library.
Adding jars in shared library will work. Try figuring out which beans are not initialized and add them in Spring file to initialize. Also check other bean properties for dependency.

Categories

Resources