Reputation: 880
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>
Upvotes: 5
Views: 6754
Reputation: 12880
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.
Upvotes: 1
Reputation: 953
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.
Upvotes: 12
Reputation: 616
If you can add the exception stack-trace along with your spring configuration that will help to answer more accurately. I am guessing it is because you might have more than one bean instances for NetworkUsageMapper class or its missing.
If its missing just add one :). To avoid exception you can use required attribute:
@Autowired(required=false) private NetworkUsageMapper networkUsageMapper;
If more than one then you need to use qualifier for example :
then
@Autowired
@Qualifier("networkUsageMapper1")
private NetworkUsageMapper networkUsageMapper;
Upvotes: 0
Reputation: 35598
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
}
Upvotes: 1