zoukankan      html  css  js  c++  java
  • Spring(0)

    0) Spring的helloWorld

    https://blog.csdn.net/cflys/article/details/70598903

    当我们没有使用Spring的时候,调用sayHello()方法需要3步:
        1.创建一个HelloWorld的实例对象
        2.设置实例对象的name属性
        3.调用对象的sayHello()方法


        使用Spring需要3步:(已经配置好Spring的xml配置文件)
        1.创建一个Spring的IOC容器对象:    ApplicationContext context=new ClassPathXmlApplicationContext("spring-config.xml");

          ApplicationContext代表IOC容器 ,是一个接口;ClassPathXMLApplicationContent是它的一个实现类。


        2.从IOC容器中获取Bean实例          HelloWorld helloWorld=(HelloWorld) context.getBean("helloWorld");

          有多个获取bean的方式;


        3.调用sayHello()方法                       helloWorld.sayHello();

    Spring做了什么?

    创建实例对象,配置对象的属性。

    1)Spring是什么?

    Spring是一个IOC(DI)和AOP容器的框架 --------控制反转,依赖注入,面向切面编程

    IOC(Inversion of Control)----控制反转,反转资源获取的方向

    DI(Dependency Injection)----依赖注入,IOC的另一种表达方式

    如何配置Bean属性:

      1) 配置关于对象

      2) list

      3) map

      4) 集合bean

      5) p标签

    自动装配Bean

    Bean之间的关系(继承,依赖):继承Bean配置,父Bean可以设置为抽象Bean   (abstract="true")

    Bean的生命周期:在Bean声明里设置init-method 和 destroy-method 方法,指定类的某个方法为初始化方法销毁方法init-meethod=“init1”   // init 为类里边的方法

               Bean的后置处理器:配置Bean的那个方法实现BeanPostProcessor接口(配置的bean不需要给ID,IOC容器会自动识别是一个后置处理器),并实现两个方法,一个在init-method之前          调用,一个在之后调用。会处理所有的Bean,所以要处理某个类型的Bean时,要进行过滤。

    AOP面向切面编程:

       Aspect:切面:基础知识:https://blog.csdn.net/u013782203/article/details/51799427

      注解:

        @pointcut:切入点 表达式

        @order:优先级

        @before:前置通知

    使用AspectJ框架:https://blog.csdn.net/gavin_john/article/details/80156963

              http://baimoz.me/1057/

    AspectJ基础实战:https://www.cnblogs.com/Lemonades/p/11050278.html

    AspectJ遇到的坑:

    报错信息:

    Exception in thread "main" java.lang.ClassCastException: class com.sun.proxy.$Proxy9 cannot be cast to class SpringAOP2.AtithmeticCalculatorImpl

    解决方法1:在AOP里面设置proxy-target-class="true"属性

            <aop:aspectj-autoproxy proxy-target-class="true">
            </aop:aspectj-autoproxy>
    

    解决方法2:报这个错,只有一个原因,就是转化的类型不对.

    接口过父类的子类,在强制转换的时候,一定要用接口父类来定义。!!

            //由于AtithmeticCalculatorImpl实现了AtithmeticCalculator接口,所以强制转换必须用父类AtithmeticCalculator来定义
            AtithmeticCalculator atithmeticCalculator=(AtithmeticCalculator)context.getBean("atithmeticCalculatorImpl");
    
  • 相关阅读:
    [BZOJ3257]树的难题
    [BZOJ4987]Tree
    [NOI2015][洛谷P2150]寿司晚宴
    P2221 [HAOI2012]高速公路
    BUG全集(我遇到的)
    NOIP2018游记
    BZOJ1103
    Google Chrome 优化
    特殊空格
    Ant Design Vue 使用
  • 原文地址:https://www.cnblogs.com/Lemonades/p/10970090.html
Copyright © 2011-2022 走看看