gpt4 book ai didi

java - 使用 BeanPostProcessor 获取 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 07:21:12 27 4
gpt4 key购买 nike

我正在制作一个简单的程序来测试 bean 后处理器,但收到 NullPointerException。这是我写的代码

人员类别:

public class Person {

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public void init(){
System.out.println("Bean Initialize");
}

public void destroy(){
System.out.println("Bean Destroy");
}
}

后处理器类:

public class Post implements BeanPostProcessor {

@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("Before Initialization : " + beanName);
return null;
}

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("After Initialization : " + beanName);
return null;
}
}

Controller 类:

public class Controller {

public static void main(String[] args){
@SuppressWarnings("resource")
AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
Person p = (Person)context.getBean("person");
System.out.println(p.getName());
context.registerShutdownHook();
}
}

Spring.xml:

<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-3.0.xsd">

<bean id="person" class="com.data.Person" init-method="init" destroy-method="destroy">
<property name="name" value="Spring"></property>
</bean>

<bean class="com.postprocessor.Post"/>

堆栈跟踪:

After Initialization : person
Apr 27, 2016 10:29:08 PM org.springframework.context.support.ClassPathXmlApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'person' defined in class path resource [spring.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'person' defined in class path resource [spring.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.controller.Controller.main(Controller.java:12)
Caused by: java.lang.NullPointerException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1660)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1645)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
... 12 more

这就是我编写的所有代码,任何人都可以告诉我我在这里犯了什么错误。我将不胜感激。

最佳答案

我不确定你想要实现什么,但是为了正常工作,BeanPostProcessor应该在它的方法中返回bean实例,而不是像你那样返回null。

关于java - 使用 BeanPostProcessor 获取 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36905948/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com