如何读取Spring引导application.properties?
How to read Spring Boot application.properties?
我刚刚阅读了有关外部配置的文档,发现我可以使用以下方法轻松访问 application.properties:
@Component
public class MyBean {
@Value("${name}")
private String name;
// ...
}
但我想将 属性 添加到 class,那不是 Spring 组件(我使用 new 初始化它)。
我想在这里申请:
public class PageWrapper<T> {
private int maxButtonQuantity = **I want to put here the property from file**;
private Page<T> page;
private List<PageItem> buttons;
private int currentNumber;
private String url;
...
}
PageWrapper 不是 Spring Bean class - 它不是 Spring 容器中的 "floating"。我在一些控制器中手动初始化它 - 使用 new operator
有没有什么方法可以以简单的方式访问它,我不必使用容器?
由于PageWrapperclass是在controller中实例化的,所以可以得到属性,只需要在下面的方法中传递,或者在构造函数中传递PageWrapper class。
@Configuration
@Controller
public class MyController
{
@Value("${name}")
private String name;
@Value("${maxButtonQuantity}")
private int maxButtonQuantity;
................
//* pass the value to PageWrapper after you instantiate it,
pageWrapper.setMaxButtonQuantity(maxButtonQuantity);
}
public class PageWrapper<T> {
private int maxButtonQuantity = **I want to put here the property from file**;
private Page<T> page;
private List<PageItem> buttons;
private int currentNumber;
private String url;
public void setMaxButtonQuantity(int maxButtonQuantity)
{
this.maxButtonQuantity = maxButtonQuantity;
}
}
我刚刚阅读了有关外部配置的文档,发现我可以使用以下方法轻松访问 application.properties:
@Component
public class MyBean {
@Value("${name}")
private String name;
// ...
}
但我想将 属性 添加到 class,那不是 Spring 组件(我使用 new 初始化它)。
我想在这里申请:
public class PageWrapper<T> {
private int maxButtonQuantity = **I want to put here the property from file**;
private Page<T> page;
private List<PageItem> buttons;
private int currentNumber;
private String url;
...
}
PageWrapper 不是 Spring Bean class - 它不是 Spring 容器中的 "floating"。我在一些控制器中手动初始化它 - 使用 new operator
有没有什么方法可以以简单的方式访问它,我不必使用容器?
由于PageWrapperclass是在controller中实例化的,所以可以得到属性,只需要在下面的方法中传递,或者在构造函数中传递PageWrapper class。
@Configuration
@Controller
public class MyController
{
@Value("${name}")
private String name;
@Value("${maxButtonQuantity}")
private int maxButtonQuantity;
................
//* pass the value to PageWrapper after you instantiate it,
pageWrapper.setMaxButtonQuantity(maxButtonQuantity);
}
public class PageWrapper<T> {
private int maxButtonQuantity = **I want to put here the property from file**;
private Page<T> page;
private List<PageItem> buttons;
private int currentNumber;
private String url;
public void setMaxButtonQuantity(int maxButtonQuantity)
{
this.maxButtonQuantity = maxButtonQuantity;
}
}