如何修复构造函数注入中的空指针异常-Spring

时间:2018-10-16 21:01:37

标签: java spring nullpointerexception constructor-injection

我有一个Spring类,如下所示:

package com.abc.mysource.mypackage;

@Component
public class MyHelper {

    @Autowired
    @Qualifier("helperClock")
    Clock helperClock;

    LocalDate NEW_YEAR_DATE=LocalDate.of(2018, Month.January, 1);
    int holidayCountInCurrentYear = 45;

    //A few helper methods that uses these constants
}

我想外部化这些属性(即将它们存储到文件中,然后从该文件中读取到Java对象中)

default.propertiessrc/main/java

myhelper.newYearMonth=JANUARY
myhelper.newYearDay=1
myhelper.holidayCountInCurrentYear=45

与属性对应的Bean:

package com.abc.mysource.mypackage.config;

@Component
public class CommonProperties {
    private String newYearMonth;
    private int newYearDay;
    private int holidayCountInCurrentYear;

    //Getters and Setters
}

用于初始化Bean的配置类:

@Configuration
@ComponentScan(basePackages ="com.abc.mysource.mypackage.*")
@PropertySource("classpath:default.properties")
public class CommonConfig {
    @Autowired
    private Environment env; 

    @Bean(name="commonProperties")
    @Qualifier("commonProperties")
    public CommonProperties commonProperties() throws Exception { 
        CommonProperties commonProperties = new CommonProperties();
        commonProperties.setNewYearMonth(env.getProperty("myhelper.newYearMonth"));
        commonProperties.setNewYearDay(Integer.valueOf(env.getProperty("myhelper.newYearDay")));        commonProperties.setHolidayCountInCurrentYear(Integer.valueOf(env.getProperty("myhelper.holidayCountInCurrentYear")));
        System.out.println("Common properties values set");//Gets printed
        return commonProperties;
        }
    }

修改后最后MyHelper个类:

@Component
public class MyHelper {

    @Autowired
    @Qualifier("helperClock")
    Clock helperClock;

    @Autowired
    @Qualifier("commonProperties")
    private CommonProperties commonProperties;

    LocalDate NEW_YEAR_DATE=LocalDate.of(2018, commonProperties.getNewYearMonth(), commonProperties.getNewYearDay());
    int holidayCountInCurrentYear = commonProperties.getHolidayCountinCurrentYear();

    //A few helper methods that uses these constants
}   

我可以看到正在打印“设置公共属性值”。因此,CommonConfig.commonProperties()肯定被称为。

我收到以下错误:

Error starting ApplicationContext. To display the conditions report
re-run your application with 'debug' enabled. 2018-10-16 16:54:05.569
ERROR 15396 --- [  restartedMain] o.s.boot.SpringApplication          
: Application run failed

org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'myHelper': Instantiation of bean failed;
nested exception is
org.springframework.beans.BeanInstantiationException: Failed to
instantiate [com.abc.mysource.mypackage.HolidayHelper]: Constructor
threw exception; nested exception is java.lang.NullPointerException
   at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1229)
~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]     at

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1128)
~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]     at 
..................................................
..................................................
..................................................
..................................................
..................................................
                 Caused by: java.lang.NullPointerException: null    at com.abc.mysource.mypackage.MyHelper.<init>(MyHelper.java:8)
~[classes/:na]  at
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
~[na:1.8.0_161]     at

sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
~[na:1.8.0_161]     at

sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
~[na:1.8.0_161]     at
java.lang.reflect.Constructor.newInstance(Constructor.java:423)
~[na:1.8.0_161]     at
org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:170)
~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]     ... 24 common frames
omitted`

此外,此代码是从具有自己的Config类-HelperConfig.java

的另一个项目中调用的

HelperConfig.java

package com.abc.externalsource.externalpackage;

@Import(CommonConfig.class)
@Configuration
public class HelperConfig {
    @Qualifier("helperClock")
    @Bean
    public Clock helperClock() {
        return Clock.systemDefaultZone();
    }
}

0 个答案:

没有答案