您的位置:首页 > 编程语言 > ASP

Spring Aop开发基于AspectJ注解方式的案例

2016-12-16 17:03 1021 查看
工程结构:



运行效果:



代码如下:

ServiceAop.java

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class ServiceAop {

@Pointcut("execution(* com.sunsharing.web.service.*.*(..))")
public void commonPoint() {
};

public ServiceAop() {
System.out.println("初始化");
}

@Before("commonPoint()")
public void doBefore(JoinPoint jp) {
System.out.println("前置通知before++++++++++++++++++++++");
System.out.println(jp.toString());// 连接点所在位置的相关信息
System.out.println(jp.toShortString());// 连接点所在位置的简短相关信息
System.out.println(jp.toLongString());// 连接点所在位置的全部相关信息
System.out.println(jp.getThis()); // 返回AOP代理对象
System.out.println(jp.getTarget()); // 返回目标对象
System.out.println(jp.getArgs()); // 返回被通知方法参数列表
System.out.println(jp.getSignature()); // 返回当前连接点签名
System.out.println(jp.getSourceLocation());// 返回连接点方法所在类文件中的位置
System.out.println(jp.getKind()); // 连接点类型
System.out.println(jp.getStaticPart()); // 返回连接点静态部分
}

@After("commonPoint()")
public void doAfter() {
System.out.println("最后的通知after++++++++++++++++++++++");
}
}


UserController.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sunsharing.web.service.UserService;

@Controller
public class UserController {
@Autowired
private UserService userService;

@RequestMapping("/test.do")
public void save(){
userService.save();
}

}


UserService.java

import org.springframework.stereotype.Service;

@Service
public class UserService {

public void save() {

System.out.println("--------进入service-------");
}
}


applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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/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-3.0.xsd"> 
<context:component-scan base-package="com.sunsharing.web"/>
<context:annotation-config/>
<aop:aspectj-autoproxy proxy-target-class="true" />
</beans>


springmvc.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:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"> 
<context:component-scan base-package="com.sunsharing.web"></context:component-scan>

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

<!--视图解析器  -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

</beans>


web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>SpringAop</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<servlet>
<!-- 配置核心控制器 -->
<servlet-name>springmvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<!-- 定义加载的xml文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springmvc.xml,/WEB-INF/applicationContext.xml</param-value>
</init-param>
<!-- 启动时加载 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: