Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set arrays of string to @EnableJpaRepositories from property files

I have a jpa configuration file with @EnableJpaRepositories annotaion. I set this annotaion value from application.properties file like this :

@EnableJpaRepositories("${jpa.repository.packages}")
public class JPAConfiguration {
    ....
}

and here is my application.properties file:

jpa.repository.packages=com.epms.model

and it works perfect. but i want to specify multiple packages for @EnableJpaRepositories . so i changed my config file to this :

jpa.repository.packages=com.epms.model,com.ecms.model

and also configuration file to this :

@EnableJpaRepositories("#{'${jpa.repository.packages}'.split(',')}")
public class JPAConfiguration {
}

but it's not working . any idea ? how can i do this in my configuration file?

like image 266
Zhozhe Avatar asked Jul 25 '16 11:07

Zhozhe


People also ask

Why do we use JPA repository?

JPA Repository is mainly used for managing the data in a Spring Boot Application. We all know that Spring is considered to be a very famous framework of Java. We mainly use this Spring Boot to create the Spring-based stand-alone and production-based applications with a very minimal amount of effort.

Why do we extend JPA repository?

Our interface extends the JpaRepository interface so that we'll benefit from all the standard behavior. You'll also notice we added the @NoRepositoryBean annotation. This is necessary because otherwise, the default Spring behavior is to create an implementation for all subinterfaces of Repository.

Which one is used to explicitly wire the PlatformTransactionManager to be detected by the repositories element?

Custom namespace attributes Explicitly wire the PlatformTransactionManager to be used with the repositories being detected by the repositories element.


1 Answers

As @amicoderozer is asking, if your classes share a common base package you only must indicate that root package.

If it's not your case (despite you are loading from a config file or you are declaring them manually) maybe the problem (will help posting any Exception or Runtime trace) is the way the split method is used. It returns an array, and I guess the generated code will be like this:

@EnableJpaRepositories("jpa.repository.packages1","jpa.repository.packages2")

That code doesn't compile.

Never tried Spring EL inside the annotation of a component, but despite this, maybe you should indicate the basePackages this way:

@EnableJpaRepositories(basePackages = "#{'${jpa.repository.packages}'.split(',')}")

If doesn't work, I recomend you first test it by manual array declaration:

@EnableJpaRepositories(basePackages = { "com.epms.model","com.ecms.model" })

Be sure all works as you expect, and then try again reading and parsing from config file.


UPDATE: After some readings, I've concluded that is not possible do what you want. The SpEL is allowed in many places but for annotations there is only documentation and working examples with @Value annotation.

like image 181
exoddus Avatar answered Jan 02 '23 12:01

exoddus