Spring学习 Ioc篇(一 )
一直以来忙于项目的开发,Spring虽然不用,一直想系统地学习一下,想看看它的源码,都没有时间,这段时间比较充裕,就索性先把Spring学习下,熟悉各个功能再去探究它内部的实现。就从Ioc篇开始学习。
1、实例化spring容器的两种方法
方法一:在类路径下寻找配置文件来实现Spring容器的实例化
- ApplicationContext cx= new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
方法二:在文件系统路径下寻找配置文件进行Spring容器的实例化
- ApplicationContext cx= new FileSystemXmlApplicationContext(new String[]{"D:\\applicationContext.xml"});
在实际的应用中建议使用第一种方式,在程序开发中比较灵活,与路径无关。同时,sping可以指定多个配置文件,由数组传入。
2、实例化bean的三种方式
方法一:使用类构造器实例化
- //接口
- package com.cvicse.service;
- public interface PersonService {
- public void save();
- }
- //实现类
- package com.cvicse.service.impl;
- import com.cvicse.service.PersonService;
- public class PersonServiceBean implements PersonService {
- public void save(){
- System.out.println("我是save()方法");
- }
- }
在applicationContext.xml的XML中配置如下:
- <bean id="personService" class="com.cvicse.service.impl.PersonServiceBean"></bean>
在应用类中调用
- ApplicationContext cx= new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
- PersonService personService = (PersonService)cx.getBean("personService");
- personService.save();
方法二:使用静态工厂方法实例化
在方法一的基础上添加一个静态工厂方法,专门实例化bean
- package com.cvicse.service.impl;
- public class PersonServiceBeanFactory {
- public static PersonServiceBean createPersonServiceBean(){
- return new PersonServiceBean();
- }
- }
在applicationContext.xml的XML中配置如下:
- <bean id="personService" class="com.cvicse.service.impl.PersonServiceBeanFactory"
- factory-method="createPersonServiceBean"/>
在应用类中调用
- ApplicationContext cx= new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
- PersonService personService = (PersonService)cx.getBean("personService");
- personService.save();
方法三:使用实例工厂方法实例化
在方法一的基础上添加一个工厂方法类,用于bean的实例化
- package com.cvicse.service.impl;
- public class PersonServiceBeanFactory {
- public PersonServiceBean createPersonServiceBean(){
- return new PersonServiceBean();
- }
- }
在applicationContext.xml的XML中配置如下:
- <bean id="personServiceFactory" class="com.cvicse.service.impl.PersonServiceBeanFactory"/>
- <bean id="personService" factory-bean="personServiceFactory" factory-method="createPersonServiceBean"/>
在应用类中调用
- ApplicationContext cx= new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
- PersonService personService = (PersonService)cx.getBean("personService");
- personService.save();
3、bean的作用域范围
(1) singleton:默认的情况下在容器启动只有一个bean的实例化对象,在容器实例化的时候初始化。如果添加了lazy-init=“true”则会延迟加载。
(2) prototype:每次调用都会初始化一个新的实例对象
- <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" scope="prototype"/>
(3) request
(4) session
(5)global session
4、spring容器管理bean的生命周期
bean默认的情况下是在sping容器实例化的时候被创建,在spring容器关闭之前被销毁。
在配置文件中可以为每个bean指定初始化方法和销毁方法
- <strong> <bean id="personService"
- class="cn.com.impl.PersonServiceBean"
- init-method="initMethod" destroy-method="destroyMethod"></bean></strong>
相关文章
- Spring Boot JPA中关联表的使用
- spring学习之springMVC 返回类型选择 以及 SpringMVC中model,modelMap.request,session取值顺序
- Spring MVC学习------------WebApplicationContext
- [转]ssh中如何实现定时任务(spring对quartz的支持)
- Spring 拦截器实现+后台原理(HandlerInterceptor)
- SpringMvc学习-1-Spring MVC了解
- SpringMvc学习-3-Spring MVC 数据绑定
- Spring Boot 使用 Micrometer 集成 Prometheus 监控 Java 应用性能
- spring mvc 学习01 搭建
- spring-boot-configuration-processor用法举例
- spring接口文档注解:@ApiOperation
- Spring Security 实战干货:使用 JWT 认证访问接口
- spring mvc环境之validator表单验证(十一)
- Spring之事务操作(配置文件)
- Spring之注入复杂类型属性
- Spring Security JWT学习 (七)
- 【转】Spring学习---为什么要用spring,springMVC
- 【Spring】HelloSpring:使用IntelliJ IDEA学习Spring
- 不能吧,最后一次它又来了--整合Mybatis和事务管理(“最易懂得Spring学习”)
- Java学习之路-Spring的HttpInvoker学习
- 【spring框架】spring框架学习目录
- Spring MVC入门——day02
- 最近无意中看到一个讲解spring mvc的系列,从源码的角度讲解,特记录下来,供以后反复学习
- 基于Spring Boot和Spring Cloud实现微服务架构学习--转
- 曹工说Spring Boot源码(9)-- Spring解析xml文件,到底从中得到了什么(context命名空间上)
- Spring学习笔记(一)---注入方式
- Spring学习笔记(一)---概念入门
- Spring Boot2 跨域问题