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://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:util= "Http://www.springframework.org/schema/util"Xmlns:jaxws= "Http://cxf.apache.org/jaxws"xmlns:http= "Http://cxf.apache.org/transports/http/configuration"Xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.1.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/ Spring-util-3.1.xsd Http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://w Ww.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd "
    >            <Context:annotation-config/>      <!--Bean declarations Go here -</Beans>
View Code

2) @autowired provided by spring:

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

#2优先byType;

#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= "Com.active.leo.helloworld.InstrumentImpl" />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://www.springframework.org/schema/beans"4 5 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"6 7 Xmlns:util= "Http://www.springframework.org/schema/util"8 9 Xmlns:jaxws= "Http://cxf.apache.org/jaxws"Ten  One xmlns:http= "Http://cxf.apache.org/transports/http/configuration" A  - Xmlns:context= "Http://www.springframework.org/schema/context" -  the xsi:schemalocation=" -  - Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd -  + Http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd  -  + Http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd A  at Http://www.springframework.org/schema/context -  - http://www.springframework.org/schema/context/spring-context-3.1.xsd "> -  -        -  in       <Context:component-scan -  to              Base-package= "Com.active.leo.helloworld"> +  -       </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://www.springframework.org/schema/beans"4 5 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"6 7 XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"8 9 Xmlns:tx= "Http://www.springframework.org/schema/tx"Ten  One Xmlns:context= "Http://www.springframework.org/schema/context" A  - xsi:schemalocation=" -  the Http://www.springframework.org/schema/beans -  - http://www.springframework.org/schema/beans/spring-beans-2.5.xsd -  + Http://www.springframework.org/schema/tx -  + http://www.springframework.org/schema/tx/spring-tx-2.5.xsd A  at HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP -  - http://www.springframework.org/schema/aop/spring-aop-2.5.xsd -  - Http://www.springframework.org/schema/context -  in http://www.springframework.org/schema/context/spring-context-2.5.xsd "> -  to   +  -        <Context:annotation-config/> the  *        $ Panax Notoginseng   <BeanID= "Audience"class= "Com.active.leo.helloworld.Audience" /> -  the   <BeanID= "Instrumentalist"class= "Com.active.leo.helloworld.InstrumentAlist" /> +  A   <BeanID= "Instrument2"class= "COM.ACTIVE.LEO.HELLOWORLD.INSTRUMENTIMPL2"/> the  +   -  $   <Aop:config> $  -     <Aop:aspectref= "Audience"> -  the       <Aop:pointcutID= "Performance" - Wuyi expression= "Execution (* com.active.leo.helloworld.Performer.perform (..))" /> 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: info-contact@alibabacloud.com 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.