基於代理類ProxyFactoryBean的AOP實現---異常通知編碼執行個體_異常

來源:互聯網
上載者:User

 代碼:

1.設定檔

  applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><!-- 配置建立UserDAOImpl的執行個體 --><bean id="userDAO" class="com.springtest1.dao.UserDAOImpl"> </bean><!-- 配置建立UserBizImpl的執行個體 --><bean id="userBiz" class="com.springtest1.biz.UserBizImpl">  <!-- 依賴注入資料訪問層組件 -->  <property name="userDAO" ref="userDAO" /></bean><!-- 定義前置通知 --><bean id="logAdvice" class="com.springtest1.aop.LogAdvice"></bean>        <!-- 定義後置通知 --><bean id="afterLogAdvice" class="com.springtest1.aop.AfterLogAdvice"></bean><!-- 定義異常通知 --><bean id="throwsLogAdvice" class="com.springtest1.aop.ThrowsLogAdvice"></bean><!-- 定義代理類,名 稱為ub,將通過ub訪問業務類中的方法 --><bean id="ub" class="org.springframework.aop.framework.ProxyFactoryBean">  <property name="proxyInterfaces">    <value>com.springtest1.biz.UserBiz</value>  </property>  <property name="interceptorNames">    <list>          <value>logAdvice</value>     <!-- 織入後置通知 -->     <value>afterLogAdvice</value>        <!-- 織入異常通知 -->     <value>throwsLogAdvice</value>    </list>  </property>  <property name="target" ref="userBiz"></property></bean></beans>

 

 2.資料訪問層

     UserDAO.java

package com.springtest1.dao;public interface UserDAO {   //添加使用者public void addUser(String username,String password);//刪除使用者public void delUser(int id);}

 

   UserDAOImpl.java

package com.springtest1.dao;public class UserDAOImpl implements UserDAO {@Overridepublic void addUser(String username, String password) {// TODO Auto-generated method stub       System.out.println(username+"使用者添加成功");}@Overridepublic void delUser(int id) {// TODO Auto-generated method stub       System.out.println("編號為"+id+"的使用者被刪除");}}


 

3.編寫商務邏輯層

   UserBiz.java

package com.springtest1.biz;public interface UserBiz {   //添加使用者public void addUser(String username,String password);//刪除使用者public void delUser(int id);}


    UserBizImpl.java

package com.springtest1.biz;public interface UserBiz {   //添加使用者public void addUser(String username,String password);//刪除使用者public void delUser(int id);}


 

package com.springtest1.biz;import com.springtest1.dao.UserDAO;import com.springtest1.dao.UserDAOImpl;public class UserBizImpl implements UserBiz {//使用UserDAO介面聲明了一個對象//並為其添加set方法,用於依賴注入UserDAO userDAO;public void setUserDAO(UserDAO userDAO){this.userDAO=userDAO;}@Overridepublic void addUser(String username, String password) {// TODO Auto-generated method stub       userDAO.addUser( username, password);}@Overridepublic void delUser(int id) {// TODO Auto-generated method stub       userDAO.delUser(id);       throw new RuntimeException("這是特意拋出的異常資訊。");}}

 

 4.編寫方面代碼

    ThrowsLogAdvice.java

package com.springtest1.aop;import org.apache.log4j.Logger;import java.lang.reflect.Method;import org.springframework.aop.*;public class ThrowsLogAdvice implements ThrowsAdvice {private Logger logger=Logger.getLogger(LogAdvice.class);    public void afterThrowing(Method method, Object[] args, Object target, Throwable exeptionClass){    //擷取被調用的類名String targetClassName=target.getClass().getName();//擷取被調用的方法名String targetMethodName=method.getName();//日誌格式字串String logInfoText="異常通知:執行"+targetClassName+"類的"+targetMethodName+"方法時發生異常";//將日誌資訊寫入配置的檔案中logger.info(logInfoText);    }}

 

5.測試類別

  AOPTest.java

package com.springtest1;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.springtest1.biz.UserBiz;public class AOPTest {public static void main(String[] args) {// TODO Auto-generated method stub//載入applicationContext.xml配置ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");//擷取配置中的UserBizImpl執行個體UserBiz userBiz =(UserBiz)context.getBean("ub");userBiz.addUser("zhangsan","123");userBiz.delUser(1);} }


 

 6.截圖


 



package com.springtest1.dao;public interface UserDAO {   //添加使用者public void addUser(String username,String password);//刪除使用者public void delUser(int id);}


 


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.