Saturday, March 19, 2011

Autowiring in xml config

Autowiring by type

Autowiring in xml config can be acheived, so that we don't need to use <property> or <constructor-arg> tags, whereby reducing configuration size.

<import resource="db-config.xml" />

<bean id="flightRepo" class="xml.FlightRepositoryImpl" autowire="byType" />

So IoC container will search for type DataSource and will find it in db-config.xml and autowire by type.

Autowiring by name

Autowiring can be achieved by name as well. Consider our FlightRepositoryImpl class.

public class FlightRepositoryImpl  {

private DataSource dataSource;

public void FlightRepositoryImpl (DataSource dataSource) {
this.dataSource = dataSource;

Now in the config file, where datasource is described, we have to name it "dataSource" as we defined in our class.

so we have :

<!-- name of the bean is dataSource -->
<bean id="dataSource"
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="" />

No comments:

Post a Comment