Implementation of Spring IOC + AOP

Source: Internet
Author: User

Spring thought very well and minimized intrusive programming. It is now understood that spring provides features, DI,IOC, database operations, AOP,MVC. Wrote some small demo for DI,AOP.

PS:AOP is really good.

Code reference: "Spring Combat" third edition

Environment:

Win7 64

IDE Eclipse

JDK 1.8

Spring 4.1.7

------------------------------------------------I'm a code splitting line---------------------------------------------------------

Ideas:

A performer and audience, performers and viewers are injected through DI (XML and annotation injections), and the audience moves through AOP (the database writes some configuration files) before the performer's performance.

Performer Performer.java

1  PackageCom.gxf.beans;2 3 /**4 * Performers5  * @authorGXF6  *7  */8  Public classPerformer {9     Ten     /** One * performers ' performance behavior A      */ -      Public voidperform () { -SYSTEM.OUT.PRINTLN ("Performers begin to perform"); the     } -}

Audience Audience.java

1 /**2 * Audience3  * @authorGXF4  *5  */6  Public classAudience {7     8     /**9 * Before the show, the audience began to find seatsTen      */ One      Public voidtakeseats () { ASystem.out.println ("Audience starts looking for seats"); -     } -      the     /** - * Audience shutdown before the show -      */ -      Public voidTurnoffcellphone () { +System.out.println ("Audience will hand machine"); -     } +      A     /** at * The show's over, the audience starts clapping -      */ -      Public voidapplaud () { -System.out.println ("Audience starts clapping"); -     } -      in     /** - * The performance failed, the audience demanded a refund to      */ +      Public voidDemandrefund () { -SYSTEM.OUT.PRINTLN ("Hush! Refund! "); the     } *}

Profile Config file

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:tx= "Http://www.springframework.org/schema/tx"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5. XSD Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context           -2.5.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-2.5.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd " >            <Beanclass= "Org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>    <BeanID= "User"class= "Com.gxf.beans.User">        < Propertyname= "Name">            <value>Zhangsan</value>        </ Property>    </Bean>    <BeanID= "Phone"class= "Com.gxf.beans.Phone">        < Propertyname= "Number">            <value>13608196502</value>        </ Property>    </Bean>    <!--Configure a bean for performers and viewers -    <BeanID= "Performer"class= "Com.gxf.beans.Performer">    </Bean>    <BeanID= "Audience"class= "Com.gxf.beans.Audience"></Bean>    <!--Configure facet-oriented -    <Aop:config>        <Aop:aspectref= "Audience">            <Aop:beforepointcut= "Execution (* com.gxf.beans.Performer.perform (..))"Method= "Takeseats"/>            <Aop:afterpointcut= "Execution (* com.gxf.beans.Performer.perform (..))"Method= "Applaud"/>        </Aop:aspect>            </Aop:config>    <BeanID= "DataSource"class= "Org.apache.commons.dbcp.BasicDataSource">        < Propertyname= "Driverclassname">            <value>Com.mysql.jdbc.Driver</value>        </ Property>        < Propertyname= "url">            <value>Jdbc:mysql://localhost:3306/test?useunicode=true&amp;Characterencoding=utf-8</value>        </ Property>        < Propertyname= "username">            <value>Root</value>        </ Property>        < Propertyname= "Password">            <value>12345</value>          </ Property>       </Bean></Beans>

Test procedure Test.java

1 ImportOrg.springframework.context.ApplicationContext;2 ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;3 4 ImportCom.gxf.beans.Performer;5 6  Public classTest {7 8      Public Static voidMain (string[] args) {9ApplicationContext context =NewClasspathxmlapplicationcontext ("Config + +");TenPerformer performer = (performer) Context.getbean ("performer"); One performer.perform (); A          -     } -  the}

There are some test Di and database code not completed, database I think it is better to use the ORM Framework, although spring provides some template methods to manage the database, but it is not easy to use the ORM framework.

Source: Http://pan.baidu.com/s/1jGKnKv8

Implementation of Spring IOC + AOP

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.