![irfan irfan]()
本文介绍了@ConfigurationProperties 不适用于 .YAML 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我是 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:@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 calledpublic 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()); }}---patterns: password_patterns: pattern1: "test"EXPECTATION: is that getPattern1() method should return the value read from the yml filei 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 it is related to dependency injection and i have used that also ...please suggestFURTHER 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<java.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 configurationFURTHER EDIT 2:@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; } }}@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()); }patterns: password-patterns: pattern1: testOUTPUT: . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.0.4.RELEASE)2018-08-24 18:56:21.116 INFO 11712 --- [ main] c.e.passwordpat.PasswordpatApplication : Starting PasswordpatApplication v0.0.1-SNAPSHOT on PUNITP130066L with PID 11712 (C:\Users\irfan.sayed\Downloads\passwordpat\target\passwordpat-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.PasswordpatApplication : No active profile set, falling back to default profiles: default2018-08-24 18:56:21.271 INFO 11712 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@6c629d6e: startup date [Fri Aug 24 18:56:21 IST 2018]; root of context hierarchy2018-08-24 18:56:22.066 WARN 11712 --- [ main] f.a.AutowiredAnnotationBeanPostProcessor : Autowired annotation is not supported on static methods: public static void com.example.passwordpat.PasswordpatApplication.main(java.lang.String[])2018-08-24 18:56:23.887 INFO 11712 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup2018-08-24 18:56:23.920 INFO 11712 --- [ main] c.e.passwordpat.PasswordpatApplication : Started PasswordpatApplication in 3.484 seconds (JVM running for 4.104)2018-08-24 18:56:23.929 INFO 11712 --- [ main] c.e.passwordpat.PasswordpatApplication : hi2018-08-24 18:56:23.931 INFO 11712 --- [ main] c.e.passwordpat.PasswordpatApplication : null2018-08-24 18:56:23.938 INFO 11712 --- [ Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@6c629d6e: startup date [Fri Aug 24 18:56:21 IST 2018]; root of context hierarchy2018-08-24 18:56:23.946 INFO 11712 --- [ Thread-2] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdownnow , nothing is getting printed on the console. neither any error OR exceptionplease suggest 解决方案 You should start Spring context and name your property "password-patterns" or "passwordPatterns".This should work:@ConfigurationProperties(prefix = "patterns")public class AppProperties { private Map<String, String> passwordPatterns = new HashMap<>(); public Map<String, String> getPasswordPatterns() { return passwordPatterns; }}@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")); }}patterns: password-patterns: pattern1: "test" 这篇关于@ConfigurationProperties 不适用于 .YAML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-19 03:13