<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Spring AOP + AspectJ XML 配置示例 > 原文: [https://howtodoinjava.com/spring-aop/spring-aop-aspectj-xml-configuration-example/](https://howtodoinjava.com/spring-aop/spring-aop-aspectj-xml-configuration-example/) 如果您是仍然對 JDK 1.4 感興趣的極少數開發人員之一,或者您正在維護一個舊的 Spring 應用程序,其中 AOP 代碼已用 XML 配置文件編寫,那么此文章適合您。 在此處學習如何使用基于 xml 的配置來定義和使用 AspectJ 的 spring aop。 > **閱讀更多內容**:[使用注解配置](https://howtodoinjava.com/spring/spring-aop/spring-aop-aspectj-example-tutorial-using-annotation-config/)的 Spring AOP AspectJ 示例 ## 1)如何聲明切面 使用`<aop:aspect>`元素聲明一個切面,并使用`ref`屬性引用該支持 bean,如下所示: ```java <aop:config> <aop:aspect id="loggingAspect" ref="loggingAspectBean"> ... </aop:aspect> </aop:config> <bean id="loggingAspectBean" class="com.howtodoinjava.demo.aop.EmployeeCRUDLoggingAspect" /> ``` ## 2)如何定義切入點 切入點有助于確定要使用不同建議執行的連接點。 切入點將定義如下: ```java <aop:config> <aop:aspect id="loggingAspect" ref="loggingAspectBean"> <aop:pointcut id="loggingOperation" expression="execution(* com.howtodoinjava.demo.aop.EmployeeManager.*(..))" /> <aop:pointcut id="transactionOperation" expression="execution(* com.howtodoinjava.demo.aop.EmployeeManager.getEmployeeById(..))" /> </aop:aspect> </aop:config> <bean id="loggingAspectBean" class="com.howtodoinjava.demo.aop.EmployeeCRUDLoggingAspect" /> ``` 在上面的示例中,兩個切入點(`loggingOperation`和`transactionOperation`)將與`EmployeeManager`類中定義的方法匹配。 其中`loggingOperation`切入點將匹配`EmployeeManager`中定義的所有方法,而`transactionOperation`僅匹配`EmployeeManager.getEmployeeById()`方法執行。 > **閱讀更多內容**:[Spring AOP AspectJ 切入點表達式以及示例](https://howtodoinjava.com/spring/spring-aop/writing-spring-aop-aspectj-pointcut-expressions-with-examples/) ## 3)定義建議 您可以使用`<aop:advise_name>`元素在`<aop:aspect>`中聲明五個建議中的任何一個,如下所示: ```java <aop:config> <!-- Spring AOP Pointcut definitions --> <aop:pointcut id="loggingOperation" expression="execution(* com.howtodoinjava.demo.aop.EmployeeManager.*(..))" /> <aop:pointcut id="transactionOperation" expression="execution(* com.howtodoinjava.demo.aop.EmployeeManager.getEmployeeById(..))" /> <!-- Spring AOP aspect --> <aop:aspect id="loggingAspect" ref="loggingAspectBean"> <!-- Spring AOP advises --> <aop:before pointcut-ref="loggingOperation" method="logBefore" /> <aop:after pointcut-ref="loggingOperation" method="logAfter" /> </aop:aspect> </aop:config> ``` ## 基于 XML 的 Spring AOP 配置的示例應用程序 在此示例中,我在`EmployeeManager`接口內定義的所有方法的日志建議之前和之后應用`EmployeeManager.getEmployeeById()`方法的事務建議。 完整的配置文件如下: `applicationContext.xml` ```java <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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop/ http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <!-- We don't need to this; This is required only in annotation based AOP support --> <!-- <aop:aspectj-autoproxy /> --> <aop:config> <!-- Spring AOP Pointcut definitions --> <aop:pointcut id="loggingOperation" expression="execution(* com.howtodoinjava.demo.aop.EmployeeManager.*(..))" /> <aop:pointcut id="transactionOperation" expression="execution(* com.howtodoinjava.demo.aop.EmployeeManager.getEmployeeById(..))" /> <!-- Spring AOP aspect 1 --> <aop:aspect id="loggingAspect" ref="loggingAspectBean"> <!-- Spring AOP advises --> <aop:before pointcut-ref="loggingOperation" method="logBefore" /> <aop:after pointcut-ref="loggingOperation" method="logAfter" /> </aop:aspect> <!-- Spring AOP aspect 2 --> <aop:aspect id="transactionAspect" ref="transactionAspectBean"> <aop:before pointcut-ref="transactionOperation" method="getEmployeeById" /> </aop:aspect> </aop:config> <!-- Spring AOP aspect instances --> <bean id="loggingAspectBean" class="com.howtodoinjava.demo.aop.EmployeeCRUDLoggingAspect" /> <bean id="transactionAspectBean" class="com.howtodoinjava.demo.aop.EmployeeCRUDTransactionAspect" /> <!-- Target Object --> <bean id="employeeManager" class="com.howtodoinjava.demo.aop.EmployeeManagerImpl" /> </beans> ``` 本示例中使用的其他文件是: `EmployeeDTO.java` ```java public class EmployeeDTO { private Integer id; private String firstName; private String lastName; //Setters and Getters } ``` `EmployeeManager.java` ```java public interface EmployeeManager { public EmployeeDTO getEmployeeById(Integer employeeId); public List<EmployeeDTO> getAllEmployee(); public void createEmployee(EmployeeDTO employee); public void deleteEmployee(Integer employeeId); public void updateEmployee(EmployeeDTO employee); } ``` `EmployeeManagerImpl.java` ```java public class EmployeeManagerImpl implements EmployeeManager { public EmployeeDTO getEmployeeById(Integer employeeId) { System.out.println("Method getEmployeeById() called"); return new EmployeeDTO(); } public List<EmployeeDTO> getAllEmployee() { System.out.println("Method getAllEmployee() called"); return new ArrayList<EmployeeDTO>(); } public void createEmployee(EmployeeDTO employee) { System.out.println("Method createEmployee() called"); } public void deleteEmployee(Integer employeeId) { System.out.println("Method deleteEmployee() called"); } public void updateEmployee(EmployeeDTO employee) { System.out.println("Method updateEmployee() called"); } } ``` `EmployeeCRUDLoggingAspect.java` ```java public class EmployeeCRUDLoggingAspect { public void logBefore(JoinPoint joinPoint) { System.out.println("EmployeeCRUDAspect.logBefore() : " + joinPoint.getSignature().getName()); } public void logAfter(JoinPoint joinPoint) { System.out.println("EmployeeCRUDAspect.logAfter() : " + joinPoint.getSignature().getName()); } } ``` `EmployeeCRUDTransactionAspect.java` ```java public class EmployeeCRUDTransactionAspect { public void getEmployeeById(JoinPoint joinPoint) { System.out.println("EmployeeCRUDTransactionAspect.getEmployeeById() : " + joinPoint.getSignature().getName()); } } ``` `TestAOP.java` ```java public class TestAOP { @SuppressWarnings("resource") public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("com/howtodoinjava/demo/aop/applicationContext.xml"); EmployeeManager manager = ( EmployeeManager ) context.getBean("employeeManager"); manager.getEmployeeById(1); manager.createEmployee(new EmployeeDTO()); manager.deleteEmployee(100); } } Output: EmployeeCRUDAspect.logBefore() : getEmployeeById EmployeeCRUDTransactionAspect.getEmployeeById() : getEmployeeById Method getEmployeeById() called EmployeeCRUDAspect.logAfter() : getEmployeeById EmployeeCRUDAspect.logBefore() : createEmployee Method createEmployee() called EmployeeCRUDAspect.logAfter() : createEmployee EmployeeCRUDAspect.logBefore() : deleteEmployee Method deleteEmployee() called EmployeeCRUDAspect.logAfter() : deleteEmployee ``` 在下面的評論部分中給我任何評論/查詢。 **祝您學習愉快!**
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看