A: IoC annotations development
1, need to be introduced in the context constraints applicationContext.xml
2, simple annotation Development Example
Note: For injection property to be achieved in a class, may not be provided the method attribute set, if provided, the hair can be applied @Value annotations in the set method
3, IoC annotations details
@Component
Modification of a class, the class will be handed over to Spring management, there are three derivative annotation (similar function) is recommended with three notes derived
- @Controller (web layer)
- @Service (service layer)
- @Repository (dao layer)
Properties injected comment
- @Value ( "") injection common attributes, and attribute values set
- @Autowired injection object properties, injection according to the type of object, but is generally injected by name, with annotations need @Qualifier (value = "Name"
- @Resource (name = "name") into the object properties by name, common
Other notes:
- @PostConstruct initialization method
- @PreDestroy destruction method
- @Scope ( "singleton & prototype & request & session & globalsession") annotation scope, the default is singleton, singleton
4, xml and notes the difference between integration and development
Annotations and XML configuration differences
Note: metadata in a distributed, tightly bound with the source code.
xml: is a centralized meta data, with the source code without binding.
Details: https://www.cnblogs.com/iOS-mt/p/6133656.html
xml development and integration of annotation (understand)
Note: bean managed to xml, property injection to comment
Two, AOP (Aspect Oriented Programming) Oriented Programming
Reference: http://www.cnblogs.com/xrq730/p/4919025.html
AOP is a simple illustration:
1, the underlying principle of dynamic proxies (reflection)
- JDK dynamic proxy: Only class that implements the interface proxy
- Cglib :( dynamic proxy class is the third party agent to Javasist technology) can be achieved without the agent to the implementation of the interface class, subclass object generated
2, AOP related terms:
- JoinPoint (point of attachment, may be enhanced / are cut method may be referred to the point of attachment)
- Pointcut (entry point, the real point is enhanced connectivity is known as the starting point)
- The Advice (notification / enhancement, enhancement of the relevant code or method)
- Introduction (introducing, enhanced class level, ie directly into the enhanced class code)
- Target (target, the enhanced object)
- Weaving (weaving, the Advice of Target into the process)
- The Proxy (proxy, after class by weaving AOP)
- Aspect (combination section, a plurality of notifications, and a plurality of entry points)
Note: a single entry point may be a single method of a class in the packet, the method may be a plurality of classes of a plurality of one or more packages, flexible configuration required
3, based on xml aspectJ aop development of simple entry
1) into the corresponding jar package
2) introducing the aop schema constraints in the configuration file
3) the preparation of the target class
4) writing class section
5) xml configuration
4, advice (notification) Type
1) Pre-notification
Only get to the entry point information (of all types of notifications can be obtained)
2) after advice
You can obtain the entry point of the return value
3) around advice
Both before and after the starting point of the enhanced implementation of the relevant code, you can prevent the execution of an entry point (such as an entry point for transaction processing)
4) exception notification
When the entry point for the application / notification of abnormal operation, will perform exception notification, you can throw an exception
5) The final notice
Regardless of whether there is an exception entry point, the code will eventually notice of execution
5, pointcut expression
Execution ([Access Modifiers] Access Return Value Type package name. class name. Method name (parameter))
Access modifier can be omitted, can be used to access return type instead of *, represents any type, instead of the parameter .., * represents an arbitrary parameter can be flexibly configured with a starting point
Example: Say in all classes * * * * (..) represents any packets are configured as an entry point method
* Com.test.dao.UserDao + .save (..) represents UserDao class and its subclasses save method configured as an entry point