ContextLoaderListener接口

Create a new ContextLoaderListenerthat will create a web application context based on the "contextClass" and "contextConfigLocation" servlet context-params. See ContextLoadersuperclass documentation for details on default values for each.

This constructor is typically used when declaring ContextLoaderListener as a <listener> within web.xml, where a no-arg constructor is required.

这个接口实现了J2EE的ServletContextListener接口

通过listener 像Servlet容器注册 Web容器启动时 初始化Spring上下文的信息

    <!-- 加载Spring容器 -->
     <context-param>
         <param-name>contextConfigLocation</param-name>
         <param-value>classpath:applicationContext-core.xml</param-value>
      </context-param>
      <!-- 通过listener 像Servlet容器注册 Web容器启动时 初始化context-param的配置信息。-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

BeanFactory接口

  Spring通过BeanFactory接口的getBean来拿到我们所配置Bean的实列,交给Spring管理的Bean全部默认是单例。

  以下是批量扫描初始化Bean 交给Spring管理

 <context:component-scan base-package="com.sk.service.*"></context:component-scan>

IOC

ioc 是依赖注入,当我们的成员变量是Spring的一个Bean的时候,那这个成员变量可以由Spring帮我们注入(Spring会通过反射调用Set方法)

依赖注入也叫控制反转,以前编程完完全全控制在我自己的手里。用了Spring之后 成员变量的初始化过程控制过程反转到Spring手里。

注解用法:

  @Autowired
  DemoService demoService;

AOP

AOP的实现原理,动态代理。

从代理的原理我们知道,代理的目的是调用目标方法时可以转而执行InvocationHandlerinvoke方法,所以如何在InvocationHandler上做文章就是Spring实现AOP的关键所在。

Spring的AOP实现遵守AOP联盟的约定,同时Spring又扩展了它。增加了 PointCut Advisor接口使得其更加灵活

    <!-- 切面逻辑类的对象 -->
    <bean id="myInterceptor" class="com.sk.util.MyInterceptor"></bean>
    <aop:config>
        <!-- 在add方法上加各种各样的我们切入进来的逻辑 -->
        <aop:pointcut expression="execution(public * com.sk.service..*.*(..))" id="servicePointcut"/>

        <aop:aspect id = "logAspect" ref="myInterceptor">
            <!-- aop:pointcut可以加到aspect的里面来   加到里面的话 只能是logAspect 这个aspect使用 -->
            <aop:before method="before" pointcut-ref="servicePointcut"/>
            <aop:after method="after" pointcut-ref="servicePointcut"/>
        </aop:aspect>
    </aop:config>

当我们执行的时候 符合我们execution(public * com.sk.service..*.*(..))语法要求的方法的时候,

它会在方法执行之前 执行before方法(logInterceptor的before方法) 方法执行之后 执行after方法。

Junit测试

    @Test
    public void testIoc() {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("applicationContext-core.xml");
        DemoService demoService = beanFactory.getBean(DemoService.class);
        demoService.testAop();
    }

利用JoinPoint模拟AOP 实现事物管理

JoinPoint:连接点(AOP切面切到我们程序时的连接点,切入的那个点)

上面的配置保持不变,首先给我们的Service 加一个自定义的注解

@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface MyAnnotation {
    public String transaction() default "";
}
@Service
public class DemoServiceImpl  implements DemoService{
    @Override
    @MyAnnotation(transaction= "transaction")
    public void testAop() {
        System.out.println("excute Service***********");
    }
}

完善我们的切面逻辑类

当我们的切面发现这个注解的时候 就进行事物的控制

public class MyInterceptor {
    public void before(JoinPoint jp) throws Exception {
        MyAnnotation myAnnotation =getHandlerChain(jp);
        System.out.println("方法开始通过AOP拿到方法上的注解-开始事物"+myAnnotation.transaction());
    }
    public void after(JoinPoint jp)throws Exception  {
        MyAnnotation myAnnotation =getHandlerChain(jp);
        System.out.println("放过结束通过AOP拿到方法上的注解-结束事物"+myAnnotation.transaction());
    }
     // 取方法或者类上的HandlerChain注解,方法上的优先
    private MyAnnotation getHandlerChain(JoinPoint jp) throws Exception {
        MethodSignature methodSignature = (MethodSignature) jp.getSignature();
        Method realMethod = jp.getTarget().getClass().getDeclaredMethod(methodSignature.getName(), methodSignature.getParameterTypes());
        MyAnnotation myAnnotation = realMethod.getAnnotation(MyAnnotation.class);
        if(myAnnotation==null) {
            Class<? extends Object> cls = jp.getTarget().getClass();
            myAnnotation = (MyAnnotation) cls.getAnnotation(MyAnnotation.class);
        }
        return myAnnotation;
    }
}

打印结果

