Spring Boot @查找不起作用-抛出空指针异常

ctehm74n  于 2023-02-04  发布在  Spring
关注(0)|答案(2)|浏览(122)

我正在检查bean作用域,一个注入到单例bean中的原型。当我使用@Lookup注解时,它抛出了空指针异常。
在singletonBean类中显示null指针...实际上,返回的null应该被cglib覆盖,并且应该返回原型作用域的bean...

AppConfiguration.java

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import Bean.ProtoBean;
import Bean.SingletonBean;

@Configuration
 public class AppConfiguration {

@Bean
@Scope("prototype")
public ProtoBean protoBean() {
    return new ProtoBean();
}

@Bean
@Scope("singleton")
public SingletonBean singletonBean() {
    return new SingletonBean();
}

}

SingletonBean.java

@Component
public class SingletonBean {

public SingletonBean() {
    System.out.println("SingletonBean");
}

public ProtoBean fromProto() {
    ProtoBean protoBean = getProtoBean();
//throws nullpointer here
    System.out.println("beans is here: "+protoBean.hashCode());
    return protoBean;
}

@Lookup
public ProtoBean getProtoBean() {
    return null;
}

}

App.java

public class App 
{
 public static void main( String[] args )
{
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfiguration.class);
    SingletonBean sb = ctx.getBean(SingletonBean.class);
    ProtoBean pb = sb.fromProto();

    SingletonBean sb1 = ctx.getBean(SingletonBean.class);
    ProtoBean pb1 = sb1.fromProto();

    if(pb.equals(pb1)) {
        System.out.println("Equal Proto");
    } else {
        System.out.println("Unequal Proto");
    }

    ctx.close();

}
}
5hcedyr0

5hcedyr01#

我曾经用这种方法做过同样的事情,然后,由于某种未知的原因,它停止了工作,产生了同样的NPE问题。我相信Spring在用GCLIB编译的实现替换自己的实现方面存在问题。
最后,我使用了factory method approach,即客户端bean拥有Spring应用程序上下文,并使用它返回getProtoBean()中所需的依赖项,这没有使用@Lookup那么直接,但似乎更可靠。

rjee0c15

rjee0c152#

我还有下一个案子:
在原型Bean的@PostConstruct处及之后抛出异常,在@Lookup方法中始终返回空值。

@Scope(SCOPE_PROTOTYPE)
@Component
public class PrototypeBean {

    @PostConstruct
    public void init() {
        sometimesThrownException();
    }
}

@Service
public class SingletoneBean{

    @Lookup
    protected PrototypeBean getPrototypeBean() {
        return null;
    } 
}

例外情况:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'prototypeBean': Invocation of init method failed; nested exception is java.lang.NullPointerException
        at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:160)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:415)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1786)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:342)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:227)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveNamedBean(DefaultListableBeanFactory.java:1175)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveBean(DefaultListableBeanFactory.java:420)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:350)
        at org.springframework.beans.factory.support.CglibSubclassingInstantiationStrategy$LookupOverrideMethodInterceptor.intercept(CglibSubclassingInstantiationStrategy.java:245)
        at ru.open.obo.imports.service.importer.ImporterFactoryImpl$$EnhancerBySpringCGLIB$$42f89ab8.payment1cImporter(<generated>)
        at ru.open.obo.imports.service.importer.ImporterFactoryImpl.getImporter(ImporterFactoryImpl.java:29)
        at ru.open.obo.imports.service.ImportServiceImpl.processImport(ImportServiceImpl.java:326)
        at ru.open.obo.imports.service.ImportServiceImpl.processImport(ImportServiceImpl.java:307)
        at ru.open.obo.imports.service.ImportServiceImpl$$FastClassBySpringCGLIB$$5260e96c.invoke(<generated>)

为了修复此行为,我在@PostConstruct方法中捕获异常:

@Scope(SCOPE_PROTOTYPE)
@Component
public class PrototypeBean {

    @PostConstruct
    public void init() {
        try {
            sometimesThrownException();
        } catch (Exception e) {
            log.error("Error at initialize prototype", e);
            context.setIsHasUnexpectedError(true);
        }
    }
}

数据库. springframework. Bootstrap :2.3.2.版本
io. Spring .依赖性管理:1.0.8.发布

相关问题