Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom annotation like @Value

Tags:

spring

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.

like image 828
DarVar Avatar asked Oct 11 '13 11:10

DarVar


People also ask

Can we create custom annotation in spring boot?

In this way, we can create different custom annotations for validation purposes. You can find the full source code here. It is easy to create and use custom annotations in Java. Java developers will be relieved of redundant code by using custom annotations.

Can we create custom annotations Java?

Java Custom annotations or Java User-defined annotations are easy to create and use. The @interface element is used to declare an annotation. For example: @interface MyAnnotation{}

What is @interface annotation in Java?

Annotation is defined like a ordinary Java interface, but with an '@' preceding the interface keyword (i.e., @interface ). You can declare methods inside an annotation definition (just like declaring abstract method inside an interface). These methods are called elements instead.


1 Answers

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.

like image 151
Jasper Blues Avatar answered Dec 03 '22 04:12

Jasper Blues