0

I'm new to Spring and I'm building an application where some entities (JPA/Hibernate) need access to a property from application.properties. I do have a configuration class in which this is trivial:

@Configuration
public class FactoryBeanAppConfig {    
    @Value("${aws.accessKeyId}")
    private String awsAccessKeyId;
    @Value("${aws.secretKey}")
    private String awsSecretKey;
}

but since entities do not have and I think they should not have the annotations such as @Configuration or @Component, what's the Spring way for them to access the property?

Now, I know I can create my own class, my own bean, and make it as a simple wrapper around the properties; but is that the Spring way to do it or is there another way?

Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622
  • Why do entities need to access configuration data? This sounds so very wrong. – Kayaman Nov 10 '17 at 11:48
  • @Kayaman, totally agreed – Leffchik Nov 10 '17 at 11:56
  • Is it common in Ruby (basing this question on your stats) to have active records interacting with the rest of the environment? What do you expect to achieve with having entities access properties? – Kayaman Nov 10 '17 at 12:23
  • @Kayaman: the entity generates S3 signed URLs so it needs to access the property that specifies the bucket where the URL will be. – Pablo Fernandez Nov 10 '17 at 13:25
  • 2
    The entity should not be generating any S3 signed URLs. An entity is a representation of a piece of data, it has no business thinking about S3. That belongs to a service class. – Kayaman Nov 10 '17 at 13:27
  • @Kayaman: similar problem: previously, an entity used LocatlDateTime.now() method for its CREATION_DATE. Now, this should be configurable for testing, see https://stackoverflow.com/questions/32792000/how-can-i-mock-java-time-localdate-now . – serv-inc Jan 04 '22 at 11:26

5 Answers5

2

specify Property file location using @PropertySource
Something like below

@PropertySource("classpath:/application.proerties")

You also need to add below bean in your config

@Bean
    public static PropertySourcesPlaceholderConfigurer propertyConfigIn() {
        return new PropertySourcesPlaceholderConfigurer();
    }
Niraj Sonawane
  • 10,225
  • 10
  • 75
  • 104
1

There is no "Spring way", since JPA entities and Spring have nothing to do with each other. Most importantly, JPA entities are not Spring beans, so Spring doesn't know or care about them as they're not managed by Spring.

You can try to hack around, trying in vain to access Spring's configuration from code that should not be trying to access it, or you can accept the truth that your design is broken and you're trying to do something that's not meant to be done.

As was proposed several times, use a service class for this. It's managed by Spring, so it can access the Spring config, and it can handle entities, so there's no crossing boundaries.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
1

First create a public static variable in some bean managed by Spring, then make the following use of the @Value annotation.

public static String variable;

@Value("${variable}")
private void setVariable(String value) {
    variable = value;
}

It will be set at runtime on startup, now you can access it from entities and everywhere else because it is just a public static var.

0

You can use @PropertySource to load the properties file as follows

@Configuration
@PropertySource("classpath:/com/organization/config/application.proerties")
public class FactoryBeanAppConfig {
 ...
}
amdg
  • 2,211
  • 1
  • 14
  • 25
  • I already have a Configuration class that is loading the properties without specifying PropertySource. I don't see how this helps an entity reach the properties. – Pablo Fernandez Nov 10 '17 at 13:24
  • I would have preferred to do all the properties related stuff in a Service/Controller class – amdg Nov 10 '17 at 13:39
0

Entities should not acces environment properties. If you are using your entity through a service, then the service can access the properties to act on the entity.