Spring

Posted 柠檬茶lemon

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring相关的知识,希望对你有一定的参考价值。

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");

以上是关于Spring的主要内容,如果未能解决你的问题,请参考以下文章

Spring boot:thymeleaf 没有正确渲染片段

What's the difference between @Component, @Repository & @Service annotations in Spring?(代码片段

spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段

Spring Rest 文档。片段生成时 UTF-8 中间字节无效 [重复]

解决spring-boot启动中碰到的问题:Cannot determine embedded database driver class for database type NONE(转)(代码片段

一张图帮你记忆,Spring Boot 应用在启动阶段执行代码的几种方式