lishman levelup
«previous  next»


Getting Started
Form Processing
IoC Container
Hibernate ORM



The Business Service Facade


Now that we have a DAO for the COUNTRY table, we can develop a second WorldService implementation, which will access the database.
@Service
public class WorldServiceImpl implements WorldService {

  @Autowired
  private CountryDao countryDao;

  public Collection<Country> getAllCountries() {
    return countryDao.getAll();
  }

  public Country getCountryById(int countryId) {
    return countryDao.getById(countryId);
  }

  public Country getCountryByName(String countryName) {
    return countryDao.getByName(countryName);
  }

  public void saveCountry(Country country) {
    countryDao.save(country);
  }

  public void deleteCountry(Country country) {
    countryDao.delete(country);
  }

}
We identify the class as a component using the @Service annotation, and use autowiring to provide the DAO implementation.

Qualification

If we add the WorldServiceImpl class to the levelup.world.domain.service package, then a component scan on this package will find two implementations of the WorldService interface.

World Service Pachages

Therefore, we will need to use the @Qualifier annotation to idenify which implementation to use.
@Autowired
@Qualifier(value="worldServiceImpl")
private WorldService worldService;

Separate Packages

As an alternative, we could split the implementations into separate packages; mock classes in one package and live classes in another,

World Service Pachages

Now the appropriate implementations can be injected, just by changing the configuration file.

For example, in applicationContext-live.xml we would scan the live package:
<beans>

  <context:component-scan base-package="levelup.world.domain.service.live"/>

</beans>
While applicationContext-mock.xml would scan the mock package:
<beans>

  <context:component-scan base-package="levelup.world.domain.service.mock"/>

</beans>

Tip
Tip - in general, dependencies are injected from the lower tiers. DAOs (@Component) are injected into business classes (@Service) which are themselves injected into web controllers (@Controller).
»