简体   繁体   English

从另一个类的静态方法中调用spring注入类

[英]Call spring inject class from a static method from another class

Well, i have a class with @Component anotation, this class makes some selects in database, see: 好吧,我有一个带有@Component注释的类,该类在数据库中进行了一些选择,请参见:

@Component(value = "parametroRelatorioHelper")
public class ParametroRelatorioHelper {

    @Autowired
    private BasicDAO dao;

    public ParametroRelatorio getParametroByNome(String nome) {

        List<ParametroRelatorio> parametros = (List<ParametroRelatorio>) dao
                .findByNamedQuery(ParametroRelatorio.FIND_BY_NOME,
                        new NamedParams("nome", nome));

        if (parametros.size() > 0)
            return parametros.get(0);
        else
            return null;
    }

    public List<ParametroRelatorio> getAll() {

        return (List<ParametroRelatorio>) dao
                .findByNamedQuery(ParametroRelatorio.FIND_ALL);

    }

    public BasicDAO getDao() {
        return dao;
    }

    public void setDao(BasicDAO dao) {
        this.dao = dao;
    }
}

Now, i have a "Helper" class, where user can call your method directly (static method) but i need call a method from ParametroRelatorioHelper, see: 现在,我有一个“ Helper”类,用户可以在其中直接调用您的方法(静态方法),但是我需要从ParametroRelatorioHelper中调用方法,请参见:

public class ReportHelper {
  public static void call(){
      //how can i do it without @Component injection
      parametroRelatorioHelper.getAll();
   }
}

听起来您的体系结构是不正确的,相反,ReportHelper也应该是一个组件,并且应该在其中注入依赖项,否则它与Spring IOC的思想冲突,helper方法不应依赖于服务上的组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM