Using autowired dependency in constructor in spring

autowired, java, spring

Solution

Instead of doing a lot of work in the constructor (and potentially introduce problems because the application tries to go to the database when it isn't fully initialized) it's probably better to do this initialization in a different method and annotate this with @PostConstruct . This way you know for sure your application is completely wired before networkusermapper is called.

Problem

I have class NetworkUsageService which uses NetworkUsageMapper to access a database. Mapper class is autowired to the service class. I need to access the database during the construcion of service class, so I do something like this: ``` private int someField; @Autowired private NetworkUsageMapper networkUsageMapper; public NetworkUsageService() { someField = networkUsageMapper.getSomeResultFromDB(); } ``` However, this doesn't seem to work as I get exception while creating the service bean. Is there a way to use an autowired dependency during the construction of an object? EDIT: this is my beans configuration as requested: ``` <context:component-scan base-package="mypackage" /> <mybatis:scan base-package="mypackage.mappers" /> <mvc:annotation-driven /> <context:property-placeholder location="classpath:/jdbc.properties"/> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mapperLocations" value="classpath:mappers/*.xml" /> <property name="typeAliasesPackage" value="mypackage.transfer" /> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> ```

Original source