Donnerstag, 27. Oktober 2011

Spring Customize Annotation Scanning

A really nice feature in Spring 3.0 is to have custom annotation. Why do you like to have own annotations? Because you could provide some semantic details about the component types. For example when you have a spring bean which is facade, why not use a annotation with the name facade?

Here the spring wiring for beans with the annotation facade:
<context:component-scan base-package="demo.design">
<context:include-filter type="annotation"
expression="com.seitenbau.demo.stereotypes.Facade" />
</context:component-scan>

The facade annotation:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Facade {}
view raw Facade.java hosted with ❤ by GitHub

And here a demo facade spring bean:
@Facade
public class AddressFacadeImpl implements AddressFacade
{
private AddressService addressService;
private UserRepository userRepository;
@Override
public List<AdresseBean> getAdresses() throws ApplicationException
{
return addressService.getAdresses();
}
@Override
public void saveAdresse(AdresseBean adresse) throws ApplicationException
{
addressService.saveAdresse(adresse);
}
@Override
public boolean userExists(String username)
{
return userRepository.userExists(username);
}
@Autowired
public void setAddressService(AddressService addressService)
{
this.addressService = addressService;
}
@Autowired
public void setUserRepository(UserRepository userRepository)
{
this.userRepository = userRepository;
}
}