当前位置: 首页 > 知识库问答 >
问题:

使用autowired从实现类设置抽象类的属性

宫俊远
2023-03-14

我试图从实现类(服务)的抽象类中设置受保护属性的属性。我在实现类中使用@Autowired注释和setter注释。我正在用groovy进行编码,如果需要,groovy在幕后实现getter和setter。当我尝试运行此代码时,会创建一条错误消息,指示发生了StackOverFlow错误:

""2019-04-02 19:53:57 [restartedMain] ERROR o.s.boot.SpringApplication - Application startup failed
"org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:139)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:124)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
    at org.springframework.boot.SpringApplication$run.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:128)
    at gov.epa.esml.WarInitializerApplication.main(WarInitializerApplication.groovy:30)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.initialize(TomcatEmbeddedServletContainer.java:138)
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.<init>(TomcatEmbeddedServletContainer.java:87)
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getTomcatEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:554)
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:179)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:166)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:136)
    ... 15 common frames omitted
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sessionVariableFilter': Unsatisfied dependency expressed through field 'myEmsService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myEmsService': Injection of autowired dependencies failed; nested exception is java.lang.StackOverflowError
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:586)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:364)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1269)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:551)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:481)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.boot.web.servlet.ServletContextInitializerBeans.getOrderedBeansOfType(ServletContextInitializerBeans.java:234)
    at org.springframework.boot.web.servlet.ServletContextInitializerBeans.addAsRegistrationBean(ServletContextInitializerBeans.java:182)
    at org.springframework.boot.web.servlet.ServletContextInitializerBeans.addAsRegistrationBean(ServletContextInitializerBeans.java:177)
    at org.springframework.boot.web.servlet.ServletContextInitializerBeans.addAdaptableBeans(ServletContextInitializerBeans.java:159)
    at org.springframework.boot.web.servlet.ServletContextInitializerBeans.<init>(ServletContextInitializerBeans.java:80)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getServletContextInitializerBeans(EmbeddedWebApplicationContext.java:252)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.selfInitialize(EmbeddedWebApplicationContext.java:225)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.access$000(EmbeddedWebApplicationContext.java:90)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext$1.onStartup(EmbeddedWebApplicationContext.java:215)
    at org.springframework.boot.context.embedded.tomcat.TomcatStarter.onStartup(TomcatStarter.java:55)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5267)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1423)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1413)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myEmsService': Injection of autowired dependencies failed; nested exception is java.lang.StackOverflowError
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:370)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1269)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:551)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:481)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1136)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1064)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:583)
    ... 27 common frames omitted
Caused by: java.lang.StackOverflowError: null
    at java.lang.Exception.<init>(Exception.java:102)
    at java.lang.ReflectiveOperationException.<init>(ReflectiveOperationException.java:89)
    at java.lang.reflect.InvocationTargetException.<init>(InvocationTargetException.java:72)
    at sun.reflect.GeneratedMethodAccessor71.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
    at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:2726)
    at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:3785)
    at gov.epa.esml.table.ARichTableService.setProperty(ARichTableService.groovy)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.setGroovyObjectProperty(ScriptBytecodeAdapter.java:545)
    at gov.epa.esml.myEms.service.MyEmsService.setConverter(MyEmsService.groovy:38)
    at sun.reflect.GeneratedMethodAccessor71.invoke(Unknown Source)
....
@Service("myEmsService")
class MyEmsService extends ARichTableService implements IMyEmsService {

    @Autowired
    EmRepository emRepository
    @Autowired
    void setDataService(IMyEmsDAO dataService) {
        this.dataService = dataService
    }

    @Autowired
    @Qualifier("emFieldsRenderer")
    void setConverter(AClassFieldsRenderer converter) {
        this.converter = converter
    }

    @Override
    TableData getTableData(HttpServletRequest request, Collection<ColumnHeader> headers,
                           Integer offset, Integer pageSize, Collection<ADataFilter> filters,
                           Collection<ColSort> orderBy) throws Exception {

        Collection<Object> data
        try {
            data = dataService.getTableData(headers, offset, pageSize, filters, orderBy);
            Collection<Object> ems = new ArrayList<Object>()
            for (Object d : data) {
                ems.add(d)
            }
            data = ems
        } //This happens when subqueries returns empty sets.
        catch (SQLGrammarException e) {
            data = new ArrayList<Object>()
        }

        TableData formatedData = new TableData()
        formatedData.setHeaders((ArrayList<ColumnHeader>) headers)

        for (Object o : data) {
            Object d = converter.convert(o, headers)
            formatedData.addData((ArrayList<String>) d)
        }

        return formatedData
    }


