繁体   English   中英

@Autowired 在 Spring Boot 中为 Null,但同样可以通过应用程序上下文访问

[英]@Autowired is Null in Spring Boot but same is accessible through Application Context

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

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

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

第一步:使用@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);

            }
        }
}

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

我已经使用上面的步骤 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"));

}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM