I use spring-boot with spring xml in my project.
I wrapper the DAOs in a DataAccessService
class to serve as a DB service layer, both the service and the DAOs are injected in spring xml and used by Autowired
.
Now I want to have a XXXutils
class to provide some static useful functions.
Some of the functions need to access the database. But I can't access the DB service or the DAO in static method.
How should I do this? Can I Autowire
a static DB service or the DAO in XXXUtils
class? May be it not a good practice?
I even don't know whether Spring support the static injection.
Is there any good practice about this?
Best Answer
You can do like this:
Then you can use the
XXXXUtils.getDBData()
to access the DB.and in spring xml you can config like this:
Hopes this can help you :-)