IWorkDao
package com.lbl.dao;
public interface IWorkDao {
void work();
}
WorkDaoImpl
/**
* Created by 李柏霖
* 2020/10/12 10:30
*/
package com.lbl.dao.Impl;
import com.lbl.dao.IWorkDao;
public class WorkDaoImpl implements IWorkDao {
@Override
public void work() {
System.out.println("我爱加班");
}
}
Advice
/**
* Created by 李柏霖
* 2020/10/12 10:32
*/
package com.lbl.aspect;
public class Advice {
public void writeLog(){
System.out.println("写入日志");
}
}
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: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-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="workDaoImpl" class="com.lbl.dao.Impl.WorkDaoImpl"></bean>
<bean id="advice" class="com.lbl.aspect.Advice"></bean>
<aop:config>
<aop:pointcut id="work" expression="execution(public void com.lbl.dao.Impl.WorkDaoImpl.work())"/>
<aop:aspect ref="advice">
<aop:after method="writeLog" pointcut-ref="work"></aop:after>
</aop:aspect>
</aop:config>
</beans>
DaoTest
/**
* Created by 李柏霖
* 2020/10/12 10:37
*/
package com.lbl.dao;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class DaoTest {
@Autowired
IWorkDao iworkDao;
@Test
public void work(){
System.out.println(iworkDao);
iworkDao.work();
}
}
测试运行结果:
AOP切面编程-切面表达式
- (1)什么是切面表达式
execution([修饰符] 返回值类型 包.类.方法(参数列表) );
- (2)切面表达式有什么用?
符合表达式的方法,会被增强
使用* 表过任意的内容
使用… 可以表示包与子包下面的类
使用…可以写在方法(…)表示任意参数
我这边在上面代码的基础上再加一个eat()方法,然后用通配符的方法,同时配置这两个方法。
IWorkDao
package com.lbl.dao;
public interface IWorkDao {
void work();
void eat();
}
WorkDaoImpl
/**
* Created by 李柏霖
* 2020/10/12 10:30
*/
package com.lbl.dao.Impl;
import com.lbl.dao.IWorkDao;
public class WorkDaoImpl implements IWorkDao {
@Override
public void work() {
System.out.println("我爱加班");
}
@Override
public void eat() {
System.out.println("吃饭");
}
}
Advice方法同上不变。
applicationContext.xml
这里面用了通配符的方法,将任意返回值的,在com.lbl.dao包以及其子包下的所有方法,参数任意。都织入writeLog这个方法。
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="workDaoImpl" class="com.lbl.dao.Impl.WorkDaoImpl"></bean>
<bean id="advice" class="com.lbl.aspect.Advice"></bean>
<aop:config>
<aop:pointcut id="work" expression="execution( * com.lbl.dao..*(..))"/>
<aop:aspect ref="advice">
<aop:after method="writeLog" pointcut-ref="work"></aop:after>
</aop:aspect>
</aop:config>
</beans>