springAOP的简单实现

面向方面编程 (AOP) 提供从另一个角度来考虑程序结构以完善面向对象编程(OOP)。面向对象将应用程序分解成 各个层次的对象,而AOP将程序分解成各个方面 或者说 关注点 。这使得可以模块化诸如事务管理等这些横切多个对象的关注点。(这些关注点术语称作横切关注点。)

我们通过一个简单的实例来先进入spring的aop,面向方面(面向切面)编程

  • 编写接口类Foo
public interface Foo {
    public String pringMessage(String message);
}
  • 编写接口类实现FooImpl
public class FooImpl implements Foo{
    public String pringMessage(String message) {
        return "This Object is "+message;
    }
}

功能很简单,只是返回一段字符串

  • 编写动态代理接口实现类实现(InvocationHandler)
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.BasicConfigurator;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class MyInvocationHandler implements InvocationHandler{
    private Object foolImpl;

    public MyInvocationHandler(Object foolImpl) {
        this.foolImpl = foolImpl;
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        BasicConfigurator.configure();
        Log log = LogFactory.getLog(MyInvocationHandler.class);
        log.info("This is "+MyInvocationHandler.class.getName()+" You want invoke "+method.getName());
        return method.invoke(foolImpl,args);
    }
}
  • 建立bean工厂
import org.springframework.beans.factory.FactoryBean;

import java.lang.reflect.Proxy;

public class MyFactoryBean implements FactoryBean{
    private String target ;
    private String interf ;

    public Object getObject() throws Exception {
        Class[] interClass = new Class[]{Class.forName(interf)};
        Object objTarget = Class.forName(target).newInstance();
        Object objProxy = Proxy.newProxyInstance(Foo.class.getClassLoader(), interClass,
                new MyInvocationHandler(objTarget));

        return objProxy;
    }

    public Class<?> getObjectType() {
        return null;
    }

    public boolean isSingleton() {
        return false;
    }

    public String getTarget() {
        return target;
    }

    public void setTarget(String target) {
        this.target = target;
    }

    public String getInterf() {
        return interf;
    }

    public void setInterf(String interf) {
        this.interf = interf;
    }
}
  • 新建application.xml和log4j.protityes
<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    <bean id="bean" class="com.zwq.springaop.MyFactoryBean">
        <property name="target">
            <value>com.zwq.springaop.FooImpl</value>
        </property>
        <property name="interf">
            <value>com.zwq.springaop.Foo</value>
        </property>
    </bean>
</beans>
log4j.rootLogger=info,sysout
log4j.appender.sysout=org.apache.log4j.ConsoleAppender
log4j.appender.sysout.layout=org.apache.log4j.SimpleLayout
  • 新建测试类_Main
public class _Main {
    public static void main(String[] args) {
        Log log = LogFactory.getLog(_Main.class);
        BeanFactory factory = new XmlBeanFactory(new ClassPathResource("/application.xml"));
        Foo foo = (Foo)factory.getBean("bean");
        log.info(foo.pringMessage("Hello,world"));
    }
}

执行日志

"C:\Program Files\Java\jdk1.7.0_80\bin\java" -Didea.launcher.port=7532 -Didea.launcher.bin.path=D:\ideaIU-2016.3.1.win\bin -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.7.0_80\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\jfxrt.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\rt.jar;D:\IdeaProjects\springAopTest\target\classes;C:\Users\DELL\.m2\repository\org\springframework\spring-core\4.1.7.RELEASE\spring-core-4.1.7.RELEASE.jar;C:\Users\DELL\.m2\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;C:\Users\DELL\.m2\repository\org\springframework\spring-beans\4.1.7.RELEASE\spring-beans-4.1.7.RELEASE.jar;C:\Users\DELL\.m2\repository\org\springframework\spring-context\4.1.7.RELEASE\spring-context-4.1.7.RELEASE.jar;C:\Users\DELL\.m2\repository\org\springframework\spring-aop\4.1.7.RELEASE\spring-aop-4.1.7.RELEASE.jar;C:\Users\DELL\.m2\repository\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;C:\Users\DELL\.m2\repository\org\springframework\spring-expression\4.1.7.RELEASE\spring-expression-4.1.7.RELEASE.jar;C:\Users\DELL\.m2\repository\org\springframework\spring-jdbc\4.1.7.RELEASE\spring-jdbc-4.1.7.RELEASE.jar;C:\Users\DELL\.m2\repository\org\springframework\spring-tx\4.1.7.RELEASE\spring-tx-4.1.7.RELEASE.jar;C:\Users\DELL\.m2\repository\org\springframework\spring-web\4.1.7.RELEASE\spring-web-4.1.7.RELEASE.jar;C:\Users\DELL\.m2\repository\org\springframework\spring-webmvc\4.1.7.RELEASE\spring-webmvc-4.1.7.RELEASE.jar;C:\Users\DELL\.m2\repository\org\springframework\spring-test\4.1.7.RELEASE\spring-test-4.1.7.RELEASE.jar;C:\Users\DELL\.m2\repository\org\slf4j\slf4j-api\1.7.12\slf4j-api-1.7.12.jar;C:\Users\DELL\.m2\repository\ch\qos\logback\logback-core\1.1.1\logback-core-1.1.1.jar;C:\Users\DELL\.m2\repository\ch\qos\logback\logback-classic\1.1.1\logback-classic-1.1.1.jar;C:\Users\DELL\.m2\repository\log4j\log4j\1.2.17\log4j-1.2.17.jar;D:\ideaIU-2016.3.1.win\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain com.zwq.springaop._Main
INFO - Loading XML bean definitions from class path resource [application.xml]
INFO - This is com.zwq.springaop.MyInvocationHandler You want invoke pringMessage
264 [main] INFO com.zwq.springaop.MyInvocationHandler  - This is com.zwq.springaop.MyInvocationHandler You want invoke pringMessage
INFO - This Object is Hello,world
264 [main] INFO com.zwq.springaop._Main  - This Object is Hello,world

Process finished with exit code 0

案例所用的jar包引用

<dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <!--Spring依赖-->
        <!--spring核心-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
        <!--spring的DAO层依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
        <!--spring的WEB层依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
        <!--spring的单元测试依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
        <!-- 日志 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.12</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.1.1</version>
        </dependency>
        <!--实现slf4j接口并进行整合-->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值