1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.module.blossom.support;
35
36 import org.springframework.beans.factory.InitializingBean;
37 import org.springframework.context.ApplicationContext;
38 import org.springframework.context.ApplicationContextAware;
39
40
41
42
43
44 public abstract class AbstractBeanDetector implements ApplicationContextAware, InitializingBean {
45
46 private ApplicationContext applicationContext;
47 private Class<?> type = Object.class;
48
49 public ApplicationContext getApplicationContext() {
50 return applicationContext;
51 }
52
53 public void setApplicationContext(ApplicationContext applicationContext) {
54 this.applicationContext = applicationContext;
55 }
56
57 public Class<?> getType() {
58 return type;
59 }
60
61 public void setType(Class<?> type) {
62 this.type = type;
63 }
64
65 public void detectBeans() {
66 String[] beanNames = applicationContext.getBeanNamesForType(type, true, true);
67 for (String beanName : beanNames) {
68 Class<?> type = applicationContext.getType(beanName);
69 if (type != null && include(type, beanName)) {
70 Object bean = applicationContext.getBean(beanName);
71 onBeanDetection(bean, beanName);
72 }
73 }
74 }
75
76 public void afterPropertiesSet() throws Exception {
77 detectBeans();
78 }
79
80 protected boolean include(Class<?> beanType, String beanName) {
81 return true;
82 }
83
84 protected abstract void onBeanDetection(Object bean, String beanName);
85 }