spring 在静态工具类中使用注解注入bean

/**
* @author: jerry
* @Email:
* @Company:
* @Action: 日志处理工具类
* @DATE: 2016-9-19
*/
 
@Component //泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注
public  class  LogUtil {
 
@Autowired <br> //注意这里非静态
private  AdminLogService logService;
 
private  static  LogUtil logUtil;
 
@PostConstruct  //@PostConstruct修饰的方法会在服务器加载Servle的时候运行,并且只会被服务器执行一次。PostConstruct在构造函数之后执行,init()方法之前执行
public  void  init() {
logUtil =  this ;
logUtil.logService =  this .logService;
}
public  AdminLogService getUserService() {
return  logService;
}
public  void  setUserService(AdminLogService userService) {
this .logService = userService;
}
public  static  void  addLog(LoginInfo loginInfo,String desc){
//添加日志
AdminLog log= new  AdminLog();
log.setCreatetime( new  Date());
log.setDescription(desc);
try  {
log.setCuid(loginInfo.getUser().getId());
log.setCreateuser(loginInfo.getUser().getAccount());
logUtil.logService.insertLog(log);
catch  (Exception e) {
log.setCuid( 0 );
log.setCreateuser( "unknow" );<br> //调用时通过类去调
logUtil.logService.insertLog(log);
}
 
}

}


  1. public class LogUtil {    
  2.     @Autowired    
  3.     private LogService logService;    
  4.     private static LogUtil logUtil;    
  5.     @PostConstruct      
  6.     public void init() {    
  7.         logUtil = this;    
  8.         logUtil.logService = this.logService;    
  9.     }    
  10.     //之后调用    
  11.     logUtil.logService.xxx();    
  12. }  
@PostConstruct
  被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器调用一次,类似于Servlet的init()方法。

  被@PostConstruct修饰的方法会在构造函数之后,init()方法之前执行。

@PreConstruct

  被PreConstruct修饰的方法会在服务器卸载Servlet的时候运行,并且只会被服务器调用一次,类似于Servlet的destroy()方法。
  被@PreConstruct修饰的方法会在destroy()方法之后运行,在Servlet被彻底卸载以前。


或者使用spring获得bean的方式

3、通用的方法来了,神器啊,前的  1、2两种方法并不通用,可以抛弃了。
在配置文件中加入:

Xml代码
  1. <!-- 用于持有ApplicationContext,可以使用SpringContextHolder.getBean('xxxx')的静态方法得到spring bean对象 -->  
  2. <bean class="com.xxxxx.SpringContextHolder" lazy-init="false" />  

 

Java代码
  1. import org.springframework.context.ApplicationContext;  
  2. import org.springframework.context.ApplicationContextAware;  
  3. /** 
  4.  * 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext. 
  5.  *  
  6.  */  
  7. public class SpringContextHolder implements ApplicationContextAware {  
  8. private static ApplicationContext applicationContext;  
  9.   
  10. /** 
  11. * 实现ApplicationContextAware接口的context注入函数, 将其存入静态变量. 
  12. */  
  13. public void setApplicationContext(ApplicationContext applicationContext) {  
  14. SpringContextHolder.applicationContext = applicationContext; // NOSONAR  
  15. }  
  16.   
  17. /** 
  18. * 取得存储在静态变量中的ApplicationContext. 
  19. */  
  20. public static ApplicationContext getApplicationContext() {  
  21. checkApplicationContext();  
  22. return applicationContext;  
  23. }  
  24.   
  25. /** 
  26. * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型. 
  27. */  
  28. @SuppressWarnings("unchecked")  
  29. public static <T> T getBean(String name) {  
  30. checkApplicationContext();  
  31. return (T) applicationContext.getBean(name);  
  32. }  
  33.   
  34. /** 
  35. * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型. 
  36. */  
  37. @SuppressWarnings("unchecked")  
  38. public static <T> T getBean(Class<T> clazz) {  
  39. checkApplicationContext();  
  40. return (T) applicationContext.getBeansOfType(clazz);  
  41. }  
  42.   
  43. /** 
  44. * 清除applicationContext静态变量. 
  45. */  
  46. public static void cleanApplicationContext() {  
  47. applicationContext = null;  
  48. }  
  49.   
  50. private static void checkApplicationContext() {  
  51. if (applicationContext == null) {  
  52. throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder");  
  53. }  
  54. }  
  55. }  

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值