Spring Framework comment

Notes Configuration

Spring does not automatically find the notes, annotations need to take the initiative to declare the need to find the package in which
modify the configuration file:
Adding a workspace: xmlns: context and xsi: schemaLocation
declaration notes in the package:

<context:component-scan base-package="包名,包名"/>

Common Annotations

The object is handed over container management

① @ Component ( "") is equivalent to <bean />, id in brackets for the object in the container, the first default lowercase
② @ Service and @Component same function, are generally used for the ServiceImpl
③ @ Respoitory and function @Component the same, generally used for data access layer
④ @ controller and @Component same function as the controller is generally used, the container is managed SpringMVC

Embodiment, by .getBeanDefinitionNames () method of detection:
Here Insert Picture Descriptioncan be found, in addition to annotate the object with our own container management, some dependent objects scanned annotations

Injecting property value

Notes java in ⑤ @ Resource, default in accordance with byName, if not name, in accordance with byType
use on the property, and set method does not require property, it is recommended to refer to the same attribute name and the name, looking to improve the efficiency
⑥ @ Autowired spring of notes, in accordance with the byType default, and no property set methods
⑦ @ Value () Gets the contents of the properties file
Example:
Here Insert Picture Description

Notes achieve AOP-based Aspect

premise:

<!-- 设置注解完成AOP -->
 <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

① For tangent point, before adding method @Pointcut ( "execution (* package name. Class name. Method name ())"), corresponding to <the pointcut />
② pre-notification, and the notification method based @Aspect added before @ Component, particularly the former method of adding @Before ( "cut-off point of the package name. class name. method name ()")
③ with the other front, rear (@After), abnormal (the @AfterThrowing), surround (@Around)
if and configure front and rear surround, surround generally before pre / post

Ps: It should be noted here that, Spring AOP is used by default JDK dynamic proxy class and we set the notification method if it is inherited from the interface, then need to modify the dynamic proxy way CGLIB:
need to be modified in the configuration file:

<aop:aspectj-autoproxy proxy-target-class="true"/>
<!--true:cglib动态代理,false:jdk动态代理(默认值,此时需要接口来获取代理对象)-->

Example:
Here Insert Picture Description

Published 88 original articles · won praise 2 · Views 1739

Guess you like

Origin blog.csdn.net/qq_41891805/article/details/105214289