spring 使用XML配置开发Spring AOP

  XML方式开发AOP与注解开发原理是相同的,所以这里主要介绍一些用法即可。这里需要在XML中引入AOP的命名空间,所以先来了解一下AOP可配置的元素

  代码清单:切面类

package com.ssm.chapter11.xml.aspect;

public class XmlAspect {

    public void before() {
        System.out.println("before ......");
    }

    public void after() {
        System.out.println("after ......");
    }

    public void afterThrowing() {
        System.out.println("after-throwing ......");
    }

    public void afterReturning() {
        System.out.println("after-returning ......");
    }

}

  没有任何的注解,这就意味着需要我们使用XML去向Spring IoC容器描述它们。

  代码清单:spring-cfg4.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-4.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

    <bean id="xmlAspect" class="com.ssm.chapter11.xml.aspect.XmlAspect"/>
    <bean id="roleService" class="com.ssm.chapter11.xml.service.impl.RoleServiceImpl"/>

    <aop:config>
        <!-- 引用xmlAspect作为切面 -->
        <aop:aspect ref="xmlAspect">
            <!-- 定义切点 -->
            <aop:pointcut id="printRole" expression="execution(* com.ssm.chapter11.xml.service.impl.RoleServiceImpl.printRole(..))"/>
            <!-- 定义通知,引入切点 -->
            <aop:before method="before" pointcut-ref="printRole"/>
            <aop:after method="after" pointcut-ref="printRole"/>
            <aop:after-throwing method="afterThrowing" pointcut-ref="printRole"/>
            <aop:after-returning method="afterReturning" pointcut-ref="printRole"/>

            <aop:around method="around" pointcut-ref="printRole"/>
        </aop:aspect>
    </aop:config>

</beans>

  代码清单:测试类

public class Main {

    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("ssm/chapter11/spring-cfg4.xml");
        RoleService roleService = ctx.getBean(RoleService.class);
        Role role = new Role();
        role.setId(1L);
        role.setRoleName("role_name_1");
        role.setNote("note_1");
        roleService.printRole(role);
    }

}

原文地址:https://www.cnblogs.com/ooo0/p/11018752.html

时间: 2024-07-28 12:30:15

spring 使用XML配置开发Spring AOP的相关文章

Spring 基于xml配置方式的AOP

我们具体用代码来说明: 1.ArithmeticCalculator.java 1 package com.proc; 2 3 public interface ArithmeticCalculator { 4 int add(int i, int j); 5 int sub(int i, int j); 6 7 int mul(int i, int j); 8 int div(int i, int j); 9 } 2.ArithmeticCalculatorImpl.java 实现接口Arit

基于XML配置的Spring MVC

1.添加jar 2.web.xml配置 <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation

java实现Spring在XML配置java类

1.创建自己的bean文件:beans.xml <?xml version="1.0" encoding="UTF-8"?> <busi-beans> <beans> <bean id="SysHelloImpl" type="com.cxm.test.SysHello"> <desc>test</desc> <impl-class>com.c

Spring 使用xml配置aop

1.xml文件需要引入aop命名空间 2.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:aop="http://www

【Spring】Spring使用XML配置声明式事务

林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 一.事务介绍 事务简介: 事务管理是企业级应用程序开发中必不可少的技术,用来确保数据的完整性和一致性 事务就是一系列的动作,它们被当作一个单独的工作单元.这些动作要么全部完成,要么全部不起作用. 事务的四个关键属性(ACID) ① 原子性(atomicity):事务室一个原子操作,有一系列动作组成.事务的原子性确保动作要么全部完成,要么完全不起作用② 一致性(consistency):一旦所

基于注解的Spring MVC(所需jar包,web.xml配置,Spring文件配置,@Controller,@RequestMapping,@RequestParam,model填參,EL取值)

1.加入jar 2.web.xml配置: <?xml version="1.0" encoding="UTF-8"? > <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocati

Spring 中 Xml配置文件属性的说明

Xml配置文件属性的说明: <bean id="TheAction" ⑴ class="net.xiaxin.spring.qs.UpperAction" ⑵ singleton="true" ⑶ init-method="init" ⑷ destroy-method="cleanup" ⑸ depends-on="ActionManager" ⑹ > <propert

Spring之XML配置Bean的属性注入

Spring中XML文件配置Bean的简单示例,如下: <bean id="car" class="com.smart.ditype.Car"> <property name="color"> <value>红色</value> </property> </bean> 注:在上述例子中,<property>标签对应的属性类型是基础数据类型,Spring容器会将它的

XML配置下Spring Bean的注入

主题 之前学习了@Autowired下SpringBean是怎么注入到属性中的. https://www.cnblogs.com/abcwt112/p/12541783.html 现在学习下远古时代的XML是怎么注入的. Ac初始化 入口是XML的AC. new的时候回做refresh方法 refresh之前的文章分享过大致做了什么 https://www.cnblogs.com/abcwt112/p/12388982.html 其中这一步会去掉BeanFactory的getBean方法去初始化