5

I need to create a means to add a custom annotation like

@Value("${my.property}")

However, in my case I need to get the value from a database rather then a properties file.

Basically I would like to create a bean on container startup that reads in property name value pairs from a database and can then inject these into fields belonging to other beans.

3 Answers 3

9

Approach #1:

One way is to create an Aspect, with a point-cut expression that matches any method having this annotation.

Your aspect will then:

  • Read the property value in the annotation
  • Look up the required value an inject it into the class.

AOP Kickstart

Here's a guide to getting started with AOP in Spring

http://www.tutorialspoint.com/spring/aop_with_spring.htm

Joinpoint matching

Here's a reference that describes how to create a join-point that matches on annotations: http://eclipse.org/aspectj/doc/next/adk15notebook/annotations-pointcuts-and-advice.html

Approach #2:

Another way is to use a BeanFactoryPostProcessor - this is essentially how a PropertyPlaceholderConfigurer works.

  • It will look at your bean definitions, and fetch the underlying class.
  • It will then check for the annotation in the class, using reflection.
  • It will update the bean definition to include injecting the property as per the value in the annotation.

. . actually I think approach #2 sounds more like what you want - all of the processing happens on "start-up". . . (In actual fact your modifying the bean recipes even before startup). . whereas if you used AOP, you'd be intercepting method invocations, which might be too late for you?

Namespace Handler

If you wanted you could even create your own Spring namespace handler to turn on your post processor in a terse way. Eg:

<myApp:injectFromDb />

as an alternative to:

<bean class="MyDatabaseLookupProcessorImpl etc, etc. />

Update: Approach #3

As of Spring 3.1 there's also the PropertySourcesPlaceholderConfigurer, that will provide most of the plumbing for you, so you can achieve this with less code.

1
  • 1
    I guess this is another option: Use Spring 3.1 PropertySourcesPlaceholderConfigurer and add a new PropertySource for my database properties: spring.io/blog/2011/02/15/…
    – DarVar
    Commented Oct 11, 2013 at 12:42
0

Alternatively you should be able to configure kind of properties repository bean and then use it in SpEL directly in @Value annotation.

Let's say you'd have bean called propertiesRepository in your context that implements following interface:

interface PropertiesRepository {
    String getProperty(String propertyName);
}

then on bean where you want to inject values you can use following expression

@Value("#{propertiesRepository.getProperty('my.property')}")
String myProperty;
0

You can use @Value annotation by injecting database configuration in application environment itself.

I know this is an old question but I didn't find an exact solution. So documenting it here.

I have already answered the same on different forum.

Please refer to this answer for exact solution to your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.