7

I am a newbie in Spring and am trying to understand the below concept.

Assume that accountDAO is a dependency of AccountService.

Scenario 1:

<bean id="accServiceRef" class="com.service.AccountService">
    <property name="accountDAO " ref="accDAORef"/>
</bean>

<bean id="accDAORef" class="com.dao.AccountDAO"/>

Scenario 2:

<bean id="accServiceRef" class="com.service.AccountService" autowire="byName"/>
<bean id="accDAORef" class="com.dao.AccountDAO"/>

In AccountService Class:

public class AccountService {
    AccountDAO accountDAO;
    ....
    ....
}

In the second scenario, How is the dependency injected ? When we say it is autowired by Name , how exactly is it being done. Which name is matched while injecing the dependency?

Thanks in advance!

vegemite4me
  • 6,621
  • 5
  • 53
  • 79
MAlex
  • 1,234
  • 2
  • 16
  • 29
  • Possible duplicate of [Understanding Spring @Autowired usage](https://stackoverflow.com/questions/19414734/understanding-spring-autowired-usage) – tkruse Dec 06 '17 at 05:11

2 Answers2

12

Use @Component and @Autowire, it's the Spring 3.0 way

@Component
public class AccountService {
    @Autowired
    private AccountDAO accountDAO;
    /* ... */
}   

Put a component scan in your app context rather than declare the beans directly.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com"/>

</beans>
Paul McKenzie
  • 19,646
  • 25
  • 76
  • 120
  • 1
    Sorry Paul, but internally what does this do? – MAlex Jul 06 '11 at 10:50
  • 4
    Component-scan looks for all classes annotated @Component in package com (as per your example) and sub-packages. So if your AccountDAO and AccountService classes are @Components, then Spring will inject the one into the other. It is using the class rather than the name of the beans to do this. I think that this has emerged as the preferred method of wiring your dependencies together using Spring 3.0. It makes your application context much cleaner and the dependencies are solely expressed in java code, where they should be. – Paul McKenzie Jul 06 '11 at 10:53
  • Thanks Paul. Got it. But, dont we require a higher java version for using Spring 3.0. I am using 1.4. I believe in that case I cant use annotations. – MAlex Jul 07 '11 at 07:04
  • 6
    Java 1.4 went end-of-life nearly three years ago. I suggest that, if you can, you get yourself to Java 6. – Paul McKenzie Jul 07 '11 at 14:25
2
<bean id="accServiceRef" class="com.service.accountService" autowire="byName">
</bean>    
<bean id="accDAORef" class="com.dao.accountDAO">
</bean>

and

public class AccountService {
    AccountDAO accountDAO;
    /* more stuff */
}

When spring finds the autowire property inside accServiceRef bean, it will scan the instance variables inside the AccountService class for a matching name. If any of the instance variable name matches the bean name in the xml file, that bean will be injected into the AccountService class. In this case, a match is found for accountDAO.

Hope it makes sense.

cdeszaq
  • 30,869
  • 25
  • 117
  • 173
yonikawa
  • 581
  • 1
  • 9
  • 32