程序人生

java EE框架Spring Aop使用案例

时间:2017/4/27 13:51:36  作者:solgle  来源:solgle.com  查看:816  评论:0
内容摘要:Aop面向切面编程谁spring的主要特性,使用spring框架需要导入需要的jar,推介下载地址http://repo.springsource.org/libs-release-local/org/springframework/spring/ 主要使用的jar有spring...
Aop面向切面编程谁spring的主要特性,使用spring框架需要导入需要的jar,推介下载地址http://repo.springsource.org/libs-release-local/org/springframework/spring/ 
主要使用的jar有

spring-beans-4.3.8.RELEASE.jar
spring-context-4.3.8.RELEASE.jar
spring-context-support-4.3.8.RELEASE.jar
spring-core-4.3.8.RELEASE.jar
spring-expression-4.3.8.RELEASE.jar
commons-logging-1.2.jar
aspectjrt.jar
aspectjtools.jar
aspectjweaver.jar
org.aspectj.matcher.jar
 
其中jar包commons-logging-1.2.jar不在spring下载文件夹中,需要以下地址下载
http://commons.apache.org/proper/commons-logging/download_logging.cgi
最后
四个jar包也是额外的包,下载地址:http://www.eclipse.org/aspectj/ 
比如aspectj-1.8.10.jar下载下来后解压会包含这四个包
本文出自:http://www.solgle.com/news/361.html

首先new一个包busispring,里面分别再定义两个操作类,一个main调用类,一个配置文件。

//创建起步接口
package busispring;
 
public interface UserManager {
 
public void addUser(String user,String pass);
public void delUser(String user);
 
}
 
 
//定义接口实现类
package busispring;
 
public class UserManagerImpl implements UserManager{
 
@Override
public void addUser(String username,String password) {
// TODO Auto-generated method stub
System.out.println("begin add user...");
}
 
@Override
public void delUser(String username) {
// TODO Auto-generated method stub
System.out.println("del user");
}
 
 
}
 
 
//定义另一个操作类
package busispring;
 
import org.aspectj.lang.JoinPoint;
 
public class LogMarkWriter {
public void before(JoinPoint jp){
System.out.println("before:"+jp.getSignature());
}
 
public void after(JoinPoint jp){
System.out.println("after:"+jp.getSignature());
}
 
 
 
}
 
 
//配置文件bean.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="userManager" class="busispring.UserManagerImpl" />
  <bean id="logMark" class="busispring.LogMarkWriter" />    
  
 
  <aop:config>
    <aop:aspect id="myAop" ref="logMark">
    <aop:pointcut id="target" expression="execution(* busispring.UserManagerImpl.add*(..))" />
    <aop:before method="before" pointcut-ref="target" />
    <aop:after method="after" pointcut-ref="target" />
    </aop:aspect>
  </aop:config>
 
</beans>
 
 
//调用实现
package busispring;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class TestAop {
 
public static void main(String args[]){
ApplicationContext context = new ClassPathXmlApplicationContext("busispring/bean.xml"); 
UserManager userM=context.getBean("userManager",UserManager.class);
 
userM.addUser("user001", "secret");
userM.delUser("jione");
 
}
}
 
 
//运行结果
before:void busispring.UserManager.addUser(String,String)
begin add user...
after:void busispring.UserManager.addUser(String,String)
del user
 
 
 

标签:java框架Spring Aop案例 java框架 

solgle.com 版权所有,欢迎分享!!!

相关评论
 img1 img2 img3 img4 img5 img6 img7 img8 img9 img10
评论者:      验证码:  点击获取验证码
   Copyright © 2013-2028 solgle.com,All rights reserved.[solgle.com] 公安机关备案号:51010802000219
Email:solgle@solgle.com; weixin:cd1008610000 ICP:蜀ICP备14011070号-1