4

In my spring project we are using web dependency contains a configuration applicationContext.xml and security-config.xml. I have to change on both of them in my application how can I achieve this.

I'm expecting like this

compile (librariesdependencyname) {
        exclude ('applicationContext.xml')
        exclude ('common-security-config.xml')
    }
Araf
  • 510
  • 8
  • 32
  • 1
    Would you share more details on your Gradle configuration? What's the current outcome? – Ale Zalazar Mar 23 '22 at 19:50
  • How are these files loaded in spring context? Is there a `@Configuration` class in jar which uses `@ImportResource`? – Smile Mar 24 '22 at 07:54
  • @AleZalazar we are importing some one jar that jar have configration class along with config xml. We have to use the dependency without rewriting the so many code. We doesn't want the configartion setup from dependency jar – Araf Mar 25 '22 at 13:18
  • @Smile we are importing some one jar that jar have configration class along with config xml. We have to use the dependency without rewriting the so many code. We doesn't want the configartion setup from dependency jar – Araf Mar 25 '22 at 13:18

3 Answers3

2

If your team handling the jar. Just ask them to creating new version and without this configuration and resource files

OR

You can modify the context loader loaction and patter like below

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext.xml,classpath*:/**/*-config.xml</param-value>
    </context-param>
Araf
  • 510
  • 8
  • 32
1

More information about the project setup will help with more accurate suggestions. There can be multiple way to achieve.

Assuming you have following

  1. Library has mentioned xml files
  2. We want the code not the configurations

As @Araf mentioned, we can request the team to update the library and change the configuration

  1. Restructure the code to provide Autoconfigurations files this way consumer of library can choose to load the configurations through autoload or not
  2. Define your own xml files to load so that it will not load library configurations

One way to achieve AutoConfiguration mechanism mentioned at https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.developing-auto-configuration or as it is your own project you can try multi module project https://www.baeldung.com/spring-boot-multiple-modules

Choosing which package to load or exclude, we can use @ComponentScan https://www.baeldung.com/spring-component-scanning.

XML version of annotations should be available, I personally prefer the annotations as it will use code as configurations.

Sachin
  • 51
  • 6
1

You can add a custom task in gradle and instead of adding your jar compile "dependency. Define it as "something" dependency. Access it object using configurations."something" . Refer example below

librariesdependency "<<package-librariesdependencyname>>"

jar.dependsOn 'customJar'

task customJar (type: Jar) {
archiveName = 'your-output.jar'
from zipTree(configurations.librariesdependency.singleFile)
exclude 'applicationContext.xml'
}