我是SpringBoot的新手,并尝试使用内置的SpringBoot批注从YML文件中读取属性。
以下是代码:
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;
}
}
}
在其中调用AppProperty的测试类
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文件
---
patterns:
password_patterns:
pattern1: "test"
EXPECTATION:是getPattern1()方法应返回从yml文件读取的值
我针对此类问题推荐了许多帖子,但没有弄清楚我在做什么错误。我错过了任何特定的注释吗?我对
@自动接线
它与依赖注入有关,我也用过... 请建议
进一步的编辑:
关注“ Alexander pinkin”的帖子后,出现以下错误:
***************************
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
进一步编辑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
输出:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: 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
现在,控制台上什么都没有打印。既没有错误也没有异常
请提出建议
答案 0 :(得分:1)
您应该启动Spring上下文,并将属性命名为“ password-patterns”或“ passwordPatterns”。
这应该有效:
AppProperties.java:
x
DemoApplication.java
$
application.yml
add_action('admin_menu', 'ccheck_set');
function ccheck_set()
{
add_menu_page('ccheck', 'Sidebar', 'edit_posts', 'ccheck', 'ccheck', 'dashicons-media-spreadsheet');
}
function ccheck()
{
if($_POST['radio_clr'])
{
if (!empty(get_option('radio_clr', $_POST['radio_clr'])))
{
update_option('radio_clr', $_POST['radio_clr']);
}
else
{
add_option('radio_clr', $_POST['radio_clr']);
}
}
echo '<form method="POST"><table>
<tr><td ><input type="radio" name="radio_clr" value="0" >red
<input type="radio" name="radio_clr" value="1" >Black</td></tr>
<tr><td><input type="submit" name="submit"></td></tr>
</table></form>';
}
答案 1 :(得分:0)
您可以使用@Value批注 如果您的yml配置为
management:
security:
enabled: true
比您可以在@Configuration上使用的
@Value("${management.security.enabled}")
bolean managementSecurityenabled;
答案 2 :(得分:0)
看起来像缩进问题。为确认起见,放置了任何其他直接属性
amazon:
associateId: habuma-20
尝试获取成功,然后尝试如下使用。
patterns:
password_patterns:
pattern1: "test"
如果上述方法不起作用,请尝试从“测试”中删除引号。
注意:避免使用标签http://yaml.org/faq.html
https://docs.saltstack.com/en/latest/topics/troubleshooting/yaml_idiosyncrasies.html
答案 3 :(得分:0)
首先,您应该修复缩进:
patterns:
password_patterns:
pattern1: "test"
到
patterns:
password_patterns:
pattern1: "test"
要注意的第二件事-您已将属性类标记为@Component
,以便以后可以在春季上下文中找到它(我也建议在这里使用@Configuration
,因为它会更自然是一种配置),因此当您想将其与填充的配置属性一起使用时,应从上下文中获取它。
下一段代码将使您能够使用bean中的配置数据:
@Autowired
private AppProperties appProperties;
第三件事-确保@Configuration
文件中的某处具有@EnableConfigurationProperties
批注
在狭窄的patterns
标签下使用整个AppProperties类也不是很明显...
答案 4 :(得分:0)
除了 @Alexander Pankin 的答案外,程序可能会要求您在 pom.xml
中添加dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>