Spring 知识点整理 之 IOC

    xiaoxiao2021-03-25  111

    核心点

    IOCAOP

    有什么好处

    IOC 自动管理 对象创建 和 对象之间的依赖管理,不用再new对象,也不用再set对象。就像使用Maven、gradle管理工程一样。AOP 可以轻松的控制对象的各个生命周期,扩展、监控、拦截对象方法 声明式事务JUNIT 单元测试的集成整合其他框架。

    IOC

    核心内容 BenasCoreContextExpressLanguage

    spring 中的工厂

    ApplicationContextBeanFactory区别BeanFactory在getBean时创建对象,ApplicationContext 加载完配置文件就创建了Bean

    XML方式的Bean配置

    使用无参构造函数 <bean id="bean1" class="com.itheima.spring.demo2.Bean1"></bean> 静态工厂 public class Bean2Factory { public static Bean2 getBean2Instance(){ return new Bean2(); } } <bean id="bean2" class="com.itheima.spring.demo2.Bean2Factory" factory-method="getBean2Instance"/> 实例工厂 public class Bean3Factory { public Bean3 getBean3Instance(){ return new Bean3(); } } <!-- 实例工厂实例化 --> <bean id="bean3Factory" class="com.itheima.spring.demo2.Bean3Factory"></bean> <bean id="bean3" factory-bean="bean3Factory" factory-method="getBean3Instance"></bean>

    bean 配置

    idnameclassfactory-beanfactory-method

    id与name

    id用来标识bean,是唯一的,且只有一个;name定义的是bean的alias,可以有多个,并可能与其他的bean重名。

    scope

    singleton (单例 默认)prototype(多实例,每个线程对应一个实例)requestsessionglobalsession

    ~bean 的生命周期(按顺序)~(不作为参考)

    ~周期~ 1.instantiate bean对象实例化2.populate properties 封装属性3.如果Bean实现BeanNameAware 执行 setBeanName4.如果Bean实现BeanFactoryAware 或者 ApplicationContextAware 注入工厂 setBeanFactory 或者上下文对象 setApplicationContext5.如果存在类实现 BeanPostProcessor(后处理Bean) ,执行postProcessBeforeInitialization6.如果Bean实现InitializingBean 执行 afterPropertiesSet 7.调用 指定初始化方法 init8.如果存在类实现 BeanPostProcessor(处理Bean) ,执行postProcessAfterInitialization9.执行业务处理10.如果Bean实现 DisposableBean 执行 destroy11.调用 指定销毁方法 customerDestroy

    ~解释~

    第一步 可以理解为执行构造函数,或工厂方法。第二步 依赖注入。第三步 设置名字第四步使对象可以通过Spring容器获取其他对象第五步在bean属性装配完成之后执行一些操作(没有看出跟第六步的区别)第六步在bean属性装配完成之后执行一些操作第七步执行bean自带的init方法。第八步bean初始化完成后再执行一些操作


    Bean的生命周期

    参考 Chandler Qian的博客 传送门

    重点理解后处理器的执行时间

    postProcessBeforeInitialization 属性注入完毕之后InitializingBean接口的afterPropertiesSet()之前(如果有) init-method之前(如果有)postProcessAfterInitialization init-method之后(如果有)

    后处理器是面向容器的,对该容器内的所有bean生效


    依赖注入

    三种方式 构造方法 <bean id="car" class="com.itheima.spring.demo5.Car"> <!-- 构造方法的方式注入 --> <!-- <constructor-arg name="name" value="宝马"/> <constructor-arg name="price" value="500000"/> --> <constructor-arg index="0" type="java.lang.String" value="奔驰"/> <constructor-arg index="1" value="800000"/> </bean> set注入 对于基本类型在property标签中统一使用"",Spring自动做类型转换。 <bean id="car2" class="com.itheima.spring.demo5.Car2"> <property name="name" value="奇瑞QQ"/> <property name="price" value="35000"/> </bean> p命名空间(了解) * 引入p名称空间(2.5版本之后): <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> * 使用p名称空间注入: * 语法格式: * p:属性名=”” * p:属性名-ref=”” <bean id="car2" class="com.itheima.spring.demo5.Car2" p:name="长安奔奔" p:price="40000"/> <bean id="employee" class="com.itheima.spring.demo5.Employee" p:name="隔壁老王" p:car2-ref="car2"></bean> SpEL(Spring Expression Language)注入方式: <!-- SpEL属性注入(3.0版本之后) --> <bean id="car2" class="com.itheima.spring.demo5.Car2" > <property name="name" value="#{'法拉利'}"/> <property name="price" value="#{1200000}"/> </bean> <bean id="employee" class="com.itheima.spring.demo5.Employee"> <property name="name" value="#{'豆豆'}"/> <property name="car2" value="#{car2}"/> </bean> <bean id="carInfo" class="com.itheima.spring.demo5.CarInfo"></bean> <bean id="car2" class="com.itheima.spring.demo5.Car2" > <property name="name" value="#{carInfo.carName}"/> <property name="price" value="#{carInfo.calculatorPrice()}"/> </bean> 注入集合对象 <bean id="collectionBean" class="com.itheima.spring.demo6.CollectionBean"> <property name="arrs" > <list> <value>老王</value> <value>凤姐</value> <value>如花</value> </list> </property> <property name="list"> <list> <value>豆豆</value> <value>奶茶</value> <value>绿茶</value> </list> </property> <property name="set"> <set> <value>王尧</value> <value>刘健</value> <value>周玉</value> </set> </property> <property name="map"> <map> <entry key="老王2" value="38"/> <entry key="凤姐" value="38"/> <entry key="如花" value="29"/> </map> </property> <property name="properties"> <props> <prop key="username">root</prop> <prop key="password">123</prop> </props> </property> </bean>

    注解方式的IOC

    配置 * 开启Spring的组件扫描的配置: <!-- Spring的注解开发:组件扫描(类上注解: 可以直接使用属性注入的注解) --> <context:component-scan base-package="com.demo1"/> 注解 @Component(value="userService") 相当于<bean id=”userService” class=”...”/>@Controller 控制层对象@Service 业务层对象@Repository 持久层对象

    属性注入

    @Value@Autowired 默认按类型进行装配.按名称注入:@Qualifier:强制使用名称注入@Resource @Resource(name="baseDao")

    其他注解

    @PostConstruct :相当于init-method@PreDestroy :相当于destroy-method

    以JavaBean作为配置文件的方式的开发(所谓的零配置文件方式,spring boot使用此方式):

    @Configuration public class BeanConfig { @Bean(name="car") public Car showCar(){ Car car = new Car(); car.setName("兰博基尼"); car.setPrice(2000000d); return car; } @Bean(name="product") public Product showProduct(){ Product product = new Product(); product.setName("苹果6sp"); product.setPrice(8500d); return product; } }

    Spring整合Junit单元测试

    @RunWith(SpringJUnit4ClassRunner.class) //使用junit4进行测试 @ContextConfiguration(value = {"classpath*:/applicationContext-resources-test.xml"}) public class CreditReportManagerTest { }
    转载请注明原文地址: https://ju.6miu.com/read-39104.html

    最新回复(0)