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>
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.
That's correct. The bean is first constructed, then the dependencies are injected. If you need it in your constructor, do this
@Autowired
public NetworkUsageService(NetworkUsageMapper mapper){
// do stuff
}
The other way is to make the setterMethod of NetworkUsageMapper annotated with @Required annotation
@Required
public void setNetworkUsageMapper(NetworkUsageMapper mapper){
this.networkUsageMapper = mapper;
this.someField = this.networkUsageMapper.getSomeResultFromDB();
}
This method would be invoked immediately compulsorily after construction of the object. This is really useful when you have a long list of mandatory properties for the object to work properly. Instead of having the constructor with long parameter list, we can set all the properties using setters with @Required
annotation. It seems to be very handy for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With