Java 自己的配置属性
在第5章之后,我尝试使用spring boot建立自己的混淆变量:Java 自己的配置属性,java,spring,spring-boot,spring-config,Java,Spring,Spring Boot,Spring Config,在第5章之后,我尝试使用spring boot建立自己的混淆变量: @Component @ConfigurationProperties(prefix = "myprops") public class MyClass { private int myvar1; // Getters and setters... } 写入文件application.properties仅此: myprops.myvar1=3333 MyClass.getMyvar1()现在应该返回3
@Component
@ConfigurationProperties(prefix = "myprops")
public class MyClass {
private int myvar1;
// Getters and setters...
}
写入文件application.properties
仅此:
myprops.myvar1=3333
MyClass.getMyvar1()
现在应该返回3333
,但它仍然返回默认的int值:0
@SpringBootApplication
public class Demo1Application {
public static void main(String[] args) {
SpringApplication.run(Demo1Application.class, args);
}
@Bean
public CommandLineRunner foo(ApplicationContext ctx) {
return args -> {
MyClass mc = new MyClass();
int x = mc.getMyvar1();
System.out.println(x);
};
}
}
添加到demo1应用程序
如果我们在POJO中不使用@Configuration,那么我们需要在主Spring应用程序类中添加@EnableConfigurationProperties(ConfigProperties.class),以将属性绑定到POJO中:
添加到demo1应用程序
如果我们在POJO中不使用@Configuration,那么我们需要在主Spring应用程序类中添加@EnableConfigurationProperties(ConfigProperties.class),以将属性绑定到POJO中:
我的回答有用吗?请看否:(我在@springbootplication
下添加了@EnableConfigurationProperties(MyClass.class)
,我的回答有帮助吗?请看否:(我在@springbootplication
下添加了@EnableConfigurationProperties(MyClass.class)
)