我正在使用bean post处理器,它给出了空指针异常 [英] I am using bean post processor, and it is giving null pointer exception

查看:130
本文介绍了我正在使用bean post处理器,它给出了空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

                package com.bpp.beans;

                public class EmpBean {
                    String name;
                    String id;
                    public String getName() {
                        return name;
                    }
                    public void setName(String name) {
                        this.name = name;
                    }
                    public String getId() {
                        return id;
                    }
                    public void setId(String id) {
                        this.id = id;
                    }

                    public void initempbean()
                    {
                        System.out.println("inside initempbean");
                    }

                    public void destroyempbean()
                    {
                        System.out.println("inside destroyempbean");
                    }
                }


            package com.bpp.beans;

            public class Person {
                String name;
                String id;
                EmpBean empBean;
                public String getName() {
                    return name;
                }
                public void setName(String name) {
                    this.name = name;
                }
                public String getId() {
                    return id;
                }
                public void setId(String id) {
                    this.id = id;
                }
                public EmpBean getEmpBean() {
                    return empBean;
                }
                public void setEmpBean(EmpBean empBean) {
                    this.empBean = empBean;
                }

                public void initperson()
                {
                    System.out.println("inside init personnnnnnn");
                }

                public void destroyperson()
                {
                    System.out.println("inside destroy personnnn");
                }

            }

        package com.bpp.beans;

        import org.springframework.beans.BeansException;
        import org.springframework.beans.factory.config.BeanPostProcessor;

        public class EmpPostProcessor implements BeanPostProcessor {

            @Override
            public Object postProcessAfterInitialization(Object bean, String arg1)
                    throws BeansException {

                System.out.println("inside bean pp after initialization");

                return null;
            }

            @Override
            public Object postProcessBeforeInitialization(Object bean, String arg1)
                    throws BeansException {
                System.out.println("inside bean pp before initialization");

                return null;
            }

        }
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:aop="http://www.springframework.org/schema/aop"
            xmlns:c="http://www.springframework.org/schema/c"
            xmlns:p="http://www.springframework.org/schema/p"
            xmlns:context="http://www.springframework.org/schema/context"
            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

            <bean class="com.bpp.beans.EmpPostProcessor"/>

            <bean id="emp" class="com.bpp.beans.EmpBean" init-method="initempbean" destroy-method="destroyempbean">
                <property name="name" value="sanjay"/>
                <property name="id" value="44"/>
            </bean>

            <bean id="person" class="com.bpp.beans.Person" init-method="initperson" destroy-method="destroyperson">
                <property name="name" value="sanju"/>
                <property name="id" value="77"/>
                <property name="empBean" ref="emp"/>
            </bean>



    </beans>                


package com.bpp.main;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.bpp.beans.Person;

public class BppMain {

    public static void main(String[] args) {

        ApplicationContext context=new ClassPathXmlApplicationContext("com/bpp/commons/application-context.xml");
         Person obj=context.getBean("person",Person.class);
         System.out.println("the value of name is:"+obj.getName());
    }

}

异常是空指针异常,创建emp bean时

The exception is null pointer exception,while creating emp bean


Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'emp' defined in class path resource [com/bpp/commons/application-context.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
    at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83)
    at com.bpp.main.BppMain.main(BppMain.java:12)
Caused by: java.lang.NullPointerException
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1522)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
    ... 12 more


推荐答案

Spring doc 是为 postProcessAfterInitialization postProcessBeforeInitialization :


返回:
要使用的bean实例,无论是原始还是包装一个

Returns: the bean instance to use, either the original or a wrapped one

由于你在这些方法中什么都不做,你应该返回对象作为参数,它是bean的实例。

Since you do nothing in these methods you should return the Object passed as a parameter, which is the instance of the bean.

这篇关于我正在使用bean post处理器,它给出了空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