无法从属性文件中获取整数值

时间:2019-06-17 12:28:32

标签: java spring spring-boot rabbitmq

我想从我在application.properties文件中定义的键中读取整数值。这是我的属性文件。

  

spring.rabbitmq.port = 31111

这是代码:

public Object getPortProperty() {
    Properties properties = new Properties();
    try {
        File file = ResourceUtils.getFile("classpath:application.properties");
        InputStream in = new FileInputStream(file);
        properties.load(in);
    } catch (IOException e) {

    }
    return (Integer) properties.get("spring.rabbitmq.port");

}

@Autowired
public UnitAmqpRestController(RabbitTemplate rabbitTemplate) throws IOException, TimeoutException {
    this.rabbitTemplate = rabbitTemplate;
    rabbitTemplate.setReplyTimeout(15_000L);
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(getHostProperty());
    factory.setPort((Integer) getPortProperty());
    connection = factory.newConnection();
    channel = connection.createChannel();
}

出现以下错误:

  

org.springframework.beans.factory.BeanCreationException:错误   在URL中创建名称为“ unitAmqpRestController”的bean   [jar:file:/tmp/unitamqpservice-0.0.1-SNAPSHOT.jar!/ BOOT-INF / classes!/com/abc/ci/unitamqpservice/UnitAmqpRestController.class]:   通过构造函数实例化Bean失败;嵌套异常为   org.springframework.beans.BeanInstantiationException:失败   实例化   [com.abc.ci.unitamqpservice.UnitAmqpRestController $$ EnhancerBySpringCGLIB $$ e7ff3f42]:   构造函数抛出异常;嵌套异常为   java.lang.NullPointerException           在org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:279)   〜[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE]           在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1193)处   〜[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE]           在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1095)   〜[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE]           在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)   〜[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE]           在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)   〜[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE]           在org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:306)   〜[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE]           在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)   〜[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE]           在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)   〜[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE]           在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)   〜[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE]           在org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)   〜[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE]           在org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)   〜[spring-context-4.3.14.RELEASE.jar!/:4.3.14.RELEASE]           在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)   〜[spring-context-4.3.14.RELEASE.jar!/:4.3.14.RELEASE]           在org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)   〜[spring-boot-1.5.10.RELEASE.jar!/:1.5.10.RELEASE]           在org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)   [spring-boot-1.5.10.RELEASE.jar!/:1.5.10.RELEASE]           在org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)   [spring-boot-1.5.10.RELEASE.jar!/:1.5.10.RELEASE]           在org.springframework.boot.SpringApplication.run(SpringApplication.java:303)   [spring-boot-1.5.10.RELEASE.jar!/:1.5.10.RELEASE]           在org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)   [spring-boot-1.5.10.RELEASE.jar!/:1.5.10.RELEASE]           在org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)   [spring-boot-1.5.10.RELEASE.jar!/:1.5.10.RELEASE]           在com.abc.ci.unitamqpservice.UnitamqpserviceApplication.main(UnitamqpserviceApplication.java:10)   [课程!/:0.0.1-SNAPSHOT]           在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)〜[na:1.8.0_212]           在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)   〜[na:1.8.0_212]           在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)   〜[na:1.8.0_212]           在java.lang.reflect.Method.invoke(Method.java:498)〜[na:1.8.0_212]           在org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)   [unitamqpservice-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]           在org.springframework.boot.loader.Launcher.launch(Launcher.java:87)   [unitamqpservice-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]           在org.springframework.boot.loader.Launcher.launch(Launcher.java:50)   [unitamqpservice-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]           在org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)   [unitamqpservice-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]由以下原因引起:   org.springframework.beans.BeanInstantiationException:失败   实例化   [com..ci.unitamqpservice.UnitAmqpRestController $$ EnhancerBySpringCGLIB $$ e7ff3f42]:   构造函数抛出异常;嵌套异常为   java.lang.NullPointerException           在org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:154)   〜[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE]           在org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:122)   〜[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE]           在org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:271)   〜[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE]           ...省略了26个常见框架造成原因:java.lang.NullPointerException:null           在com.abc.ci.unitamqpservice.UnitAmqpRestController。(UnitAmqpRestController.java:75)   〜[课程!/:0.0.1-SNAPSHOT]           在com.abc.ci.unitamqpservice.UnitAmqpRestController $$ EnhancerBySpringCGLIB $$ e7ff3f42。()   〜[课程!/:0.0.1-SNAPSHOT]           在sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)处   〜[na:1.8.0_212]           在sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)   〜[na:1.8.0_212]           在sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)   〜[na:1.8.0_212]           在java.lang.reflect.Constructor.newInstance(Constructor.java:423)   〜[na:1.8.0_212]           在org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:142)   〜[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE]           ...省略了28个常见框架

我无法理解为什么它不能与整数一起使用。如果我有一个字符串值,它会完美地工作。 请提出代码中的错误

有问题的代码行是:

factory.setPort((Integer) getPortProperty());

2 个答案:

答案 0 :(得分:1)

您的代码抛出NullPointerException,并且您有一个空的catch块。至少应在其中添加一些日志记录,以查看发生了什么情况。

此外,您正在使用Spring,因此几乎不需要手动解析application.properties;它会自动加载到应用程序环境中。

因此,您可以使用@Value将其绑定到本地变量(与主机属性相同):

@Value("${spring.rabbitmq.port}")
private int mqPort;

@Value("${spring.rabbitmq.host}")
private String mqHost;

@Autowired
public UnitAmqpRestController(RabbitTemplate rabbitTemplate) throws IOException, TimeoutException {
    this.rabbitTemplate = rabbitTemplate;
    rabbitTemplate.setReplyTimeout(15_000L);
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(mqHost);
    factory.setPort(mqPort);
    connection = factory.newConnection();
    channel = connection.createChannel();
}

此外,您应该考虑使用Spring Boot Rabbit MQ starter模块来管理Rabbit MQ连接和属性,而不是自己进行管理。

答案 1 :(得分:1)

spring.rabbitmq.port=31111application.properties中有yml,请使用@Value批注读取它,该批注读取属性并解析为正确的类型:

@Value("${spring.rabbitmq.port=31111}")
private int port;

使用@Configuration@Component的任何变体,例如@Service,应将类本身识别为bean。

如果要像使用方法一样使用方法加载属性,请考虑对整数使用特殊方法:

private int getIntegerProperty(final String key, final int defaultValue) {
    final Object object = properties.get("spring.rabbitmq.port")
    if (object == null || "".equals(object.toString())) {
        return defaultValue;
    } else {
        return Integer.parseInt(object );
    }
}