Note that there are some explanatory texts on larger screens.

plurals
  1. POJava field injection via annotation
    text
    copied!<p>I'm trying to write the code to annotate a field in a class so that the value of that field is set from a config file. This is virtually identical to the @Value attribute that the spring framework has, but for reasons I won't go into, I'm not using the spring framework.</p> <p>The project I'm working on is a web app using the Jersey framework. I apologize for all the up-front information, but for the sake of completeness, here's a basic setup of what I have:</p> <p>This is the main application class:</p> <pre><code>package com.ttrr.myservice; import com.ttrr.myservice.controllers.MyController; import javax.ws.rs.ApplicationPath; import javax.ws.rs.core.Application; import java.util.HashSet; import java.util.Set; @ApplicationPath("/") public class MyApplication extends Application { @Override public Set&lt;Class&lt;?&gt;&gt; getClasses() { final Set&lt;Class&lt;?&gt;&gt; classes = new HashSet&lt;Class&lt;?&gt;&gt;(); // register root resources classes.add(MyController.class); return classes; } } </code></pre> <p>This is the MyController class, that I want annotations to work in:</p> <pre><code>package com.ttrr.myservice.controllers; import com.ttrr.myservice.annotations.Property; import javax.servlet.ServletContext; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Context; @Path("test") public class MyController { @Property("my.value.from.config") private String myValue; @GET @Produces("text/html") @Path("/testPage") public String testPage(@Context ServletContext context) { return "&lt;p&gt;" + myValue + "&lt;/p&gt;"; } } </code></pre> <p>My annotations interface is pretty straight forward:</p> <pre><code>package com.ttrr.myservice.annotations; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface Property { String value() default ""; } </code></pre> <p>And I have an annotation parser class:</p> <pre><code>package com.ttrr.myservice.annotations; import java.io.IOException; import java.lang.reflect.Field; import java.util.Properties; public class PropertyParser { public static Properties properties; static { properties = new Properties(); try { Property.class.getClassLoader(); Properties defaults = new Properties(); defaults.load(Property.class.getClassLoader().getResourceAsStream("application.properties")); for(Object key : defaults.keySet()) { properties.put(key, defaults.get(key)); } } catch (IOException e) { //in this case do nothing, properties will simply be empty } } public void parse(Class&lt;?&gt; clazz) throws Exception { Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { if (field.isAnnotationPresent(Property.class)) { Property property = field.getAnnotation(Property.class); String value = property.value(); if (!"".equals(value)) { field.set(clazz, properties.getProperty(value)); } } } } } </code></pre> <p>Where I'm really struggling is putting it all together, and getting the value from my application.properties file into an instance of the MyController class.</p> <p>Thanks for taking the time to read all that! Any help will be greatly appreciated.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload