在Spring-Boot 4中,尝试注入属性文件值

时间:2015-09-28 21:33:24

标签: java spring-boot properties-file

基本上,我想要做的就是将我的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

1 个答案:

答案 0 :(得分:2)

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

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