1 package net.sourceforge.sannotations.utils;
2
3 import java.lang.reflect.Method;
4
5 import javax.annotation.PostConstruct;
6 import javax.annotation.PreDestroy;
7
8 import org.springframework.beans.factory.annotation.Autowire;
9 import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
10 import org.springframework.beans.factory.support.BeanDefinitionRegistry;
11 import org.springframework.beans.factory.support.RootBeanDefinition;
12 /***
13 * Generic annottion bean reader, an annotation reader that registars all beans within the scanned classpath in the Spring context.<br/>
14 * The registered beans will use the reader configuration for lazy, auto wire mode and scope.<br/>
15 * to register an annotation reader you can use the following code:
16 * <code>
17 * @Retention(RetentionPolicy.RUNTIME)
18 * @Target( { ElementType.TYPE })
19 * @Documented
20 * public @interface MyAnnotation {
21 * }
22 * AnnotationBeanDefinitionReader.register(MyAnnotation.class,new GenericAnnotationBeanReader(true,"BY_NAME","session");
23 * </code>
24 * this code must run before the annotation sa:annotation-autoload
25 * @author Urubatan
26 *
27 */
28 public class GenericAnnotationBeanReader implements IBeanReader
29 {
30 private boolean lazy = false;
31 private int autoWireMode = Autowire.BY_NAME.value();
32 private String scope = "singleton";
33
34 /***
35 * Default constructor, lazy will be false, autoWire will be by name and scope will be singleton
36 */
37 public GenericAnnotationBeanReader()
38 {
39 super();
40 }
41
42 /***
43 * Creates a new generic annotation bean reader with specified properties
44 * @param lazy defines the lazy mode for the scanned classes
45 * @param autoWireMode defines the autoWire mode for the scanned classes, valid values are the constants of the interface {@link org.springframework.beans.factory.config.AutowireCapableBeanFactory}
46 * @param scope defines the scope of the scanned classes
47 */
48 public GenericAnnotationBeanReader(boolean lazy, int autoWireMode, String scope)
49 {
50 super();
51 this.lazy = lazy;
52 this.autoWireMode = autoWireMode;
53 this.scope = scope;
54 }
55
56 /***
57 * Creates a new generic annotation bean reader with specified properties
58 * @param lazy defines the lazy mode for the scanned classes
59 * @param autoWireMode defines the autoWire mode for the scanned classes, valid values are the values of the enumeration {@link org.springframework.beans.factory.annotation.Autowire}
60 * @param scope defines the scope of the scanned classes
61 */
62 public GenericAnnotationBeanReader(boolean lazy, String autoWireMode, String scope)
63 {
64 super();
65 this.lazy = lazy;
66 this.autoWireMode = Autowire.valueOf(autoWireMode).value();
67 this.scope = scope;
68 }
69
70 public void setLazy(boolean lazy)
71 {
72 this.lazy = lazy;
73 }
74
75 /***
76 * defines the autoWire mode for the scanned classes
77 * @param autoWireMode valid values are the constants of the interface {@link org.springframework.beans.factory.config.AutowireCapableBeanFactory}
78 */
79 public void setAutoWireMode(int autoWireMode)
80 {
81 this.autoWireMode = autoWireMode;
82 }
83
84 /***
85 * defines the autoWire mode for the scanned classes
86 * @param autoWireMode valid values are the values of the enumeration {@link org.springframework.beans.factory.annotation.Autowire}
87 */
88 public void setAutoWireMode(String autoWireMode)
89 {
90 this.autoWireMode = Autowire.valueOf(autoWireMode).value();;
91 }
92
93 public void setScope(String scope)
94 {
95 this.scope = scope;
96 }
97
98 /***
99 * method called every time a bean with the configured annotation is read.
100 * the bean name will bne the class name with the first letter in lower case, the other properties are read from this instance.
101 */
102 public void register(BeanDefinitionRegistry registry, Class<?> clazz)
103 {
104 final RootBeanDefinition rbd = new RootBeanDefinition();
105 rbd.setAbstract(false);
106 rbd.setBeanClass(clazz);
107 rbd.setBeanClassName(clazz.getName());
108 rbd.setLazyInit(lazy);
109 rbd.setAutowireCandidate(autoWireMode!=AutowireCapableBeanFactory.AUTOWIRE_NO);
110 rbd.setAutowireMode(autoWireMode);
111 rbd.setScope(scope);
112 String name = clazz.getSimpleName();
113 name = name.substring(0,1).toLowerCase() + name.substring(1, name.length());
114 for (final Method m : clazz.getMethods()) {
115 if (m.getAnnotation(PreDestroy.class) != null) {
116 rbd.setDestroyMethodName(m.getName());
117 }
118 if (m.getAnnotation(PostConstruct.class) != null) {
119 rbd.setInitMethodName(m.getName());
120 }
121 }
122 registry.registerBeanDefinition(name, rbd);
123 }
124
125 }