1. Spring AnnotationSupport :-
Spring2.0 has a verylittle supportto annotation,whenitcomestospring2.5 , it has extendedit's
frameworktosupportvariousaspectsof springdevelopmentusingannotations.Inspring3.0 it
startedsupportingjavaconfigprojectannotationsaswell.
In Spring2.0 it has added@Repositoryand@Requiredannotations.
In Spring2.5 it has addedfewmore annotations@Autowired,@Qualifierand@Scope.
alongwithabove annotation'sinspring2.5 sterotype annotations@Component,@Controller,
@Service .
In Spring3.0 fewmore annotationshasbeenaddedlike
@Lazy,@Bean,@Configuration,@DependsOn,@Valueetc..
In additiontospringbasedmetadataannotationsupport,ithasstartedadoptionJSR-250javaconfig
projectannotationslike @PostConstruct,@PreDestory,@Resource ,@Injectand@Namedetc...
@Requiredannotation :- In spring1.x onwordswe have dependency
check.fora beanif youwant to detectunresolveddependenciesof abean,youcanuse dependency
check.In Spring2.0 ithas introduced anannotation@Required .byusingthisannotationyoucan
make , a particularpropertyhas beensetwithvalue (OR) not.inspring2.5 @Requiredannotation
and dependency -checkcompletlyremoved.
@Autowired:- annotationtoautowire bean onthe settermethod,constructor(OR) afiled.
Example :-
EmployeeService.java
publicclassEmployeeService{
@Autowired
private EmployeeDAO empDao;
}
EmployeeDAO.java
publicclassEmployeeDAO{
@Autowired
private DataSource dataSource;
}
2. The @Autowired Annotationishighlyflexible andpowerful,andbetterthan<autowire>attribute in
bean configurationfile.
Aboutthe @Autowiredannotationtogive anintimationtospringIOCcontainerwe canuse
<context:annotation-config/> inspringfile.
Note :
By default,the @Autowiredwill performthe dependencycheckingtomake sure the propertyhas
beenwiredproperly.WhenSpringcontainer can'tfinda matchingbeanto wire,itwill throw an
Exception.if youwanttodisable the dependencycheckingfeature thenwe canuse the "required"
attribute of @Autowiredannotation.if the requiredattributevalue isfalsethenthe dependency-
checking is disabled.elsedependency -checkingisenabled.the defaultvalue of requiredattrbute is
true.
Example :-
publicclassEmployeeService{
@Autowired(required=false)
private EmployeeDAOempDao;
}
@Qualifier:-
for example,Inbeanconfigurationfile for EmployeeDAO bean two configuration'sare there.
<beans>
<beanid="empDao1"class="com.nareshit.dao.EmployeeDAO"/>
<beanid="empDao2"class="com.nareshit.dao.EmployeeDAO"/>
</beans>
In EmployeeService class
publicEmployeeService{
@Autowired
private EmployeeDAO employeeDao;
}
here employeeDao1(OR) employeeDao2whichbeanisrequiredto injectdon’tknow by the
containersoContainerWill rise AnException.ToSolve thisproblemand toinjecta particularbean
i,e employeeDao1(OR) employeeDao2 we can use @Qualifierannotation