spring boot:普通类调用spring bean对象

编写的SpringUtil在Spring Boot可以扫描的包下或者使用@ComponentScan引入自定义的包了,只需要使得SpringUtil实现接口:ApplicationContextAware,然后加上@Component 注解即可。

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.roger.util;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
* 普通类使用Spring Bean对象
*/
public class implements ApplicationContextAware {
private static ApplicationContext ctx = null;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if (ctx == null){
ctx = applicationContext;
}
}
* 获取ApplicationContext
* @return
*/
public static ApplicationContext getApplicationContext(){
return ctx;
}
* 通过Bean name获取Bean
* @param name
* @return
*/
public static Object getBean(String name){
return ctx.getBean(name);
}
* 通过Bean class获取Bean
* @param clazz
* @param <T>
* @return
*/
public static <T> T getBean(Class<T> clazz){
return ctx.getBean(clazz);
}
* 通过Bean name以及Class返回指定的Bean
* @param name
* @param clazz
* @param <T>
* @return
*/
public static <T> T getBean(String name, Class<T> clazz){
return ctx.getBean(name, clazz);
}
}