Java 是否可以将Springbean注入枚举字段?
我们有一个使用Struts 1的系统,并且是使用Enum的基于单例的系统 现在我们有一个使用Spring 4.3.6.RELEASE编写的依赖项,该依赖项应该只用于其他也使用Spring的系统,这就是为什么所有声明的Spring依赖项都有其提供的范围 现在,使用Struts 1的系统应该使用这种依赖关系。我声明了所有必要的Spring依赖项,创建了应用程序上下文,但不幸的是,我无法将bean注入到我的单例中,因为它们是枚举 该依赖项没有按照我的要求提供rest服务 这是我的应用程序上下文:Java 是否可以将Springbean注入枚举字段?,java,spring,struts-1,java-ee-7,Java,Spring,Struts 1,Java Ee 7,我们有一个使用Struts 1的系统,并且是使用Enum的基于单例的系统 现在我们有一个使用Spring 4.3.6.RELEASE编写的依赖项,该依赖项应该只用于其他也使用Spring的系统,这就是为什么所有声明的Spring依赖项都有其提供的范围 现在,使用Struts 1的系统应该使用这种依赖关系。我声明了所有必要的Spring依赖项,创建了应用程序上下文,但不幸的是,我无法将bean注入到我的单例中,因为它们是枚举 该依赖项没有按照我的要求提供rest服务 这是我的应用程序上下文: &l
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
<import resource="classpath:/applicationContext-dependency.xml" />
</beans>
我已经阅读了一些SO问题,如和,并尝试了其中列出的解决方案,但没有得到任何运气,我的枚举字段总是空的,不幸的是,这些是依赖项的唯一入口点
如何注入字段?您需要的是一个带有枚举的类。@efekctive,请在回答中详细说明您的注释好吗?ItemRemessaUtil应该是一个带有内部枚举的类。具有如此多逻辑的枚举失去了Java教程中的“枚举类型是一种特殊的数据类型,使变量成为一组预定义常量”。正是这个定义促使IoC容器在扫描ItemRemessautil时忽略它,因为它是系统中广泛使用的一个单例,更改它的实现将影响一大堆其他用例。
public enum ItemRemessaUtil {
INSTANCIA;
@Autowired
private BeneficiarioBS beneficiarioBS;
@Autowired
private ItemRemessaBS itemRemessaBS;
@Autowired
public Boleto inserirItemRemessaMultaDesassociacao(final MultaBean multa) throws BOException, DataAccessException {
return this.inserirItemRemessa(multa.getNroFatura(), multa.getNossoNumeroItemRemessa(),
multa.getDataEmissaoBoleto(), multa.getDataVencimentoBoleto(),
multa.getValorFatura().subtract(multa.getValorDesconto()), multa.getComprador(), TipoOrigem.MULTA);
}
@Autowired
public Boleto inserirItemRemessaOrdemCancelamento(final OrdCancelBean ordCancel)
throws BOException, DataAccessException {
return this.inserirItemRemessa(ordCancel.getNrOrdCancel(), ordCancel.getNossoNumeroItemRemessa(),
ordCancel.getDataEmissaoBoleto(), ordCancel.getDataVencimentoBoleto(),
BigDecimal.valueOf(ordCancel.getVlOrdem()), ordCancel.getComprd(), TipoOrigem.CANCELAMENTO_VT);
}
@Autowired
private Boleto inserirItemRemessa(final long numeroPedido, final Long nossoNumeroItemRemessa,
final Date dataDocumento, final Date dataVencimentoBoleto, final BigDecimal valorTotal,
final ComprdBean comprdBean, final TipoOrigem tipoOrigem) throws BOException, DataAccessException {
final Boleto boleto = BoletoBancarioFactory.criarBoletoSantander(
this.beneficiarioBS.construirBeneficiario(tipoOrigem, ConstantesBoleto.CNPJ_RIOPAR, numeroPedido,
nossoNumeroItemRemessa, dataVencimentoBoleto),
BoletoUtil.construirPagador(comprdBean),
Datas.novasDatas(dataDocumento,
TipoOrigem.MULTA.equals(tipoOrigem)
? ParamMultaDesassociacaoBO.retornarQuantidadeDiasVencimentoMultaDesassociacao()
: ConstantesBoleto.QUANTIDADE_DIAS_VENCIMENTO_BOLETO_SANTANDER),
valorTotal);
if (nossoNumeroItemRemessa == null
|| DateUtil.retornarDataSemHorario(dataVencimentoBoleto).before(boleto.getDatas().getVencimento())) {
this.itemRemessaBS.inserirItemRemessa(boleto);
}
return boleto;
}
}