Spring programming using annotations
Spring programming using annotations Step By Step
0. To enable annotation support -- add context namespace & add the following
<context:annotation-config/> --- To tell SC (Spring Container)--to enable annotation support(eg --- AutoWired,PostConstruct,Predestroy,.....)
0.5 --- How to specify location(base pkg) of spring beans to SC?
<context:component-scan base-package="comma sep list of pkgs"/>---
SC starts searching(scanning) in specified pkgs (including sub-pkgs) ---for classes anno with stereotype anno --- @Component,@Service,@Repository,@Controller
Basic class level annotations meant for SC
Super type
@Component --- spring bean class
sub - type annotations
@Controller --- In Web MVC scenario -- for request handling.
@Service --- Service layer (B.L) + transaction management
@Repository --- DAO layer
1. @Component --- <bean id , class> --- SC interprets it & starts bean life-cycle.
eg ---
@Component("abc")
public class MyBean
xml --- <bean id="abc" class="beans.MyBean"/>
OR
@Component
public class MyBean
xml --- <bean id="myBean" class="beans.MyBean"/>
2. @Controller -- spring web mvc controller
3. @Repository --- DAO layer class
4. @Service --- for service layer beans --- transactions.
5. @Scope(value="singleton|prototype|request|session")--- class level annotaion --- in xml via scope atribute.
6. @Lazy(true|false) ----class level anno -- lazy-init attribute
7. @PostConstruct ---method level anno - init-method ---method level
8. @PreDestroy ---method level anno-- destroy-method --- method level
9. @Required(true|false) --- setter method or paramed constr or field level ---tells SC if depenency is mandatory or optional-- def=true
10. @AutoWired ---setter method or paramed constr or field level
eg --- PrimSplechker --- imple spellchecker
auto-wired="byType"
eg -- field level annotation ---in TextEditor bean
@AutoWired(required=true)
private SpellChecker checker;
Meaning -- no parameterised constr, no setter , no xml containing bean definition is required.
SC --- chks for any bean of SpellChecker type & injects it in TextEditor.
11. @AutoWired
@Qualifier("advSpellChecker")
private SpellChecker checker; ---- auto-wired="byName"
---spring supplied anno.
OR
@Resource(name="advSpellChecker")
private SpellChecker checker; ---- auto-wired="byName"
--J2EE supplied via javax.annotation
SpEL --- spring expression language
dynamic expression languge ---spring(3.x) supplied -- to evaluate expressions dynamically.
#{SpEL expression} --- more powerful than JSP EL ---JSP EL allows only getters , where as SpEL allows --- getters,setters,constr invocation, static & non-static method invocations.
Read More:
Comments
Post a Comment