Spring

    科技2022-07-14  145

    在Spring中实现控制反转的是Ioc容器,其实现方式是依赖注入。

    依赖注入的方式: 1、构造器 2、set注入 3、接口注入(注解注入)

    常用依赖

    项目基本依赖

    <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.0.RELEASE</version> </dependency>

    设置java compiler

    <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>14</source> <target>14</target> </configuration> </plugin> </plugins> </build>

    基本约束

    基本约束

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> </beans>

    注解约束

    <?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" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> </beans>

    aop约束

    <?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.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> </beans>

    注解说明

    @Autowired:spring的注解(最常用) 如果Autowired不能唯一自动装配上属性,则需要通过@Quantify(value=“xxx”) @Resource:java的注解,先通过名字查找,再通过类型查找。 区别:

    都是自动装配的,都可以放在属性字段上@Autowired 通过类型装配@Resource默认通过byName方式实现,如果找不到,则通过类型装配

    以下注解均为创建bean实例,写在类上 @Component (要开启包下的扫描<context:component-scan base-package=“com.zhu.pojo”/>) @Service(对应mvc的service层)@Controller(对应mvc的controller层)@Repository(对应mvc的dao层)

    注解与xml

    xml更加万能, 维护简单 注解只能用在自己的类上,维护复杂 注解要开启注解支持

    <context:component-scan base-package="com.zhu.pojo"/> <context:annotation-config/>

    使用Java的方式配置Spring

    AOP

    AOP 主要用来解决:在不改变原有业务逻辑的情况下,增强横切逻辑代码,根本上解耦合,避免横切逻辑代码重复。 动态代理

    InvocationHandler:调用处理程序并返回结果Proxy.newProxyInstance() :生成动态代理的实类

    静态代理/动态代理

    动态代理:运行时增强(Spring Aop)静态代理:编译时增强 (AspectJ)

    Spring Aop:基于动态代理,如果要代理的对象实现了某个接口,Spring Aop就会使用JDK Proxy,而对于没有实现接口的对象,这时候Spring Aop会使用Cglib


    AOP原理:

         Spring的动态代理(Spring AOP):利用Java反射和Proxy.newProxyInstance()生成动态代理类的实例对象,利用InvocationHandler调用处理程序并返回结果。(在调用方法前后调用新增方法)

         JDK动态代理要求被代理的类实现一个接口,只有接口中的方法才能被代理.其方法是将被代理对象注入到一个中间对象,而中间对象实现InvocationHandler接口,在实现该接口时,可以在被代理对象调用它的方法时,在调用的前后插入一些代码。而 Proxy.newProxyInstance() 能够利用中间对象来生产代理对象。

    Processed: 0.014, SQL: 8