spring2.5 xml的简单入门示例(aop)

1.新建Java项目,创建lib文件夹,将spring.jar,commons-logging-1.1.1.jar、aspectj-1.7.3.jar、aspectjweaver-1.6.10.jar、cglib-nodep-2.2.jar复制在里面,并且右键jar包,add to build path,将其加入构建路径。

2.在src目录下创建com.dao.StudentDao类,类的主要作用的模拟操作数据库实体的dao层

package com.dao;

public class StudentDao {
    public void save(){
        System.out.println("save success!");
    }
}

3.在src目录下创建com.aspec.Aspectt类,这是切面类,即aspect调用的函数的类

package com.aspect;

import org.aspectj.lang.JoinPoint;

public class Aspect {
    public void beforeMethod(JoinPoint jp){
        System.out.println(jp.getTarget().getClass().getName()+"开始执行");
    }

    public void afterMethod(JoinPoint jp){
        System.out.println(jp.getTarget().getClass().getName()+"结束执行");
    }
}

4.在src目录下创建beans.xml,aop的相关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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

    <bean id="studentDao" class="com.dao.StudentDao"></bean>

    <bean id="aspect" class="com.aspect.Aspect"></bean>
    <aop:config>
        <aop:aspect id="myAspect" ref="aspect">
            <aop:pointcut id="myPointcut" expression="execution(* com.dao.*.*(..))"/>
            <aop:before pointcut-ref="myPointcut" method="beforeMethod"/>
            <aop:after pointcut-ref="myPointcut" method="afterMethod"/>
        </aop:aspect>
    </aop:config>


</beans>

5.在src下创建一个测试类com.test.Test

package com.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.dao.StudentDao;

public class Test {
    public static void main(String[] args){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        StudentDao sd = (StudentDao)context.getBean("studentDao");
        sd.save();
    }
}

运行即可看效果:
com.dao.StudentDao开始执行
save success!
com.dao.StudentDao结束执行

相关源码jar包下载地址:http://download.csdn.net/detail/qwcs163/9115733

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值