【问题标题】:@Autowired is Null in Spring Boot but same is accessible through Application Context@Autowired 在 Spring Boot 中为 Null,但同样可以通过 Application Context 访问
【发布时间】:2020-03-23 11:55:03
【问题描述】:

通过@Autowired,我无法访问具有 @Component 注释(第 1 步:方法)的其他 java 文件中的 @Component/@Service/@Respository/@Controller 类对象第 1 步方法获取 Null 指针异常,但我可以使用相同的方法实现(第 2 步:方法)。

谁能告诉我为什么我无法使用第 1 步方法实现:

仅供参考-我已经在整个项目中进行了搜索,我没有使用/调用/初始化 @Component 类,使用 new 自动装配类的方法仍然遇到“空指针异常”的问题

第 1 步:使用@Autowired 注解

@Component
public class Processor {

        @Autowired
        PropertyConfigurator propconfigrator; --> Getting here as null pointer Exception

public void getDetails(){

System.out.println ("Application URL +propconfigrator.getProperties().getProperty("appURL"));

}
}

第 2 步:使用带/不带 @AutoWired 注释的 ApplicationContext 接口。我能够从 PropertyConfigurator java 文件中获取属性值

@Component
public class Processor {

        @Autowired
        PropertyConfigurator propconfigrator = ApplicationContextHolder.getContext().getBean(PropertyConfigurator.class);

public void getDetails(){

System.out.println ("Application URL +propconfigrator.getProperties().getProperty("appURL"));

}
}

ApplicationContextHolder.java

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class ApplicationContextHolder implements ApplicationContextAware {
    private static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;   
    }

    public static ApplicationContext getContext() {
        return context;
    }
}

PropertyConfigurator.java 文件

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

@Service
@Configurable
public class PropertyConfigurator {

    private final Properties properties;

    public Properties getProperties () {
        return properties;
    }

    public  PropertyConfigurator(){
            properties = new Properties();
            try {
  properties.load(getClass().getClassLoader().getResourceAsStream("dbconfig.properties"));
            } catch (IOException e) {
                Logger.getLogger(getClass().getName()).log(Level.SEVERE, e.getMessage(), e);

            }
        }
}

【问题讨论】:

    标签: spring-boot nullpointerexception autowired


    【解决方案1】:

    你为什么使用@Configurable注解?在您发布的代码中,它没有意义。 @Configurable 只有在这个类的实例不是由 spring 创建的情况下才需要。

    【讨论】:

    • 谢谢!!。我删除但仍然面临与“空指针异常”相同的问题。
    【解决方案2】:

    我已经使用上面的第 1 步方法更改为构造函数注入自动装配,如下所示(不使用第 2 步。它最终解决了我的问题。

    不确定为什么 Spring 在不使用构造函数自动装配的情况下无法注入 bean。

    第 1 步:在构造函数中使用 @Autowired 注解

    @Component
    public class Processor {
    
    @Autowired
        public Processor (PropertyConfigurator propconfigrator) {
            this.propconfigrator = propconfigrator;
    }
    public void getDetails(){
    
    System.out.println ("Application URL +propconfigrator.getProperties().getProperty("appURL"));
    
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-12-15
      • 2018-07-19
      • 1970-01-01
      • 2019-05-16
      • 1970-01-01
      • 1970-01-01
      • 2018-06-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多