java.lang.NullPointerException:在StandAlone应用程序中自动装配Bean时为null

时间:2019-02-28 12:42:25

标签: javabeans autowired

当试图将@AutoWire功能与StandAlone应用程序之一一起使用而无法执行此操作时,却得到了Null Pointer Exception。请突出我的错误(如果有)。感谢您的帮助。

Spring Ver 5.1.5.RELEASE,并且我们没有使用任何xml配置文件来告诉spring有注释的类供您查看,而不是使用AppConfig顶部的@ComponentScan或@EnableAutoConfiguration并从main( )类作为第一行。但是,自动装配与jdk(Environment)的内部bean / java类完美协作,但不适用于自定义POJO类。如果我们试图通过getBean方法,那么它将起作用。但是,我试图避免在任何地方创建上下文并使用getBean()。请参考以下内容,仅在您提供有价值的指导时为我提供帮助。

public class ContextMaster {
private static AnnotationConfigApplicationContext appContext;
public static AnnotationConfigApplicationContext getApplicationContext() {
if (appContext == null) {
appContext =  new AnnotationConfigApplicationContext(ContextConfig.class);
//appContext =  new AnnotationConfigApplicationContext("com.xx.xx.xxx","xx.xxx.xxxx.xxx.datamanager");          
logger.debug("Context Invoked !!");
}
return appContext;
}
}

@Configuration
@EnableAutoConfiguration
@PropertySource("classpath:db.properties")
@EnableTransactionManagement
@ComponentScans(value = {
        @ComponentScan(basePackages = "xxxxx.datamanager"),
        @ComponentScan(basePackages = "com.xx.xx.xxx"),
        @ComponentScan(basePackages = "com.xx.xx.xxx.utils")})

public class AppConfig {

    @Autowired
    private Environment env;

    @Bean
    public DataSource getDataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(env.getProperty("db.driver"));
        dataSource.setUrl(env.getProperty("db.url"));
        return dataSource;
    }

    @Bean
    public LocalSessionFactoryBean getSessionFactory() {
        LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
        //LocalSessionFactoryBean sessionFactoryBean = new AnnotationSessionFactoryBean();
        factoryBean.setDataSource(getDataSource());

        Properties props=new Properties();
        props.put("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
        props.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
        props.put("hibernate.cache.region.factory_class", env.getProperty("hibernate.cache.region.factory_class"));


    factoryBean.setHibernateProperties(props);
    factoryBean.setAnnotatedClasses(xx.class, xxxx.class, xxxx.class, xxx.class);
    return factoryBean;


    }

    @Bean


    public HibernateTransactionManager getTransactionManager() {
            return transactionManager;


    }  
    }

// Here is NPE thrown when tried with auto-configured bean
    @Component
            public class Good extends Good11 {    
                @Autowired
                private RxxxDyyyyHelper rdh; 
                //RxxxDyyyyHelper rdh = 
    ContextHelper.getApplicationContext().getBean(RxxxDyyyyHelper .class);    
            rdh.setProperty(); // NPE here
            rdh.getProperty(); // NPE 

        }

// Here we're trying to initiate the LosUtils class
public class LosUtils {       
    public static void main(String args[]) throws Exception {
        AnnotationConfigApplicationContext applicationContext = `ContextHelper.getApplicationContext();`    
}

1 个答案:

答案 0 :(得分:0)

您似乎没有将完整的代码放在这里,因为您的Good类不会以这种方式编译。.