    List<Em> getRelatedIds(Integer myId) {
        emRepository.findByParentId(myId)
    }
}
abstract class ARichTableService implements IRichTableService {

    protected AClassFieldsRenderer converter

    protected IRichTableDAO dataService

    @Override
    TableData getTableData(HttpServletRequest request, Collection<ColumnHeader> headers,
                           Integer offset, Integer pageSize, Collection<ADataFilter> filters,
                           Collection<ColSort> orderBy) throws Exception {

        Collection<Object> data = null
        try {
            data = dataService.getTableData(headers, offset, pageSize, filters, orderBy)
        }
        //This happens when subqueries returns empty sets.
        catch (SQLGrammarException e) {
            data = new ArrayList<Object>()
        }

        TableData formatedData = new TableData()
        formatedData.headers = (ArrayList<ColumnHeader>) headers

        for (Object o : data) {
            Object d = converter.convert(o, headers)
            formatedData.addData((ArrayList<String>) d)
        }

        return formatedData
    }

    Long getTotalRows(Collection<ADataFilter> filters, Integer pageSize) throws Exception {
        return (dataService.getTotalRows(filters))
    }
}
interface IMyEmsService extends IRichTableService {

    List<Em> getRelatedIds(Integer myId)

}

我不确定是哪部分代码创建了这种格式,或者这种格式的注入是否可行?

共有1个答案

潘秦斩
2023-03-14

很可能是这样的。转换器=正在尝试调用此。在幕后设置转换器。理想的方法是转移到构造函数注入(这也消除了对自动连线的需要),但作为一种即时修复方法,请尝试使用直接字段访问:

@Autowired
void setConverter(AClassFieldsRenderer converter) {
    this.@converter = converter
}
 类似资料:
  • -- 我想创建一个工厂,就像抽象工厂模式一样,我可以调用,以获得我在Spring XML中配置的任何实现的规模: 但是,使用这种方法,我如何配置ScaleFactory应该使用Spring XML中的哪个实现呢? -- 另一种方法是在我的服务中使用属性而不是ScaleFacotry,并使用ScaleFactory,如下所示: 但是这个工厂就毫无意义了,因为我还可以运行。

  • 问题是: 我有一个叫做Schema的基类,它是抽象的,它是一个未生成的类。我有两个从Schema继承生成的JAXB类:FixedWidthSchema和DelimitedSchema。 我使用外部绑定(xjb)文件来指定XSD和Java类之间的映射。 在基类模式中,我定义了几个方法: 公共架构静态创建(Model m),它从提供的模型创建架构。 公共抽象Writer marshal(),它将当前架

  • 类使程序员可以构造对象的属性(attribute,表示为数据成员)和行为(behavior)或操作(operation,表示为成员函数)。C++ 中用关键字 class 定义包含数据成员和成员函数的类型。 成员函数在有些面向对象编程语言中也称为方法(method),响应对象接收的消息(message)。消息对应于一个对象发给另一个对象或由函数发给对象的成员函数调用。 一旦定义了一个类,可以用类名声

  • 问题内容: 我有以下界面: 我在下面有抽象类(没有提到方法插入): 我有具体的课程: 最后,SpecificEntryBean定义为: 我有以下错误: 类型SpecificEntry必须实现继承的抽象方法SingleRecordInterface.insert(AbstractEntryBean) 考虑到SpecificEntryBean扩展了AbstractEntryBean,所以我不明白此错误

  • 假设我有这门课: 以及子类: 我知道这是不可能的,但我想你明白我想要什么。如果Foobar实现了Cloneable,并且没有扩展AbstractFoo,那么子类就可以工作。我想我想要但不允许的是: 如果Foobar实现了Cloneable,并且没有扩展AbstractFoo,那么子类就可以工作。 除了扩展的抽象类,我怎么能做到“相同”?

  • 在package-data.dao中有一个bean 完整StackTrace: 编辑:问题解决了:我只是删除了“@transactional”,它可以工作了。我还重构了一点我的问题,这样它就实现了Peter Jurkovic建议的接口。