Java?Spring切面(implements InvocationHandler)
package OA.Dao;
public interface IUserDao<T> {
public void Add(T T);
public void Delete(
int id);
}
package OA.Dao;
import org.springframework.stereotype.Repository;
import OA.Entity.User;
@Repository(
"mDao")
public class UserDao implements IUserDao<User> {
@Override
public void Add(User T) {
System.out.println(
"数据层:添加动作" + T.getName() + T.getId());
}
@Override
public void Delete(
int id) {
System.out.println(
"数据层:删除动作");
}
}
package OA.Service;
public interface IUserService<T> {
public void add(T T);
public void delete(
int id);
}
package OA.Service;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import OA.Dao.IUserDao;
import OA.Entity.User;
@Service(
"mService")
public class UserService implements IUserService<User> {
private IUserDao<User> IUserDao;
public IUserDao<User>
getIUserDao() {
return IUserDao;
}
@Resource(name =
"mylog")
public void setIUserDao(IUserDao<User> iUserDao) {
IUserDao = iUserDao;
}
@Override
public void add(User T) {
System.
out.println(
"service:添加" + T.getName() + T.getId());
IUserDao.Add(T);
}
@Override
public void delete(
int id) {
System.
out.println(
"service:删除");
IUserDao.Delete(id);
}
}
package OA.Action;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import OA.Entity.User;
import OA.Service.IUserService;
@Controller(
"mAction")
public class UserAction {
private IUserService<User> UserService;
private User user;
public User
getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public IUserService<User>
getUserService() {
return UserService;
}
@Resource(name =
"mylog1")
public void setUserService(IUserService<User> userService) {
UserService = userService;
}
public void add() {
System.out.println(
"action:添加" + user.getName() + user.getId());
UserService.add(user);
}
public void delete() {
}
}
/切面/
package OA.Proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import OA.Entity.Logger;
public class LogerProxy implements InvocationHandler {
private LogerProxy() {
}
private Object targer;
public static Object
getInstance(Object o) {
LogerProxy proxy =
new LogerProxy();
proxy.targer = o;
System.out.println(
"走了loger");
return Proxy.newProxyInstance(o.getClass().getClassLoader(), o.getClass().getInterfaces(), proxy);
}
@Override
public Object
invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Logger.info(
"执行日志");
return method.invoke(targer, args);
}
}
package OA.Entity;
public class Logger {
public static void info(String info) {
System.
out.println(info);
}
}
<?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.xsd">
<context:annotation-config />
<context:component-scan base-package="OA"/>
<bean id="mylog" class="OA.Proxy.LogerProxy" factory-method="getInstance">
<constructor-arg ref="mDao"></constructor-arg>
</bean>
<bean id="mylog1" class="OA.Proxy.LogerProxy" factory-method="getInstance">
<constructor-arg ref="mService"></constructor-arg>
</bean>
</beans>
package OA
.Service
import org
.springframework.beans.factory.BeanFactory
import org
.springframework.context.support.ClassPathXmlApplicationContext
import OA
.Action.UserAction
import OA
.Dao.UserDao
import OA
.Entity.User
public class Test {
public static void main(String[] args) {
User u = new User()
u
.setId(
1)
u
.setName(
"name")
BeanFactory factory = new ClassPathXmlApplicationContext(
"beas.xml")
// UserAction UserAction = (UserAction) factory
.getBean(
"mylog")
UserAction UserAction = (UserAction) factory
.getBean(UserAction
.class,
"mAction")
UserAction
.setUser(u)
UserAction
.add()
}
}
Console:
走了loger
走了loger
action:添加name1
执行日志
service:添加name1
执行日志
数据层:添加动作name1
Java?Spring代理模式(AOP)
package OA.Dao;
public interface IUserDao<T> {
public void Add(T T);
public void Delete(
int id);
}
package OA.Dao;
import org.springframework.stereotype.Repository;
import OA.Entity.User;
@Repository(
"mDao")
public class UserDao implements IUserDao<User> {
@Override
public void Add(User T) {
System.out.println(
"数据层:添加动作" + T.getName() + T.getId());
}
@Override
public void Delete(
int id) {
System.out.println(
"数据层:删除动作");
}
}
package OA.Service;
public interface IUserService<T> {
public void Add(T T);
public void Delete(
int id);
}
package OA.Service;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import OA.Dao.IUserDao;
import OA.Entity.User;
@Service(
"mService")
public class UserService implements IUserService<User> {
private IUserDao<User> IUserDao;
public IUserDao<User>
getIUserDao() {
return IUserDao;
}
@Resource
public void setIUserDao(IUserDao<User> iUserDao) {
IUserDao = iUserDao;
}
@Override
public void Add(User T) {
System.
out.println(
"service:添加" + T.getName() + T.getId());
IUserDao.Add(T);
}
@Override
public void Delete(
int id) {
System.
out.println(
"service:删除");
IUserDao.Delete(id);
}
}
package OA.Proxy;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import OA.Entity.Logger;
@Component(
"logAspect")
@Aspect
public class LogerAspect {
/**
* 在程序运行之前运行
*/
@Before(
"execution(* OA.Dao.*.Add*(..))||execution(* OA.Service.*.Delete*(..))")
public void Before() {
Logger.info(
"前:日志");
}
/**
* 在程序运行之后运行
*/
@After(
"execution(* OA.Service.*.Delete*(..))")
public void After() {
Logger.info(
"后:日志");
}
}
package OA
.Service
import org
.springframework.beans.factory.BeanFactory
import org
.springframework.context.support.ClassPathXmlApplicationContext
import OA
.Action.UserAction
import OA
.Dao.UserDao
import OA
.Entity.User
public class Test {
public static void main(String[] args) {
User u = new User()
u
.setId(
1)
u
.setName(
"name")
BeanFactory factory = new ClassPathXmlApplicationContext(
"beas.xml")
// UserAction UserAction = (UserAction) factory
.getBean(
"mylog")
UserAction UserAction = (UserAction) factory
.getBean(UserAction
.class,
"mAction")
UserAction
.setUser(u)
//UserAction
.Delete(
5)
UserAction
.Add()
}
}
<?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"
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.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:annotation-config />
<context:component-scan base-package="OA"/>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
Console:
action:添加name1
service:添加name1
前:日志
数据层:添加动作name1
/*
首先xml文件配置
:
xmlns:aop=
"http://www.springframework.org/schema/aop"
和
http:/
/www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
在spring-framework-
4.3.
7.
RELEASE\docs\spring-framework-reference\html\xsd-configuration.html
在html的
41.2.
6 the tx (transaction) schema复制内容
*
/
Java?Spring代理模式(AOP(@Around))
package OA
.Proxy
import org
.aspectj.lang.ProceedingJoinPoint
import org
.aspectj.lang.annotation.After
import org
.aspectj.lang.annotation.Around
import org
.aspectj.lang.annotation.Aspect
import org
.aspectj.lang.annotation.Before
import org
.springframework.stereotype.Component
import OA
.Entity.Logger
@Component(
"logAspect")
@Aspect
public class LogerAspect {
// 第一个* 表示任意返回类型
// 任意类
// 以
add开头的任意方法
// ..代表任意参数
//
// @Before(
"execution(* OA.Dao.*.Add*(..))||execution(* OA.Service.*.Delete*(..))")
// public void Before() {
// Logger
.info(
"前:日志")
// }
//
// @After(
"execution(* OA.Service.*.Delete*(..))")
// public void After() {
// Logger
.info(
"后:日志")
// }
@Around(
"execution( * OA.Dao.*.Add*(..))")
public void logAround(ProceedingJoinPoint pj) throws Throwable {
Logger
.info(
"前:日志")
pj
.proceed()
System
.out.println(pj
.getTarget())
System
.out.println(pj
.getSignature()
.getName())
Logger
.info(
"后:日志")
}
}
Console:
action:添加name1
service:添加name1
前:日志
数据层:添加动作name1
OA
.Dao.UserDao@16293aa2
Add
后:日志
@Around(
"execution( * OA.Dao.*.Add*(..))")
public void logAround(ProceedingJoinPoint pj) throws Throwable {
Logger
.info(
"前:日志")
pj
.proceed()
System
.out.println(pj
.getTarget())
System
.out.println(pj
.getSignature()
.getName())
Logger
.info(
"后:日志")
}
Java?Spring代理模式(AOP(@Around(xml配置)))
通过上述什么都不变,该类:
package OA
.Proxy
import org
.aspectj.lang.ProceedingJoinPoint
import org
.aspectj.lang.annotation.After
import org
.aspectj.lang.annotation.Around
import org
.aspectj.lang.annotation.Aspect
import org
.aspectj.lang.annotation.Before
import org
.springframework.stereotype.Component
import OA
.Entity.Logger
@Component(
"logAspect")
//@Aspect
public class LogerAspect {
// 第一个* 表示任意返回类型
// 任意类
// 以
add开头的任意方法
// ..代表任意参数
//
// @Before(
"execution(* OA.Dao.*.Add*(..))||execution(* OA.Service.*.Delete*(..))")
// public void Before() {
// Logger
.info(
"前:日志")
// }
//
// @After(
"execution(* OA.Service.*.Delete*(..))")
// public void After() {
// Logger
.info(
"后:日志")
// }
//@Around(
"execution( * OA.Dao.*.Add*(..))")
public void logAround(ProceedingJoinPoint pj) throws Throwable {
Logger
.info(
"前:日志")
pj
.proceed()
System
.out.println(pj
.getTarget())
System
.out.println(pj
.getSignature()
.getName())
Logger
.info(
"后:日志")
}
}
xml配置
<aop:config>
//判断@Component("logAspect")
<aop:aspect id="mylogAspect" ref="logAspect">
//条件
<aop:pointcut expression="execution( * OA.Dao.*.Add*(..))" id="logPoincut"/>
//根据id执行方法找到条件继续运行
<aop:around method="logAround" pointcut-ref="logPoincut"/>
</aop:aspect>
</aop:config>
Console:
action:添加name1
service:添加name1
前:日志
数据层:添加动作name1
OA
.Dao.UserDao@16293aa2
Add
后:日志