Automatic assembly detection and AOP Overview of Spring framework Beans

Source: Internet
Author: User
Tags throw exception

Annotations can take full advantage of the Java reflection mechanism to obtain the class structure information, thus effectively reduce the configuration work; annotations are located in the same file as Java code and facilitate uniform maintenance of changes;

1) The spring container disables annotation assembly by default, so you need to display the open annotation assembly in the configuration file before using annotations to assemble:

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://"Xmlns:xsi= "Http://"Xmlns:util= "Http://"Xmlns:jaxws= "Http://"xmlns:http= "Http://"Xmlns:context= "Http://"xsi:schemalocation= "Http:// Spring-beans-3.1.xsd Spring-util-3.1.xsd Http:// http://w "
    >            <Context:annotation-config/>      <!--Bean declarations Go here -</Beans>
View Code

2) @autowired provided by spring:

#1直接修饰类变量, no setter method required;


#3如果没有找到bean或者byType匹配得到的bean有多个, an exception is thrown;

#4如果byType匹配得到的bean有多个, the bean ID can be qualified with @qualifier ("[id]");

#5如果没有找到bean, you can use @autowired (required=false) to require no assembly, but not recommended;

1 <BeanID= "Instrument1"class= "" />2 3 <BeanID= "Instrument2"class= "COM.ACTIVE.LEO.HELLOWORLD.INSTRUMENTIMPL2" />4 5 @Autowired6 7 @Qualifier ("Instrument2")8 9Private instrument instrument;
View Code

3) @inject provided by JSR-330

#1 JCP a model for unified Dependency Injection specification;

#2与 @Autowired basically the same, except that there is no required attribute, it must be assembled, otherwise abnormal;

#3可以修饰类属性, class methods and class constructors;

#4如果byType匹配得到的bean有多个, the bean ID can be qualified with @named ("[id]");

4) @resource provided by JSR-250

#[email protected] function is equivalent to @autowired, but @autowired by Bytype automatic injection, and @resource by default byname automatic injection;

#[email protected] has two important properties, name and Type,spring parse the Name property of the @resource annotation into the bean's name, and the type attribute resolves to the bean.

@Resource Assembly Order
#1如果同时指定name和type, the unique matching bean from the context is assembled and the exception is not found;
#2如果仅指定name, the name (ID) matching bean is assembled from the context, and the exception is not found;
#3如果仅指定type, the type matching bean is assembled from the context, it cannot be found or multiple exceptions are found;
#4如果同时没有指定name和type, the assembly is automatically byname (see 2), and if there is no match, fallback to an original type (Userdao) uses the ByName auto-injection strategy through the reflection mechanism, or if the match is automatically assembled, otherwise abnormal;

using annotations for Bean The automatic detection

1) in the configuration file to open the function of automatic bean detection, you need to specify the scanned Java package path;

1 <?XML version= "1.0" encoding= "UTF-8"?>2 3 <Beansxmlns= "Http://"4 5 Xmlns:xsi= "Http://"6 7 Xmlns:util= "Http://"8 9 Xmlns:jaxws= "Http://"Ten  One xmlns:http= "Http://" A  - Xmlns:context= "Http://" -  the xsi:schemalocation=" -  - Http:// -  + Http://  -  + Http:// A  at Http:// -  - "> -  -        -  in       <Context:component-scan -  to              Base-package= ""> +  -       </Context:component-scan> the  * </Beans>
View Code

2) If you add a string after the annotation to display the specified bean name, otherwise the bean name is the lowercase type name, such as @component ("Specificname")

3) @Component: corresponding to the generic bean

4) @Controller: corresponding to the control layer bean, such as the spring MVC Controller

5) @Service: corresponding to the business layer bean, such as struts Action

6) @Repository: Corresponds to a persistent layer bean, such as DAO

Plane-oriented programming

1) Spring AOP: Unify define "common features" in one place, declaratively define how these common functions are "woven" into certain "specific applications" and "No need to modify" code for a particular application;

#1通用功能: Advice, such as logs, security, or transactions;

#2方式: PointCut, such as method invocation, field modification, and throw exception, Spring AOP only supports method invocation;
#3织入: Weaving, three weaving periods: Compile period, class loading period and run time, SPRINGAOP only support the running period;

#4特定应用: Joinpoint, such as classpath;

#5不需要修改: Dynamically create proxy objects for the target class;

2) AspectJ function is more comprehensive than spring AOP, if you need to be able to load the ASPECTJ function module in spring, using SPRINGAOP in addition to regular spring's jar package also needs to introduce ASPECTJWEAVER-[XXX]. Jar

1 <?XML version= "1.0" encoding= "UTF-8"?>2 3 <Beansxmlns= "Http://"4 5 Xmlns:xsi= "Http://"6 7 XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"8 9 Xmlns:tx= "Http://"Ten  One Xmlns:context= "Http://" A  - xsi:schemalocation=" -  the Http:// -  - -  + Http:// -  + A  at HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP -  - -  - Http:// -  in "> -  to   +  -        <Context:annotation-config/> the  *        $ Panax Notoginseng   <BeanID= "Audience"class= "" /> -  the   <BeanID= "Instrumentalist"class= "" /> +  A   <BeanID= "Instrument2"class= "COM.ACTIVE.LEO.HELLOWORLD.INSTRUMENTIMPL2"/> the  +   -  $   <Aop:config> $  -     <Aop:aspectref= "Audience"> -  the       <Aop:pointcutID= "Performance" - Wuyi expression= "Execution (* (..))" /> the  -       Wu  -       <Aop:beforePointcut-ref= "Performance"Method= "Takeseats" /> About  $       <Aop:beforePointcut-ref= "Performance"Method= "Turnoffcellphones" /> -  -       <aop:after-returningPointcut-ref= "Performance"Method= "Applaud" /> -  A       <aop:after-throwingPointcut-ref= "Performance"Method= "Demandrefund" /> +  the     </Aop:aspect> -  $   </Aop:config> the  the   the  the </Beans>
View Code

#1 <aop:aspct represents a facet and is associated with a bean named audience;

#2 <aop:pointcut> representation defines a pointcut named performance and triggers the binding with the call to the Perform method;

#3 <aop:before> representation defines the action before the call perform method triggers;

#4 <aop:after-returning> represents the action that defines the invocation of the perform method after the normal end;

#5 <aop:after-throwing> represents the action that defines the invocation of the Perform method after an exception is thrown;

#6 <aop:around> Merge before and After-returning,preceedingjoinpoint.proceed () as the target action;

#7如果目标动作有参数, can be obtained by means of arg/arg-names in the tangent method;

3) Spring AOP can also be implemented with annotations

#1首先要在配置文件中添加 <aop:aspectj-autoproxy/>;

#2然后通过 @Aspect, @Pointcut, @Before, @AfterReturning, @Around and other implementations

Automatic assembly detection and AOP Overview of Spring framework Beans

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.