Introduction
I have some business logic properties in the application.yml
file.
They are loaded into the application via a @ConfigurationProperties
annotated class.
How could I use these properties in a class which is not a Spring Bean? It cannot be a singleton, because many objects of it must be created during run-time.
Example
application.yml
business.foo: 2
BusinessProperties.java
@ConfigurationProperties("business")
@Getter // lombok
@Setter // lombok
public class BusinessProperties {
private int foo;
}
TypicalBean.java
@Component
public class TypicalBean {
private final BusinessProperties properties;
@Autowired
public TypicalBean(BusinessProperties properties) {
this.properties = properties;
}
@PostConstruct
public void printFoo() {
System.out.println("Foo: " + properties.getFoo()); // "Foo: 2"
}
}
NonBean.java
public class NonBean {
public void printFoo() {
System.out.println("Foo: ???"); // How to access the property?
}
}
Is there some way to create a non-singleton class, which can have access to configuration (or even other Spring beans) but otherwise works the same as a regular java class? Meaning that I can control its creation, it is collected by the garbage collector if not used anymore, etc.