【问题标题】:In Spring-Boot 4, trying to inject property file values在 Spring-Boot 4 中,尝试注入属性文件值
【发布时间】:2015-12-26 05:42:06
【问题描述】:

基本上,我要做的就是将 application.properties 文件中的值注入 POJO。我已经尝试了很多不同的方法,包括使用 @Value 注释,尝试 @Autowite 环境,以及使用 @ConfigurationProperties 将属性条目映射到命名字段。在所有情况下,我都无法让注射起作用。

奇怪的是,我能够在我的一个表单控制器中仅使用 @Value 注释轻松地进行注入。我确实尝试将 POJO 注释为 @Service。

下面粘贴的代码反映了我使用@ConfigurationProperties 注释的最新尝试,但已注释掉早期尝试的代码。

我希望我错过了一些非常基本的东西,但我看不出是什么,我非常感谢一些帮助。谢谢!

import java.time.ZonedDateTime;
import java.util.logging.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

//@Configuration
//@ComponentScan(basePackages = "xxx.xxx.ua")
//@PropertySource(value = {"classpath:application.properties"})

@Configuration
@ConfigurationProperties(locations="classpath:application.properties", ignoreUnknownFields=false, prefix="rabbit")
public class Rabbit 
{
    //@Value("${rabbit_un}")
    private String user;

    //@Value("${rabbit-pw}")
    private String password;

    //@Value("${rabbit-queue}")
    private String queue;

    //@Value("${rabbit-host}")
    private String host;

    private Connection connection;
    private Channel channel;



/*
 * PropertySourcesPlaceHolderConfigurer Bean only required for @Value("{}") annotations.
 * Remove this bean if you are not using @Value annotations for injecting properties.
 */
/*
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
}
*/  

public Rabbit()
{

    ConnectionFactory factory = new ConnectionFactory();

    //rabbitUn = "guest";
    //rabbitPassword = "guest";
    //rabbitHost = "72.xx.xx.xx";
    //rabbitQueue = "hello";

    factory.setUsername(user);
    factory.setPassword(password);
    factory.setHost(host);

    try 
    {
        connection = factory.newConnection();
        channel = connection.createChannel();

        channel.queueDeclare(queue, false, false, false, null);

        Logger.getGlobal().fine(() -> "Successfully connected to RabbitMQ");
    }
    catch(Exception e)
    {
Logger.getGlobal().severe(() -> "FAILED connecting to RabbitMQ");
    }


    // channel.close();
    // connection.close();
}

void logLoginUserNameNotFound(String s)
{
}

void logLoginBadPassword(String s)
{
}


void logLoginSuccess(String s)
{
}

private void send(String s)
{
}
}

属性文件:

# Control what port the Sprint Boot app will be listening on
server.port=8080

# Disable Tomcat session timeout
server.session-timeout=-1

# Application specific properties
enable_admin_server=false

rabbit.user=guest
rabbit.password=guest
rabbit.queue=hello
rabbit.host=72.xx.xx.xx

# Valid choices are: SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST
logger_lever=FINE

【问题讨论】:

  • 您使用的是Spring Boot,只需添加spring-boot-starter-rabbitmq并注入Spring Boot已经为您配置的AmqpTemplate或ConnectionFactory,无需自己做。关于记录器,我强烈建议直接使用 slf4j 外观而不是 JUL,尤其是在 Spring Boot 中。

标签: java spring-boot properties-file


【解决方案1】:

这里的问题原来是 Spring 直到对象创建之后才注入值。也就是说,在将值传递给 POJO 之前调用了构造函数。

我通过删除构造函数解决了这个问题,并添加了一个方法来完成构造函数所做的事情。然后我用@PostConstruct 注释了这个方法,这导致在注入值之后调用该方法。经验教训。

【讨论】:

  • 如何将值注入到一个甚至不存在的实例中,因为它正在创建...这就是 java 的工作方式,如果你尝试,你只能使用完全构造的对象访问未初始化的值将失败。这与尝试在构造函数中访问 DataSource 基本相同,首先需要通过 setter 方法进行设置,它不会在构造函数中可用。
猜你喜欢
  • 2018-10-15
  • 1970-01-01
  • 2019-06-04
  • 2018-11-26
  • 2017-03-18
  • 2020-02-07
  • 2017-08-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多