【发布时间】: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