java - How to validate if a bean instance has been wired? -
i'm creating small framework provides abstract
base classes have implemented when using library.
how can create validation routine checks if indeed classes have been implemented?
i thought maybe use @conditionalonmissingbean
of spring-boot, nothing far. anyhow, goal be:
@configuration @enableautoconfiguration public class appcfg { @conditionalonmissingbean(basecarservice.class) //stupid exmaple public void validate() { system.out.println("missing bean!!"); } } //must implemented public abstract basecarservice { }
how can achieve this?
you can calling applicationcontext.getbeansoftype(basecarservice.class)
when context has been initialized (for example bean implements contextloaderlistener
), i.e. following:
public class beansvalidator impelements contextloaderlistener { public void contextinitialized(servletcontextevent event) { if (applicationcontext.getbeansoftype(basecarservice.class).isempty()) { // print log, throw exception, etc } } }
Comments
Post a Comment