【问题标题】:Not getting integer value from properties file没有从属性文件中获取整数值
【发布时间】:2019-10-31 01:09:05
【问题描述】:

我想从我在 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(Native Method) ~[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) ~[classes!/:0.0.1-SNAPSHOT] 在 com.abc.ci.unitamqpservice.UnitAmqpRestController$$EnhancerBySpringCGLIB$$e7ff3f42.() ~[classes!/:0.0.1-SNAPSHOT] 在 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[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());

【问题讨论】:

    标签: java spring spring-boot rabbitmq


    【解决方案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 连接和属性,而不是自己管理。

    【讨论】:

      【解决方案2】:

      application.propertiesyml 中有spring.rabbitmq.port=31111,使用@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 );
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2013-02-08
        • 2020-08-18
        • 1970-01-01
        • 2013-05-07
        • 2012-12-18
        • 1970-01-01
        • 1970-01-01
        • 2023-01-10
        • 1970-01-01
        相关资源
        最近更新 更多