@ConfigurationProperties 不适用于 .YAML 文件 [英] @ConfigurationProperties is not working with .YAML files
问题描述
我是 SpringBoot 的新手,并尝试使用内置的 SpringBoot 注释从 YML 文件中读取属性.
代码如下:
<块引用>AppProperties.java
@Component@ConfigurationProperties(prefix = "patterns")公共类 AppProperties {私人列表<PasswordPattern>password_patterns = new ArrayList<>();公共静态类 PasswordPattern {私人字符串模式1;@自动连线公共字符串 getPattern1() {返回模式1;}public void setPattern1(String pattern1) {this.pattern1 = 模式 1;}}}
调用 AppProperties 的测试类
<块引用>Test1.java
公共类Test1 {public static void main(String[] args) 抛出 JsonParseException, JsonMappingException, IOException {//TODO 自动生成的方法存根AppProperties.PasswordPattern a = new AppProperties.PasswordPattern();System.out.println(a.getPattern1());}}
<块引用>
application.yml 文件
---图案:密码模式:模式1:测试"
期望:是 getPattern1() 方法应该返回从 yml 文件读取的值
我参考了很多关于此类问题的帖子,但没有弄明白我在做什么错误.我错过了任何特定的注释吗?我红了
<块引用>@Autowired
它与依赖注入有关,我也使用过...请推荐
进一步
在关注Alexander pinkin"的帖子后,出现以下错误:
***************************应用程序无法启动****************************说明:无法将patterns.password-patterns"下的属性绑定到 java.util.Map<java.lang.String, java.lang.String>:原因:找不到能够从类型 [java.lang.String 进行转换的转换器] 输入 [java.util.Map]行动:更新应用程序的配置
进一步编辑 2:
<块引用>AppProperties.java
@Component@ConfigurationProperties(prefix = "patterns")公共类 AppProperties {私人列表<菜单>menus = new ArrayList<>();公共静态类菜单{私人字符串模式1;公共字符串 getPattern1() {返回模式1;}public void setPattern1(String pattern1) {this.pattern1 = 模式 1;}}}
<块引用>
密码patApplication.java
@SpringBootApplication@EnableConfigurationProperties(AppProperties.class)公共类 PasswordpatApplication {private static final Logger LOG = LoggerFactory.getLogger(PasswordpatApplication.class);@自动连线公共静态无效主(字符串 [] args){SpringApplication.run(PasswordpatApplication.class, args);AppProperties.Menu a = new AppProperties.Menu();LOG.info("你好");LOG.info(a.getPattern1());}
<块引用>
应用程序.yml
模式:密码模式:模式1:测试
输出:
.____ _ _ _/\\/___'_ __ _ _(_)_ __ __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/_` | \ \ \ \\\/___)||_)|||||||(_| | ) ) ) )' |____|.__|_||_|_||_\__, |////==========|_|==============|___/=/_/_/_/:: Spring Boot :: (v2.0.4.RELEASE)2018-08-24 18:56:21.116 INFO 11712 --- [主要] c.e.passwordpat.PasswordpatApplication:在 PUNITP13 上启动 PasswordpatApplication v0.0.1-SNAPSHOT0066L 与 PID 11712 (C:\Users\irfan.sayed\Downloads\passwordpat\target\passwordpat-0.0.1-SNAPSHOT.jar 由 irfan.sayed 启动,位于 C:\Users\irfan.sayed\Downloads\密码\目标)2018-08-24 18:56:21.131 INFO 11712 --- [主要] c.e.passwordpat.PasswordpatApplication :未设置活动配置文件,回退到默认配置文件:de故障2018-08-24 18:56:21.271 信息 11712 --- [主要] s.c.a.AnnotationConfigApplicationContext : 刷新 org.springframework.context.annotation.AnnotationConfigApplicationContext@6c629d6e:启动日期 [星期五 8 月 24 日 18:56:21 IST 2018];上下文层次结构的根2018-08-24 18:56:22.066 WARN 11712 --- [主要] f.a.AutowiredAnnotationBeanPostProcessor :静态方法不支持自动装配注释:public static void com.example.passwordpat.PasswordpatApplication.main(java.lang.S字符串[])2018-08-24 18:56:23.887 INFO 11712 --- [主要] o.s.j.e.a.AnnotationMBeanExporter :在启动时为 JMX 公开注册 bean2018-08-24 18:56:23.920 INFO 11712 --- [主要] c.e.passwordpat.PasswordpatApplication : 在 3.484 秒内启动 PasswordpatApplication (JVM running 为 4.104)2018-08-24 18:56:23.929 INFO 11712 --- [主要] c.e.passwordpat.Passwordpat应用程序:嗨2018-08-24 18:56:23.931 INFO 11712 --- [主要] c.e.passwordpat.PasswordpatApplication : 空2018-08-24 18:56:23.938 INFO 11712 --- [ Thread-2] s.c.a.AnnotationConfigApplicationContext : 关闭 org.springframework.context.annotation.AnnotationConfigApplicationContext@6c629d6e:启动日期 [星期五 8 月 24 日 18:56:21 IST 2018];罗上下文层次结构2018-08-24 18:56:23.946 INFO 11712 --- [ Thread-2] o.s.j.e.a.AnnotationMBeanExporter : 在关闭时注销暴露于 JMX 的 bean
现在,控制台上没有打印任何内容.没有任何错误或异常
请推荐
您应该启动 Spring 上下文并将您的属性命名为password-patterns"或passwordPatterns".
这应该有效:
<块引用>AppProperties.java:
@ConfigurationProperties(prefix = "patterns")公共类 AppProperties {私有映射<字符串,字符串>passwordPatterns = new HashMap<>();公共地图<字符串,字符串>获取密码模式(){返回密码模式;}}
<块引用>
DemoApplication.java
@SpringBootApplication@EnableConfigurationProperties(AppProperties.class)公共类 DemoApplication 实现 CommandLineRunner {私有静态最终记录器 LOG = LoggerFactory.getLogger(DemoApplication.class);@自动连线私有 AppProperties appProperties;公共静态无效主(字符串 [] args){SpringApplication.run(DemoApplication.class, args);}@覆盖公共无效运行(字符串...参数){LOG.info("pattern = {}", appProperties.getPasswordPatterns().get("pattern1"));}}
<块引用>
应用程序.yml
模式:密码模式:模式1:测试"
i am newbie to SpringBoot and trying to read the properties from YML file using in built SpringBoot annotations.
following is the code:
AppProperties.java
@Component
@ConfigurationProperties(prefix = "patterns")
public class AppProperties {
private List<PasswordPattern> password_patterns = new ArrayList<>();
public static class PasswordPattern {
private String pattern1;
@Autowired
public String getPattern1() {
return pattern1;
}
public void setPattern1(String pattern1) {
this.pattern1 = pattern1;
}
}
}
Test class where AppProperties being called
Test1.java
public class Test1 {
public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
// TODO Auto-generated method stub
AppProperties.PasswordPattern a = new AppProperties.PasswordPattern();
System.out.println(a.getPattern1());
}
}
application.yml file
---
patterns:
password_patterns:
pattern1: "test"
EXPECTATION: is that getPattern1() method should return the value read from the yml file
i referred many posts for such issues , but not getting what is the mistake i am doing. have i missed any specific annotation ? i red about
@Autowired
it is related to dependency injection and i have used that also ... please suggest
FURTHER EDITS:
after following the post from "Alexander pinkin" , getting following error :
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to bind properties under 'patterns.password-patterns' to java.util.Map<ja
va.lang.String, java.lang.String>:
Reason: No converter found capable of converting from type [java.lang.String
] to type [java.util.Map<java.lang.String, java.lang.String>]
Action:
Update your application's configuration
FURTHER EDIT 2:
AppProperties.java
@Component
@ConfigurationProperties(prefix = "patterns")
public class AppProperties {
private List<Menu> menus = new ArrayList<>();
public static class Menu {
private String pattern1;
public String getPattern1() {
return pattern1;
}
public void setPattern1(String pattern1) {
this.pattern1 = pattern1;
}
}
}
PasswordpatApplication.java
@SpringBootApplication
@EnableConfigurationProperties(AppProperties.class)
public class PasswordpatApplication {
private static final Logger LOG = LoggerFactory.getLogger(PasswordpatApplication.class);
@Autowired
public static void main(String[] args) {
SpringApplication.run(PasswordpatApplication.class, args);
AppProperties.Menu a = new AppProperties.Menu();
LOG.info("hi");
LOG.info(a.getPattern1());
}
application.yml
patterns:
password-patterns:
pattern1: test
OUTPUT:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.4.RELEASE)
2018-08-24 18:56:21.116 INFO 11712 --- [ main] c.e.passwordpat.Passwo
rdpatApplication : Starting PasswordpatApplication v0.0.1-SNAPSHOT on PUNITP13
0066L with PID 11712 (C:\Users\irfan.sayed\Downloads\passwordpat\target\password
pat-0.0.1-SNAPSHOT.jar started by irfan.sayed in C:\Users\irfan.sayed\Downloads\
passwordpat\target)
2018-08-24 18:56:21.131 INFO 11712 --- [ main] c.e.passwordpat.Passwo
rdpatApplication : No active profile set, falling back to default profiles: de
fault
2018-08-24 18:56:21.271 INFO 11712 --- [ main] s.c.a.AnnotationConfig
ApplicationContext : Refreshing org.springframework.context.annotation.Annotatio
nConfigApplicationContext@6c629d6e: startup date [Fri Aug 24 18:56:21 IST 2018];
root of context hierarchy
2018-08-24 18:56:22.066 WARN 11712 --- [ main] f.a.AutowiredAnnotatio
nBeanPostProcessor : Autowired annotation is not supported on static methods: pu
blic static void com.example.passwordpat.PasswordpatApplication.main(java.lang.S
tring[])
2018-08-24 18:56:23.887 INFO 11712 --- [ main] o.s.j.e.a.AnnotationMB
eanExporter : Registering beans for JMX exposure on startup
2018-08-24 18:56:23.920 INFO 11712 --- [ main] c.e.passwordpat.Passwo
rdpatApplication : Started PasswordpatApplication in 3.484 seconds (JVM runnin
g for 4.104)
2018-08-24 18:56:23.929 INFO 11712 --- [ main] c.e.passwordpat.Passwo
rdpatApplication : hi
2018-08-24 18:56:23.931 INFO 11712 --- [ main] c.e.passwordpat.Passwo
rdpatApplication : null
2018-08-24 18:56:23.938 INFO 11712 --- [ Thread-2] s.c.a.AnnotationConfig
ApplicationContext : Closing org.springframework.context.annotation.AnnotationCo
nfigApplicationContext@6c629d6e: startup date [Fri Aug 24 18:56:21 IST 2018]; ro
ot of context hierarchy
2018-08-24 18:56:23.946 INFO 11712 --- [ Thread-2] o.s.j.e.a.AnnotationMB
eanExporter : Unregistering JMX-exposed beans on shutdown
now , nothing is getting printed on the console. neither any error OR exception
please suggest
You should start Spring context and name your property "password-patterns" or "passwordPatterns".
This should work:
AppProperties.java:
@ConfigurationProperties(prefix = "patterns")
public class AppProperties {
private Map<String, String> passwordPatterns = new HashMap<>();
public Map<String, String> getPasswordPatterns() {
return passwordPatterns;
}
}
DemoApplication.java
@SpringBootApplication
@EnableConfigurationProperties(AppProperties.class)
public class DemoApplication implements CommandLineRunner {
private static final Logger LOG = LoggerFactory
.getLogger(DemoApplication.class);
@Autowired
private AppProperties appProperties;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void run(String... args) {
LOG.info("pattern = {}", appProperties.getPasswordPatterns().get("pattern1"));
}
}
application.yml
patterns:
password-patterns:
pattern1: "test"
这篇关于@ConfigurationProperties 不适用于 .YAML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!