• spring容器ioc和di


    spring ioc 容器的创建

    BeanFactory 接口提供了一种高级配置机制,能够管理任何类型的对象,它是SpringIoC容器标准化超接口!
    ApplicationContextBeanFactory 的子接口。它扩展了以下功能:

    • 更容易与 Spring 的 AOP 功能集成
    • 消息资源处理(用于国际化)
    • 特定于应用程序给予此接口实现,例如Web 应用程序的 WebApplicationContext

    ApplicationContext容器实现类:

    ClassPathXmlApplicationContext通过读取类路径下的 XML 格式的配置文件创建 IOC 容器对象
    FileSystemXmlApplicationContext通过文件系统路径读取 XML 格式的配置文件创建 IOC 容器对象
    AnnotationConfigApplicationContext通过读取Java配置类创建 IOC 容器对象
    WebApplicationContext专门为 Web 应用准备,基于 Web 环境创建 IOC 容器对象,并将对象引入存入 ServletContext 域中。

    spring框架的配置方式:xml配置,注解,java配置类三种方式。

    其中xml配置 无参构造函数,有参构造函数,静态工厂,非静态工厂4种配置方式。

    其中有参构造函数
    尚硅谷spring笔记
    中周期方法(创造和销毁)以及作用域(单例和多例)

    周期方法
    <beans>
      <bean id="beanOne" class="examples.BeanOne" init-method="init" />
      <bean id="beanTwo" class="examples.BeanTwo" destroy-method="cleanup" />
    beans>
    
    
    
    
    <bean id="happyMachine8" scope="prototype" class="com.atguigu.ioc.HappyMachine">
        <property name="machineName" value="happyMachine"/>
    bean>
    
    <bean id="happyComponent8" scope="singleton" class="com.atguigu.ioc.HappyComponent">
        <property name="componentName" value="happyComponent"/>
    bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    基于配置类的方式管理bean

    注解说明
    @Component该注解用于描述 Spring 中的 Bean,它是一个泛化的概念,仅仅表示容器中的一个组件(Bean),并且可以作用在应用的任何层次,例如 Service 层、Dao 层等。 使用时只需将该注解标注在相应类上即可。
    @Repository该注解用于将数据访问层(Dao 层)的类标识为 Spring 中的 Bean,其功能与 @Component 相同。
    @Service该注解通常作用在业务层(Service 层),用于将业务层的类标识为 Spring 中的 Bean,其功能与 @Component 相同。
    @Controller该注解通常作用在控制层(如SpringMVC 的 Controller),用于将控制层的类标识为 Spring 中的 Bean,其功能与 @Component 相同。

    配置文件来确定扫描范围

    
    <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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
        
        
        <context:component-scan base-package="com.atguigu.components"/>
      
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    组件BeanName问题

    默认情况:

    类名首字母小写就是 bean 的 id。例如:SoldierController 类对应的 bean 的 id 就是 soldierController。
    使用value属性指定:

    @Controller(value = "tianDog")
    public class SoldierController {
    }
    
    • 1
    • 2
    • 3

    当注解中只设置一个属性时,value属性的属性名可以省略
    作用域的配置。自动装配@Autowired,不需要有set方法!
    给属性复制

    
    <context:property-placeholder location="application.properties" />
    
    • 1
    • 2
    package com.atguigu.components;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    
    /**
     * projectName: com.atguigu.components
     *
     * description: 普通的组件
     */
    @Component
    public class CommonComponent {
    
        /**
         * 情况1: ${key} 取外部配置key对应的值!
         * 情况2: ${key:defaultValue} 没有key,可以给与默认值
         */
        @Value("${catalog:hahaha}")
        private String name;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    配置类

    配置类实现上述场景,多个配置类使用import导入,加载时只需要加载一次就行。

    AOP切面编程

    动态代理技术分类

    • JDK动态代理:JDK原生的实现方式,需要被代理的目标类必须实现接口!他会根据目标类的接口动态生成一个代理对象!代理对象和目标对象有相同的接口!(拜把子)
    • cglib:通过继承被代理的目标类实现代理,所以不需要目标类实现接口!(认干爹)
    @Configuration
    @ComponentScan(basePackages = "com.atguigu")
    
    @EnableAspectJAutoProxy//作用等于在xml中配置  配置类上开启 Aspectj注解支持!
    public class MyConfig {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    spring事务处理

    在这里插入图片描述
    需要做两件事:
    1.选择合适的一个事务管理器实现类加入到IOC容器中
    2.指定业务类的那些方法需要添加事务。

    事务属性的设置

    只读,超时间,事务异常(那些异常回滚,那些异常不回滚),事务隔离级别,事务传播行为。

  • 相关阅读:
    咸达医药数据库--介绍
    贼简单的Android计时工具,老铁,还不试用起来。
    K8s和Docker
    Redis-使用java代码操作Redis
    java计算机毕业设计springboot+vue地铁站自动售票系统-火车票售票系统
    python中的小问题01--使用cmd命令行中的pip3下载的包(模块)在pycharm中无法获取
    Pytorch实现波阻抗反演
    局部线性嵌入LLE算法--学习笔记
    VS2019+QT5.15调用动态库dll带有命名空间
    js 正则表达式大全
  • 原文地址:https://blog.csdn.net/xunzhaofupo/article/details/133770874