Java @未正确设置值批注
这是我的应用程序:Java @未正确设置值批注,java,spring,annotations,Java,Spring,Annotations,这是我的应用程序: public static void main( String[] args ) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class); //run the importer final ImportNewOrders importer = (ImportNewOrders) ApplicationContextP
public static void main( String[] args ) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);
//run the importer
final ImportNewOrders importer = (ImportNewOrders) ApplicationContextProvider.getApplicationContext().getBean("importNewOrders");
importer.run();
//importer.runInBackground();
}
这是我的配置:
@Configuration
@ComponentScan(basePackages = {
"com.production"
})
@PropertySource(value = {
"classpath:/application.properties",
"classpath:/environment-${MY_ENVIRONMENT}.properties"
})
@EnableJpaRepositories("com.fettergroup.production.repositories")
@EnableTransactionManagement
public class Config {
.... skipping things that aren't relevant
@Bean
public ImportNewOrders importNewOrders() {
return new ImportNewOrders();
}
这是我的班级
@Component
public class ImportNewOrders implements Task {
private final static Logger logger = Logger.getLogger(ImportNewOrders.class.getName());
@Autowired
private OrderService orderService;
@Autowired
private ImportOrderRequest importOrderRequest;
@Value("${api.user}")
private String apiUser;
@Value("${api.password}")
private String apiPassword;
@Value("${api.orders.pingFrequency}")
private String pingFrequency;
最后是应用程序.properties
:
# ------------------- Application settings -------------------
#Base URL to the API application
api.baseUrl=http://localhost:9998
#Unique token used to authenticate this vendor
api.vendor.token=asdf
#API credentials
api.user=myuser
api.password=mypassword
#How often to check for new orders; frequency is in seconds
api.orders.pingFrequency=60
这在一两个小时前起作用,现在它决定不喜欢这些值。我不知道为什么。我觉得一切都很正常
更新
@Configuration
@ComponentScan(basePackages = {
"com.production"
})
@PropertySource(value = {
"classpath:/application.properties",
"classpath:/environment-${MY_ENVIRONMENT}.properties"
})
@EnableJpaRepositories("com.production.repositories")
@EnableTransactionManagement
public class Config {
@Value("${db.url}")
private static String PROPERTY_DATABASE_URL;
@Bean
public DataSource dataSource() {
MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setUrl(PROPERTY_DATABASE_URL); //is null
/*dataSource.setUser(environment.getRequiredProperty(PROPERTY_NAME_DATABASE_USER));
dataSource.setPassword(environment.getRequiredProperty(PROPERTY_NAME_DATABASE_PASSWORD));*/
return dataSource;
}
@Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer () {
return new PropertySourcesPlaceholderConfigurer();
}
}
您的属性文件由
@配置
找到,并且由于@PropertySource
的原因,正在将其用于该类中的数据库属性。但是@Value
字段和${}
评估需要的不仅仅是这些
从
为了解析定义或中的${…}占位符
@必须使用PropertySource中的属性进行值注释
注册PropertySourcesPlaceholderConfigurer。这种情况会发生
在XML中使用时自动,但
使用时必须使用静态@Bean方法显式注册
@配置类。请参阅“使用外部化值”
@Configuration Javadoc和“关于
返回@Bean Javadoc的@Bean方法”的BeanFactory后处理器
细节和例子
因此,请宣布
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer p = new PropertySourcesPlaceholderConfigurer();
p.setLocation(new ClassPathResource("your properties path"));
// other properties
return p;
}
如果使用@PropertySource
,则可以在您的配置类中,或者如ach在注释中恰当地提到的那样,完全忽略设置位置
:
@Configuration
@PropertySource(value="classpath:your_file.properties")
public class MyConfiguration{
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer p = new PropertySourcesPlaceholderConfigurer();
return p;
}
}
当您拥有属性资源占位符配置器时,不应该需要环境
然而,在大多数情况下,应用程序级bean不需要>
直接与环境交互,但可能需要
${…}属性值被属性占位符配置器替换
例如PropertySourcesPlaceholderConfigurer,它本身是
默认情况下,当
使用
您的属性文件由@配置
找到,并且由于@PropertySource
的原因,正在将其用于该类中的数据库属性。但是@Value
字段和${}
评估需要的不仅仅是这些
从
为了解析定义或中的${…}占位符
@必须使用PropertySource中的属性进行值注释
注册PropertySourcesPlaceholderConfigurer。这种情况会发生
在XML中使用时自动,但
使用时必须使用静态@Bean方法显式注册
@配置类。请参阅“使用外部化值”
@Configuration Javadoc和“关于
返回@Bean Javadoc的@Bean方法”的BeanFactory后处理器
细节和例子
因此,请宣布
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer p = new PropertySourcesPlaceholderConfigurer();
p.setLocation(new ClassPathResource("your properties path"));
// other properties
return p;
}
如果使用@PropertySource
,则可以在您的配置类中,或者如ach在注释中恰当地提到的那样,完全忽略设置位置
:
@Configuration
@PropertySource(value="classpath:your_file.properties")
public class MyConfiguration{
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer p = new PropertySourcesPlaceholderConfigurer();
return p;
}
}
当您拥有属性资源占位符配置器时,不应该需要环境
然而,在大多数情况下,应用程序级bean不需要>
直接与环境交互,但可能需要
${…}属性值被属性占位符配置器替换
例如PropertySourcesPlaceholderConfigurer,它本身是
默认情况下,当
使用
今天我要帮你做一切。没有找到这些属性吗?还是你有别的错误?哈哈。。。找到属性后,我可以进行验证,因为该文件还包含数据库连接详细信息,运行正常。我发现我的代码中有错误,这是由于apiPassword=“${api.password}”
您是否在数据库字段中使用@Value
造成的?今天我将帮助您解决所有问题。没有找到这些属性吗?还是你有别的错误?哈哈。。。找到属性后,我可以进行验证,因为该文件还包含数据库连接详细信息,运行正常。我在代码中看到错误,这是由于apiPassword=“${api.password}”
是否对数据库字段使用@Value
造成的?您甚至不需要设置位置,只需返回新的PropertySourcePlaceHolderConfigurer()
只要你有@PropertySources
指向你的属性文件。亲爱的,我不知道这一点。当我为此添加一个bean时,它不再自动连接环境
进入我的配置类。@Webnet尝试使属性资源占位符配置器bean方法静态。你在春季3.1+?@SotiriosDelimanolis-就是这样。。。非常感谢。我在3.2.1上,你甚至不需要设置位置,只要你有@PropertySources
指向你的属性文件,你就可以返回新的propertySourcesplaceConfigurer()
进入我的配置类。@Webnet尝试使属性资源占位符配置器bean方法静态。你在春季3.1+?@SotiriosDelimanolis-就是这样。。。非常感谢。我在3.2.1上