Spring的核心接口

ContextLoaderListener接口

Create a new ContextLoaderListener that 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

转载于:https://www.cnblogs.com/ssskkk/p/9281785.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring核心容器是Spring框架中最核心的部分,提供了管理和组织应用程序中组件的功能。它包含了一系列的模块,如文本、图片等各种资源文件,同时提供了应用程序所需的各种组件,如BeanFactory、ApplicationContext等。 首先,Spring核心容器提供了BeanFactory接口,用于对Java对象(也即Bean)进行管理和配置的工厂。通过配置文件或注解的方式,我们可以定义Bean的属性和依赖关系,从而使用容器来获取已经创建好的Bean实例。BeanFactory接口负责创建和管理这些Bean,同时也负责销毁它们。 其次,Spring核心容器还提供了ApplicationContext接口,它是BeanFactory的子接口,提供了更多的功能。ApplicationContext可以从多种来源(比如文件系统、数据库、网络等)加载配置信息,并管理Bean的生命周期。除了BeanFactory的所有功能,ApplicationContext还支持国际化、事件发布、资源管理等更高级的功能。 另外,Spring核心容器还包括了一些辅助模块,如AOP(面向切面编程)、ORM(对象关系映射)等。这些模块可以与核心容器无缝集成,提供更强大、更灵活的功能。例如,通过AOP,我们可以在不修改原有代码的情况下,为应用程序添加事务、日志等横切关注点。而通过ORM,我们可以方便地将Java对象映射到数据库中的表。 总结来说,Spring核心容器是Spring框架最为重要的组成部分,它提供了BeanFactory和ApplicationContext两个接口,用于管理和组织应用程序中的组件。同时,它还包括了一些辅助模块,如AOP和ORM,以提供更多的功能支持。使用Spring核心容器,我们可以简化应用程序的开发、配置和管理,提高代码的可重用性和可维护性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值