Spring Learning One: the IOC (inversion of control) and AOP (aspect-oriented) XML configuration and annotation methods

Source: Internet
Author: User

The role of the spring framework: To simplify Java development

Spring is an open source framework, and spring is a lightweight Java development framework that emerged in 2003 by Rod Johnson in his book expert one-on-one development and Some of the concepts and prototypes elaborated in design are derived. It is created to address the complexities of enterprise application development

I. IOC (Control reversal)

1 constructor Injection

(XML configuration)

2 Method Injection

(Annotation method)

(XML configuration)

Two, AOP (face-tangent)

Annotation method

XML configuration

Just use, that's simple, the principle behind the

Code schema


Spring-test.xml

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xs I= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xsi:schemalocation= "http://www.springframework.org/s Chema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd HTTP://WWW.SP                 Ringframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP Http://www.springframework.org/schema/aop/spring-ao P-3.0.xsd > <!--annotation support, in order for us to use annotations to create a bean--><context:annotation-config/><!--Set the bits of the package to be scanned --<context:component-scan base-package= "Com.eyugame.test"/><!--support AOP annotations--><aop:aspectj-autop Roxy/><aop:config ><!--configuration aoP The class that triggers the call is MyService, which corresponds to Myserviceimpl--><aop:aspect id= "spect" ref= "MYAOP" ><!-- Configuring the Todo () method to invoke Com.eyugame.test.MyBean triggers the AOP--><aop:pointcut id= "target" expression= "Execution (* Com.eyugame.test.MyBean.toDo (..)) " /><!--called before execution, calling method Dobefore--><aop:before pointcut-ref= "target" method= "Dobefore ()"/><!--after execution, Call method Doafter--><aop:after pointcut-ref= "target" method= "Doafter ()"/><!--return call, Method Doreturn-->< Aop:after-returning pointcut-ref= "target" method= "Doreturn ()"/><!--throws an exception when called, calling method Doreturn--&GT;&LT;AOP: After-throwing pointcut-ref= "target" method= "dothrowing ()" throwing= "ex"/><!--note aop:aroup in the new version spring seems to lapse, pit me-- -><!--<aop:around pointcut-ref= "Businessservice" method= "Doaround"/>--></aop:aspect></ Aop:config><bean id= "MYAOP" class= "Com.eyugame.test.MyAop" ></bean><bean id= "Depedecybean" class= "Com.eyugame.test.DependencyBean" ></bean><!--Dependency Injection 1 constructor injected--><bean ID= "Mybean" class= "Com.eyugame.test.MyBean" ><constructor-arg ref= "Depedecybean" ></constructor-arg> </bean><!--Dependency Injection 2 setpoint injection--><bean id= "myBean2" class= "com.eyugame.test.MyBean2" ><!-- The name is consistent with the name of the class to be injected into the class--><property name= "Idependency" ref= "Depedecybean" ></property></bean><! --Annotated AOP--><bean id= "MyAop2" class= "Com.eyugame.test.MyAop2" ></bean></beans>
Interface for Dependency Injection
Package Com.eyugame.test;public interface Idependency {     void  Show ();}
Specific classes of dependency injection
Package Com.eyugame.test;public class Dependencybean implements idependency {public void Show () {System.out.println ("my Class Name: "+ This.getclass ());}}
AOP Classes
Package Com.eyugame.test;public class Myaop {public Myaop () {};p ublic void Dobefore () {System.out.println ("Call before Execution");} public void Doafter () {System.out.println (called after execution);} public void Doreturn () {System.out.println ("at the end of the call");} public void dothrowing () {System.out.println ("called with Exception");}}
Package Com.eyugame.test;import Org.aspectj.lang.annotation.after;import Org.aspectj.lang.annotation.afterreturning;import Org.aspectj.lang.annotation.afterthrowing;import Org.aspectj.lang.annotation.aspect;import Org.aspectj.lang.annotation.before;import org.aspectj.lang.annotation.pointcut;/* annotation aop*/@Aspectpublic class MyAop2 {public MyAop2 () {}; @Pointcut (value= " Execution (* com.eyugame.test.MyBean2.toDo (..)) ") private void Init () {} @Before ("Execution (* com.eyugame.test.MyBean2.toDo (..))") public void Dobefore () {System.out.println ("Call before Execution");} @After ("Execution (* com.eyugame.test.MyBean2.toDo (..))") public void Doafter () {System.out.println (called after execution);} @AfterReturning ("Execution (* com.eyugame.test.MyBean2.toDo (..))") public void Doreturn () {System.out.println ("at the end of the call");} @AfterThrowing ("Execution (* com.eyugame.test.MyBean2.toDo (..))") public void dothrowing () {System.out.println ("called with Exception");}}

