java - Implementing Dependency Injection using Custom Annotation -


i working on core java framework. don't want create instances directly inside class why want use dependency injection.

i thinking of declaring custom annotations on fields instantiated. , having call function create instance , inject field.

i had tried create custom annotation. looks there's no direct way callback on declared annotation. so, trying scan classes that. ended problem java scanning class annotation using google reflections

please let me know if right way of achieving this.

since question tagged 'spring', can use spring framework's bean annotations (@component / @service / @repository / ...), classpath scanning , @autowired.

for example:

setup classpath scanning on spring config xml:

<context:component-scan base-package="com.mycompany.myapp" /> 

create bean scanned. spring container automatically create singleton instance of bean using default constructor:

@repository public class foodao {   ... } 

inject reference above dao instance using di + autowiring

@service public class fooservice {    @autowired private foodao foodao;    ... } 

Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -