Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #39479 > unrolled thread

Specifying Spring DI XML config file location

Started byArne Vajhøj <arne@vajhoej.dk>
First post2020-06-02 21:25 -0400
Last post2021-01-04 20:17 -0500
Articles 5 — 2 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Specifying Spring DI XML config file location Arne Vajhøj <arne@vajhoej.dk> - 2020-06-02 21:25 -0400
    Re: Specifying Spring DI XML config file location Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2020-06-04 22:31 +0200
      Re: Specifying Spring DI XML config file location Arne Vajhøj <arne@vajhoej.dk> - 2020-06-04 21:07 -0400
        Re: Specifying Spring DI XML config file location Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2020-06-05 23:01 +0200
          Re: Specifying Spring DI XML config file location Arne Vajhøj <arne@vajhoej.dk> - 2021-01-04 20:17 -0500

#39479 — Specifying Spring DI XML config file location

FromArne Vajhøj <arne@vajhoej.dk>
Date2020-06-02 21:25 -0400
SubjectSpecifying Spring DI XML config file location
Message-ID<rb6u6o$b0m$1@gioia.aioe.org>
If I have some code that does not use Spring DI directly but uses
a library that uses Spring DI, then what are my options for
specifying what XML config file that library should use?

Any class and method:

SomeSpringClass.setDefaultConfigURL("...");

?

Any property:

-Dspring.config.url=....

?

Google does not seem to be my friend today.

Arne

[toc] | [next] | [standalone]


#39481

FromDaniele Futtorovic <da.futt.news@laposte-dot-net.invalid>
Date2020-06-04 22:31 +0200
Message-ID<rbblns$ig6$1@dont-email.me>
In reply to#39479
On 2020-06-03 03:25, Arne Vajhøj wrote:
> If I have some code that does not use Spring DI directly but uses
> a library that uses Spring DI, then what are my options for
> specifying what XML config file that library should use?
> 
> Any class and method:
> 
> SomeSpringClass.setDefaultConfigURL("...");
> 
> ?
> 
> Any property:
> 
> -Dspring.config.url=....
> 
> ?
> 
> Google does not seem to be my friend today.
> 
> Arne

https://docs.spring.io/spring/docs/5.3.0-SNAPSHOT/spring-framework-reference/core.html#beans-basics

Don't know the details, but `new ClassPathXmlApplicationContext(...)` or
`new AnnotationConfigApplicationContext(...)` would seem the way to go.

Specifying the location alone (likely) won't help you. You need
something that starts the engine that will try to read those sources --
i.e. the ApplicationContext.

On the other hand if you made your main app a spring-boot app, then you
could simply reference lib's config and be done with it.

-- 
DF.

[toc] | [prev] | [next] | [standalone]


#39482

FromArne Vajhøj <arne@vajhoej.dk>
Date2020-06-04 21:07 -0400
Message-ID<rbc5t6$1f4v$1@gioia.aioe.org>
In reply to#39481
On 6/4/2020 4:31 PM, Daniele Futtorovic wrote:
> On 2020-06-03 03:25, Arne Vajhøj wrote:
>> If I have some code that does not use Spring DI directly but uses
>> a library that uses Spring DI, then what are my options for
>> specifying what XML config file that library should use?
>>
>> Any class and method:
>>
>> SomeSpringClass.setDefaultConfigURL("...");
>>
>> ?
>>
>> Any property:
>>
>> -Dspring.config.url=....
>>
>> ?
>>
>> Google does not seem to be my friend today.
> 
> https://docs.spring.io/spring/docs/5.3.0-SNAPSHOT/spring-framework-reference/core.html#beans-basics
> 
> Don't know the details, but `new ClassPathXmlApplicationContext(...)` or
> `new AnnotationConfigApplicationContext(...)` would seem the way to go.
> 
> Specifying the location alone (likely) won't help you. You need
> something that starts the engine that will try to read those sources --
> i.e. the ApplicationContext.

That is already being done in the library.

I need a way to control where the library find the XML config
file without hacking the library.

Arne

[toc] | [prev] | [next] | [standalone]


#39484

FromDaniele Futtorovic <da.futt.news@laposte-dot-net.invalid>
Date2020-06-05 23:01 +0200
Message-ID<rbebs9$9qs$1@dont-email.me>
In reply to#39482
On 2020-06-05 03:07, Arne Vajhøj wrote:
> On 6/4/2020 4:31 PM, Daniele Futtorovic wrote:
>> On 2020-06-03 03:25, Arne Vajhøj wrote:
>>> If I have some code that does not use Spring DI directly but uses
>>> a library that uses Spring DI, then what are my options for
>>> specifying what XML config file that library should use?
>>>
>>> Any class and method:
>>>
>>> SomeSpringClass.setDefaultConfigURL("...");
>>>
>>> ?
>>>
>>> Any property:
>>>
>>> -Dspring.config.url=....
>>>
>>> ?
>>>
>>> Google does not seem to be my friend today.
>>
>> https://docs.spring.io/spring/docs/5.3.0-SNAPSHOT/spring-framework-reference/core.html#beans-basics
>>
>>
>> Don't know the details, but `new ClassPathXmlApplicationContext(...)` or
>> `new AnnotationConfigApplicationContext(...)` would seem the way to go.
>>
>> Specifying the location alone (likely) won't help you. You need
>> something that starts the engine that will try to read those sources --
>> i.e. the ApplicationContext.
> 
> That is already being done in the library.
> 
> I need a way to control where the library find the XML config
> file without hacking the library.

What exactly is the library doing? Starting an ApplicationContext?
(Presumably from a certain resource? (Is that hardcoded?))

If it's using spring-boot, then you might be able to get away by setting
`spring.config.additional-location=[PATH_TO_YOUR_XML]` (to add to the
default settings) or
`spring.config.location=PATH` (to replace them)
both of which you can either specify via System properties
(-D[key]=[value]) or the process ENV -- in the latter case you need to
format the keys as `SPRING_CONFIG_ADDITIONALLOCATION` and
`SPRING_CONFIG_LOCATION`, respectively. But I don't think you can do
that if it's plain old spring (although it might be worth a try).

Reference for these settings is here:
<https://docs.spring.io/spring-boot/docs/2.3.0.RELEASE/reference/html/appendix-application-properties.html#common-application-properties>

If it's plain old Spring, then if I'm not mistaken the path it will load
from must be specified in the code. If so, you might be able to input
your settings by removing the corresponding resource in the JAR and
providing your own version (since Spring will look for the resource on
the classpath). Your own version might even have precedence, I don't know.

But this is a lot of guesswork on my behalf, because it depends a lot on
what the library does and how. But if it's a library that you need to be
able to configure and that doesn't give you the means to, I would add it
might be worth reconsidering whether you should use it at all.

-- 
DF.

[toc] | [prev] | [next] | [standalone]


#39658

FromArne Vajhøj <arne@vajhoej.dk>
Date2021-01-04 20:17 -0500
Message-ID<5ff3be10$0$690$14726298@news.sunsite.dk>
In reply to#39484
On 6/5/2020 5:01 PM, Daniele Futtorovic wrote:
> On 2020-06-05 03:07, Arne Vajhøj wrote:
>> On 6/4/2020 4:31 PM, Daniele Futtorovic wrote:
>>> On 2020-06-03 03:25, Arne Vajhøj wrote:
>>>> If I have some code that does not use Spring DI directly but uses
>>>> a library that uses Spring DI, then what are my options for
>>>> specifying what XML config file that library should use?
>>>>
>>>> Any class and method:
>>>>
>>>> SomeSpringClass.setDefaultConfigURL("...");
>>>>
>>>> ?
>>>>
>>>> Any property:
>>>>
>>>> -Dspring.config.url=....
>>>>
>>>> ?
>>>>
>>>> Google does not seem to be my friend today.
>>>
>>> https://docs.spring.io/spring/docs/5.3.0-SNAPSHOT/spring-framework-reference/core.html#beans-basics
>>>
>>>
>>> Don't know the details, but `new ClassPathXmlApplicationContext(...)` or
>>> `new AnnotationConfigApplicationContext(...)` would seem the way to go.
>>>
>>> Specifying the location alone (likely) won't help you. You need
>>> something that starts the engine that will try to read those sources --
>>> i.e. the ApplicationContext.
>>
>> That is already being done in the library.
>>
>> I need a way to control where the library find the XML config
>> file without hacking the library.
> 
> What exactly is the library doing? Starting an ApplicationContext?
> (Presumably from a certain resource? (Is that hardcoded?))
> 
> If it's using spring-boot, then you might be able to get away by setting
> `spring.config.additional-location=[PATH_TO_YOUR_XML]` (to add to the
> default settings) or
> `spring.config.location=PATH` (to replace them)
> both of which you can either specify via System properties
> (-D[key]=[value]) or the process ENV -- in the latter case you need to
> format the keys as `SPRING_CONFIG_ADDITIONALLOCATION` and
> `SPRING_CONFIG_LOCATION`, respectively. But I don't think you can do
> that if it's plain old spring (although it might be worth a try).
> 
> Reference for these settings is here:
> <https://docs.spring.io/spring-boot/docs/2.3.0.RELEASE/reference/html/appendix-application-properties.html#common-application-properties>
> 
> If it's plain old Spring, then if I'm not mistaken the path it will load
> from must be specified in the code. If so, you might be able to input
> your settings by removing the corresponding resource in the JAR and
> providing your own version (since Spring will look for the resource on
> the classpath). Your own version might even have precedence, I don't know.
> 
> But this is a lot of guesswork on my behalf, because it depends a lot on
> what the library does and how. But if it's a library that you need to be
> able to configure and that doesn't give you the means to, I would add it
> might be worth reconsidering whether you should use it at all.

I never got to reply to this.

My problem was that I had no control over the code using Spring.

my code -> some library -> Spring

I never found a generic solution.

But my specific problem was solved.

The library in question was Apache Ignite accessed via JCache API.

And it turned out that I could just replace:

CacheManager cm = cp.getCacheManager();

with:

CacheManager cm = cp.getCacheManager(new URI("pathtoconfigfile"), null, 
null);

as that first argument with the documentation:

<quote>
an implementation specific URI for the CacheManager
</quote>

turns out to be the URI for the Spring config file with
Apache Ignite.

Arne

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web