簡體   English   中英

在 Spring-Boot 4 中,嘗試注入屬性文件值

[英]In Spring-Boot 4, trying to inject property file values

基本上,我想要做的就是將 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 在創建對象之后才注入值。 也就是說,在將值傳遞給 POJO 之前調用了構造函數。

我通過刪除構造函數解決了這個問題,並添加了一個方法來完成構造函數會做的事情。 然后我用@PostConstruct 注釋了這個方法,這導致在注入值后調用該方法。 學過的知識。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM