Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using autowired dependency in constructor in spring

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>
like image 932
Wolf Avatar asked Dec 02 '13 15:12

Wolf


3 Answers

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.

like image 152
Janoz Avatar answered Nov 11 '22 04:11

Janoz


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
}
like image 26
Chris Thompson Avatar answered Nov 11 '22 05:11

Chris Thompson


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.

like image 1
Keerthivasan Avatar answered Nov 11 '22 04:11

Keerthivasan