Spring IOC + AOP 的实现

Spring思想很不错,尽量减少侵入式编程。现在了解到的Spring提供的功能有,DI,IOC,数据库操作,AOP,MVC。针对DI,AOP写了一些小DEMO

PS:AOP真的很棒

代码参考:《Spring实战》第三版

环境:

win7 64

ide eclipse

jdk 1.8

spring 4.1.7

------------------------------------------------我是代码分割线---------------------------------------------------------

思路:

一个表演者和观众,表演者和观众通过DI注入(XML和注解注入),观众在表演者表演前,时,后的动作通过AOP实现(数据库写了一些配置文件)

表演者Performer.java

 1 package com.gxf.beans;
 2 
 3 /**
 4  * 表演者
 5  * @author GXF
 6  *
 7  */
 8 public class Performer {
 9     
10     /**
11      * 表演者表演行为
12      */
13     public void perform(){
14         System.out.println("表演者开始表演");
15     }
16 }

观众Audience.java

 1 /**
 2  * 观众
 3  * @author GXF
 4  *
 5  */
 6 public class Audience {
 7     
 8     /**
 9      * 表演前观众开始找座位
10      */
11     public void takeSeats(){
12         System.out.println("观众开始找座位");
13     }
14     
15     /**
16      * 表演前观众关机
17      */
18     public void turnOffCellPhone(){
19         System.out.println("观众将手机关机");
20     }
21     
22     /**
23      * 表演结束观众开始鼓掌
24      */
25     public void applaud(){
26         System.out.println("观众开始鼓掌");
27     }
28     
29     /**
30      * 表演失败,观众要求退钱
31      */
32     public void demandRefund(){
33         System.out.println("嘘!退票!");
34     }
35 }

配置文件config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="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" >
        
    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

    <bean id="user" class="com.gxf.beans.User">
        <property name="name">
            <value>zhangsan</value>
        </property>
    </bean>
    <bean id="phone" class="com.gxf.beans.Phone">
        <property name="number">
            <value>13608196502</value>
        </property>
    </bean>
    <!-- 配置表演者和观众的bean -->
    <bean id="performer" class="com.gxf.beans.Performer">
    </bean>
    <bean id="audience" class="com.gxf.beans.Audience"></bean>
    <!-- 配置面向切面 -->
    <aop:config>
        <aop:aspect ref="audience">
            <aop:before pointcut="execution(* com.gxf.beans.Performer.perform(..))" method="takeSeats"/>
            <aop:after pointcut="execution(* com.gxf.beans.Performer.perform(..))" method="applaud"/>
        </aop:aspect>
        
    </aop:config>
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName">
            <value>com.mysql.jdbc.Driver</value>
        </property>
        <property name="url">
            <value>jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8</value>
        </property>
        <property name="username">
            <value>root</value>
        </property>
        <property name="password">
            <value>12345</value>  
        </property>   
    </bean>
</beans>

测试程序Test.java

 1 import org.springframework.context.ApplicationContext;
 2 import org.springframework.context.support.ClassPathXmlApplicationContext;
 3 
 4 import com.gxf.beans.Performer;
 5 
 6 public class Test {
 7 
 8     public static void main(String[] args) {
 9         ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
10         Performer performer = (Performer) context.getBean("performer");
11         performer.perform();
12         
13     }
14 
15 }

中间有些测试DI和没有完成的数据库代码,数据库我觉得还是使用ORM框架好一点,虽然Spring提供了一些模板方法,对数据库进行管理,不过使用起来没有ORM框架方便

源码:http://pan.baidu.com/s/1jGKnKv8

原文地址:https://www.cnblogs.com/luckygxf/p/4710127.html