方法开始通过AOP拿到方法上的注解-开始事物transaction
excute Service***********
放过结束通过AOP拿到方法上的注解-结束事物transaction

这里只是打印模拟,项目中需要和JDBC(或者Mybatis Hibernate)结合,进而控制数据库transaction的开启和关闭

推荐文章JAVA动态代理设计模式(AOP背后的原理)

Spring新框架WebFlux

Spring的核心接口的更多相关文章

  1. spring 的核心接口

    spring有两个核心接口,BeanFactory 和ApplicationContext  ,其中ApplicationContext 是BeanFactory的子接口.他们代表了Spring容器. ...

  2. Spring事务核心接口

  3. spring中基础核心接口总结

    spring中基础核心接口总结理解这几个接口,及其实现类就可以快速了解spring,具体的用法参考其他spring资料 1.BeanFactory最基础最核心的接口重要的实现类有:XmlBeanFac ...

  4. Spring Boot REST(一)核心接口

    Spring Boot REST(一)核心接口 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html) SpringBoot RE ...

  5. 【Spring】Spring的事务管理 - 1、Spring事务管理概述(数据库事务、Spring事务管理的核心接口)

    Spring事务管理概述 文章目录 Spring事务管理概述 数据库事务 什么是Spring的事务管理? Spring对事务管理的支持 Spring事务管理的核心接口 Platform Transac ...

  6. Spring8:一些常用的Spring Bean扩展接口

    前言 Spring是一款非常强大的框架,可以说是几乎所有的企业级Java项目使用了Spring,而Bean又是Spring框架的核心. Spring框架运用了非常多的设计模式,从整体上看,它的设计严格 ...

  7. spring IOC核心原理

    下面来了解一下Spring到底是怎么运行的. public static void main(String[] args) { ApplicationContext context = new Fil ...

  8. spring之BeanFactoryAware接口

    springBeanFactoryAware (转)要直接在自己的代码中读取spring的bean,我们除了根据常用的set外,也可以通过spring的BeanFactoryAware接口实现,只要实 ...

  9. spring技术核心概念纪要

    一.背景 springframework 从最初的2.5版本发展至今,期间已经发生了非常多的修正及优化.许多新特性及模块的出现,使得整个框架体系显得越趋庞大,同时也带来了学习及理解上的困难. 本文阐述 ...

随机推荐

  1. spring-cloud-ribbon负载均衡组件

    Ribbon简介: Spring Cloud Ribbon 是一个基于 HTTP 和 TCP 的客户端负载均衡工具,它基于 Netflix Ribbon 实现. 通过 Spring Cloud 的封装 ...

  2. apache http添加证书转成https

    使用yum安装的apache. 第一步,需要把准备好的证书上传到服务器.具体位置:/app/cacerts/ 第二步,安装ssl. 命令:yum install mod_ssl openssl 安装完 ...

  3. maven环境搭建及创建maven项目

    Maven项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具. 1.maven下载地址http://maven.apache.org/download.cgi ...

  4. 关于Python 解包,你需要知道的一切

    解包在英文里叫做 Unpacking,就是将容器里面的元素逐个取出来(防杠精:此处描述并不严谨,因为容器中的元素并没有发生改变)放在其它地方,好比你老婆去菜市场买了一袋苹果回来分别发给家里的每个成员, ...

  5. [转] 使用Node.js实现简易MVC框架

    在使用Node.js搭建静态资源服务器一文中我们完成了服务器对静态资源请求的处理,但并未涉及动态请求,目前还无法根据客户端发出的不同请求而返回个性化的内容.单靠静态资源岂能撑得起这些复杂的网站应用,本 ...

  6. labview下载地址

    ftp://ftp.ni.com/evaluation/labview/ekit/other/downloader

  7. SparkSQL

    Spark SQL Spark SQL是Spark用来处理结构化数据的一个模块,它提供了2个编程抽象:DataFrame和DataSet,并且作为分布式SQL查询引擎的作用. Hive SQL是转换成 ...

  8. 第二项目AIaisell(易销宝)

    一.什么是报表 向上级报告情况的表格.简单的说:报表就是用表格.图表等格式来动态显示数据,可以用公式表示为:“报表 = 多样的格式 + 动态的数据” 表格:详细数据 图表: 直观 二.表格数据展示 2 ...

  9. 关于<超文本>定义

    百度百科定义: 1, 超文本是用超链接的方法,将各种不同空间的文字信息组织在一起的网状文本.它更是一种用户界面范式,用以显示文本及与文本之间相关的内容.现时超文本普遍以电子文档方式存在,其中的文字包含 ...

  10. H5分享功能

    web端分享功能 https://www.cnblogs.com/sdcs/p/8328367.html H5分享功能 公司里面做web开发经常会做H5页面,今天整理分享一下. 微信公众号平台 步骤一 ...