Announcement Announcement Module
Collapse
No announcement yet.
Autowiring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Autowiring

    Hi,

    I am learning spring and I have few issues with Autowiring. I have the below class :

    @Component
    public class MyProperties{

    @Autowired
    private Map<String, String> props;

    @Autowired
    DBUtility dbUtility;

    }

    The dbutility component contains api to query the db and return a properties map that should be injected into the above props property.

    I am not sure how to do that. Please provide your input

  • #2
    DI is performed at application context initialization (i.e. at application startup). If the map is an object that is created programmatically and changes from time to time, you can't DI it, neither with autowiring nor with normal declared wiring. So simply don't. Dependency injection was simply not designed for that.

    Also, regarding your "DBUtility" class: first thing, program by interfaces. Never wire the implementation, but always the interface. Second, I suggest you to discard a custom db accessing utility like the one you want to use, and learn to use at the very least Spring JDBC for database access (best option would be to use JPA, an ORM like Hibernate or at least a SQL mapper like MyBatis).

    Comment


    • #3
      Well the Map props just contains the properties from DB and does not change. I want this MyProperties bean to be available to all other component in the application after the application starts.

      So is it possible for me to load the Map props with the properties from DB at application startup.

      Comment

      Working...
      X