在静态类中使用@autowired [英] @autowired in static classes

查看:726
本文介绍了在静态类中使用@autowired的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个使用Hibernate的Spring MVC项目。
我试图创建一个Logger类,负责将日志输入到数据库中。
其他类只是调用具有某些属性的适当方法,而这个类应该做所有的魔术。
本质上它应该是一个带有静态方法的类,但是会导致自动创建dao对象的问题。

  public class StatisticLogger {
@Autowired
static Dao dao;
public static void AddLoginEvent(LogStatisticBean user){
// TODO code this god damn it
}
public static void AddDocumentEvent(LogStatisticBean user,Document document,DocumentActionFlags actionPerformed){
// TODO代码它真该死
}
public static void addErrorLog(Exception e,String page,HashMap< String,Object> parameters){
ExceptionLogBean elb = new ExceptionLogBean );
elb.setStuntDescription(e);
elb.setSourcePage(page);
elb.setParameters(parameters);
if(dao!= null){//但DAO为NULL
dao.saveOrUpdateEntity(elb);
}
}

如何使它正确?我应该怎么做才能让dao对象为null?
我知道我可以将它作为方法参数传递,但这不是很好。
我猜测autowired无法在静态对象上工作,因为它们被创建为早期自动创建机制尚未创建。

解决方案

您不能 @Autowired 一个静态字段。但有一个棘手的技巧来处理这个问题:

  @Component 
public class StatisticLogger {

私人静态刀道;

@Autowired
private Dao dao0;

@PostConstruct
private void initStaticDao(){
dao = this.dao0;
}

}

总之, @Autowired 一个实例字段,并在构建对象时将该值赋给静态字段。顺便说一句, StatisticLogger 对象也必须由Spring管理。

This is an Spring MVC project with Hibernate. I'm, trying to make a Logger class that, is responsible for inputting logs into database. Other classes just call proper methods with some attributes and this class should do all magic. By nature it should be a class with static methods, but that causes problems with autowiering dao object.

public class StatisticLogger {
    @Autowired
    static Dao dao;
    public static void AddLoginEvent(LogStatisticBean user){
        //TODO code it god damn it
    }
    public static void AddDocumentEvent(LogStatisticBean user, Document document, DocumentActionFlags actionPerformed){
        //TODO code it god damn it
    }
    public static void addErrorLog(Exception e, String page,  HashMap<String, Object> parameters){
        ExceptionLogBean elb=new ExceptionLogBean();
        elb.setStuntDescription(e);
        elb.setSourcePage(page);
        elb.setParameters(parameters);
        if(dao!=null){ //BUT DAO IS NULL
            dao.saveOrUpdateEntity(elb);
    }
}

How to make it right? What should I do not to make dao object null? I know that I could pass it as a method parameter, but that isn't very good. I'm guessing that autowired can't work on static objects, because they are created to early to autowiering mechanism isn't created yet.

解决方案

You can't @Autowired a static field. But there is a tricky skill to deal with this:

@Component
public class StatisticLogger {

  private static Dao dao;

  @Autowired
  private Dao dao0;

  @PostConstruct     
  private void initStaticDao () {
     dao = this.dao0;
  }

}

In one word, @Autowired a instance field, and assign the value to the static filed when your object is constructed. BTW, the StatisticLogger object must be managed by Spring as well.

这篇关于在静态类中使用@autowired的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