1、導包
4+2 +2+2 spring(aop aspect) spring依賴包
2、準備目標對象(UserServiceImpl)
package cn.hd.spring_proxy;
public interface UserService {
void add();
void delete();
void update();
void find();
}
package cn.hd.spring_proxy.impl;
import cn.hd.spring_proxy.UserService;
public class UserServiceImpl implements UserService {
@Override
public void add() {
// System.out.println("開啓事務");
System.out.println("添加用戶");
// System.out.println("提交用戶");
}
@Override
public void delete() {
// System.out.println("開啓事務");
System.out.println("刪除用戶");
// System.out.println("提交事務");
}
@Override
public void update() {
// System.out.println("開啓事務");
System.out.println("更新用戶");
// System.out.println("提交事務");
}
@Override
public void find() {
// System.out.println("開啓事務");
System.out.println("查詢用戶");
// System.out.println("提交事務");
}
}
3、準備通知
package cn.hd.spring_proxy;
import org.aspectj.lang.ProceedingJoinPoint;
public class MyAdvice {
public void before(){
System.out.println("在目標方法前調用。");
}
public void afterReturning(){
System.out.println("如果目標對象方法沒有出現異常,就會在該方法調用後調用。");
}
public Objectaround(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("環繞前在目標方法前都調用。");
Object proceed = pjp.proceed();
System.out.println("環繞後在目標方法後都調用。");
return proceed;
}
public void after(){
System.out.println("目標方法前後,不管有沒有異常都調用。");
}
public void throwException(){
System.out.println("目標方法出現異常調用該方法。");
}
}
4、配置applicationContext.xml文件
1、導入約束
(1).直接打入<aop:conf如果有提示,直接打回車,自動幫你導入約束。
(2).如多沒有提示,手動導入約束
xmlns:aop="http://www.springframework.org/schema/aop"
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
2、配置切入
<aop:config>……</aop:config>
3、配置切入點
<!--配置切入點
execution(那個對象的那個方法:public void cn.hd.spring_proxy.impl.UserServiceImpl.add())
public:可以默認不寫
void:可以用*來代替 代表任意的返回值類型 * cn.hd.spring_proxy.impl.UserServiceImpl.add()
所有方法可以用*來代替 * cn.hd.spring_proxy.impl.UserServiceImpl.*()
()裏面可能有參數,可以用..來代替 * cn.hd.spring_proxy.impl.UserServiceImpl.*(..)
一般給多個實現類配置切入點,所以可以用*來代替 *cn.hd.spring_proxy.impl.*ServiceImpl.*(..)
impl..代表有可能還有子包 *cn.hd.spring_proxy.impl..*ServiceImpl.*(..)
-->
<aop:pointcut id="pc" expression="execution(*cn.hd.spring_proxy..impl.*ServiceImpl.*(..))"></aop:pointcut>
4、配置切面(將通知織入到對應的切入點)
<!--配置切面-->
<!--織入通知
ref(通知):把某個通知織入到代理對象中
將myAdvice織入到expression中
-->
<aop:aspect ref="myAdvice">
<!--多種方式切入,總共有五種-->
<!--目標調用方法前
pointcut-ref="pc":對應上面的id
method="before":通知中增強的代碼
-->
<!--<aop:beforemethod="before" pointcut-ref="pc"></aop:before>-->
<!--不管方法是否出現異常,都執行-->
<!--<aop:aftermethod="after"pointcut-ref="pc"></aop:after>-->
<!--環繞前後調用,如果有異常則不調用環繞後方法,方法中必須有 ProceedingJoinPoint參數-->
<!--<aop:aroundmethod="around"pointcut-ref="pc"></aop:around>-->
<!--如果出現異常就不調用,不出現異常,在方法後調用-->
<!--<aop:after-returningmethod="afterReturning"pointcut-ref="pc"></aop:after-returning>-->
<!--平常都不調用,出現異常才調用-->
<aop:after-throwing method="throwException"pointcut-ref="pc"></aop:after-throwing>
</aop:aspect>
</aop:config>
5、測試代碼
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:cn/hd/spring_proxy/applicationContext.xml")
public class Demo {
/*對應配置bean類的名字*/
@Resource(name = "userService")
private UserService userService;
@Test
public void fun1(){
userService.add();
}
}