Constructor injection
Package Com.eyugame.test;public class Mybean {     private idependency idependency;          Public Mybean () {          }/     * Dependency Injection 1: Constructor injection */public     Mybean (idependency idependency) {     this.idependency= idependency;     }     public void ToDo () {    this.iDependency.show ();     }     }
The package com.eyugame.test;/* method injects */public class MyBean2 {private Idependency idependency;    Public MyBean2 () {} public void ToDo () {this.iDependency.show ();    }/* Dependency Injection 2: Set Value injection */public void setidependency (Idependency idependency) {this.idependency = idependency; }}
Annotation-Created Bean
Package Com.eyugame.test;import org.springframework.stereotype.Component, @Component ("MYBEAN3") public class MyBean3 {public void Show () {System.out.println ("My name is MyBean3");}}

Test

Import Com.eyugame.test.mybean;import Com.eyugame.test.mybean2;import Com.eyugame.test.mybean3;public class testspring {@SuppressWarnings ("resource") public static void main (string[] args) {ApplicationContext context = new Classpathxmlapplicationcontext ("Config/spring/spring-test.xml"); System.out.println ("Using the constructor to inject and configure AOP-------------");/* Dependency Injection One: constructor injection |AOP test */mybean Mybean =  Context.getbean (" Mybean ", Mybean.class); Mybean.todo (); System.out.println ("Using methods to inject and annotate AOP-----------");/* Dependency Injection Two: Method injection */mybean2 myBean2 = Context.getbean ("MyBean2", Mybean2.class), Mybean2.todo ();/* Create bean*/system.out.println with annotations ("Create bean-----------with annotations"); MyBean3 Mybean3=context.getbean ("MyBean3", Mybean3.class); Mybean3.show ();}}

Dependent packages

<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" ><modelversion >4.0.0</modelVersion><groupId>Test</groupId><artifactId>test2</artifactId> <version>0.0.1-snapshot</version><packaging>jar</packaging><name>test2</name ><url>http://maven.apache.org</url><properties><project.build.sourceencoding>utf-8 </project.build.sourceEncoding><!--versions of each jar--><spring.version>3.2.3.release</ spring.version><junit.version>4.11</junit.version><jsonlib.version>2.4</ jsonlib.version><shiro.version>1.2.0</shiro.version></properties><dependencies>< dependency><groupid>junit</groupid><artifactid>junit</artifactid><version>${ Junit.version}</version><scope>test</scope></dependency><!--Spring Jars--><dependency><groupid> org.springframework</groupid><artifactid>spring-core</artifactid><version>${ spring.version}</version></dependency><dependency><groupid>org.springframework</ Groupid><artifactid>spring-beans</artifactid><version>${spring.version}</version> </dependency><dependency><groupId>org.springframework</groupId><artifactId> spring-context</artifactid><version>${spring.version}</version></dependency>< Dependency><groupid>org.springframework</groupid><artifactid>spring-aop</artifactid ><version>${spring.version}</version></dependency><dependency><groupId> Org.aspectj</groupid><artifactid>aspectjweaver</artifactid><version>1.8.5</version ></dependency><!--<dependency> <groupid> org.aspectj</groupid> <artifactId> aspectjweaver</artifactid> <version> 1.6.11 </version> </dependency>--></dependencies></project>


Console output


Spring Learning One: the IOC (inversion of control) and AOP (aspect-oriented) XML configuration and annotation methods

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.