javascript
Spring IOC学习
文章目錄
- SpringIOC
- 常用applicationContext實(shí)現(xiàn)類
- 一、ClassPathXmlApplicationContext
- 二、AnnotationConfigApplicationContext
- Spring中的Java類
- 三、XmlWebApplicationContext
- Application初始化路徑
- 路徑前綴
- 路徑中的通配符
- applicationContext.xml配置文件
- 管理bean
- 1.在xml文件中配置開啟注解
- 2.在一個(gè)類中直接添加注解使這個(gè)類成為一個(gè)配置文件也就是成為
- Bean的作用域
- Bean的生命周期
- 在xml中開啟Spring注解配置支持
- 導(dǎo)入其他spring配置xml文件
- 引入外部的properties文件
- 將外部properties文件中的屬性注入到bean中
- p,c命名空間
- 通過p命名空間來注入屬性(通過setter方法)
- 通過c命名空間來注入構(gòu)造參數(shù)
- 注入集合、Map類型參數(shù)
- 通過對象工廠方法實(shí)例化
- 在非web應(yīng)用中優(yōu)雅的關(guān)閉SpringIOC容器
- 懶加載
- 什么是懶加載
- 實(shí)現(xiàn)方式
- 懶加載的優(yōu)缺點(diǎn)
- 默認(rèn)懶加載
SpringIOC
SpringIOC(Inversion of Control)是一個(gè)容器,也就是我們通常所說的控制反轉(zhuǎn)。 IOC容器將我們的javabean和一些用于描述這些bean應(yīng)該如何初始化、組裝的配置信息進(jìn)行整合。提供給我們開發(fā)人員一個(gè)配置、組裝完成的上下文給我們使用,我們可以方便的通過IOC容器將繁雜的對象創(chuàng)建、管理工作托管給IOC容器。所以稱之為控制反轉(zhuǎn)。(由原來的開發(fā)人員自己控制對象的創(chuàng)建、組裝改為交由Spring IOC容器負(fù)責(zé))
常用applicationContext實(shí)現(xiàn)類
ClassPathXmlApplicationContext:類路徑加載
AnnotationConfigApplicationContext:用于基于注解的配置
XmlWebApplicationContext:用于在Web工程中初始化SpringIOC容器
一、ClassPathXmlApplicationContext
用于加載類路徑下的spring配置文件,通常用于控制臺(tái)程序
使用方法:
ApplicationContext ctx = new ClassPathXmlApplicationContext(“applicationContext.xml”);
StudentService ss = ctx.getBean(StudentService.class);
System.out.println(ss);
二、AnnotationConfigApplicationContext
用于初始化通過注解方式配置的ioc容器
Spring中的Java類
@Configuration
public class Student {
private String name;
}
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(Studebt.class);
三、XmlWebApplicationContext
XmlWebApplicatoinContext用于在Web工程中初始化SpringIOC容器,不過我們一般不會(huì)手動(dòng)通過它來初始化IOC容器,Spring針對Web工程專門給我們提供了一個(gè)監(jiān)聽器來完成IOC容器的初始化工作,用法如下:
在項(xiàng)目的web.xml中配置
contextConfigLocation /WEB-INF/daoContext.xml /WEB-INF/applicationContext.xml org.springframework.web.context.ContextLoaderListener在ContextLoaderListener中Spring會(huì)優(yōu)先判斷用戶是否在web.xml中配置了名為contextClass的參數(shù),如果配置了優(yōu)先使用用戶制定的ApplicationContext實(shí)現(xiàn)類來初始化IOC,反之則使用默認(rèn)的ApplicationContext實(shí)現(xiàn)類:org.springframework.web.context.support.XmlWebApplicationContext來完成IOC容器的初始化工作。
Application初始化路徑
路徑前綴
// 前綴classpath:表示的是項(xiàng)目的classpath下相對路徑
ApplicationContext ctx = new ClassPathXmlApplicationContext(“classpath:applicationContext.xml”);
路徑中的通配符
// 使用通配符加載所有符合要求的文件
ApplicationContext ctx = new ClassPathXmlApplicationContext(“classpath*:applicationContext.xml”);
applicationContext.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>管理bean
一.通過xml配置來管理Bean 我們可以在xml文件中配置:
優(yōu)點(diǎn):對代碼沒有侵入性,改變了配置不用再重新編譯與打包 我們可以通過bean的id或者name屬性來獲取此bean,class則是初始化這個(gè)路徑的這個(gè)類
二.Annotation配置方式(注解方式) 使用注解的方式有兩種:
1.在xml文件中配置開啟注解
context:annotation-config/
2.在一個(gè)類中直接添加注解使這個(gè)類成為一個(gè)配置文件也就是成為
@Configuration
@ComponentScan(“com.john.spring”) @Configuration這個(gè)注解就是為了讓這個(gè)類成為xml配置文件
@ComponentScan(“com.john.spring”)這個(gè)注解是開啟掃描注解同時(shí)指定一個(gè)掃描的根 路徑
優(yōu)點(diǎn):配置簡單
缺點(diǎn):對代碼有侵入性,如果注解的配置改變了就得重新編譯與打包
Bean的作用域
bean的作用域:
作用域?qū)傩?#xff1a;scope(主要講解了單例與非單例的描述)
a.單例的定義就是默認(rèn)的無需強(qiáng)調(diào) b.非單例兩種方式來定義,
一種在xml文件中的中將scope的屬性定義為 prototype,
另一種就是在注解中添加@scope中定義prototype這兩種方式來定義非單例
單例只能初始化一次,非單例是每實(shí)例化一次就創(chuàng)建一個(gè)新的對象
Bean的生命周期
通過xml中給bean配置init-method、destroy-method屬性
在類中添加init與destroy方法,
在xml文件中通過bean綁定這個(gè)類
然后使用init-method、 destroy-method這兩種屬性獲取類中的方法,
在xml中開啟Spring注解配置支持
<context:annotation-config /> 開啟Spring注解配置支持<context:component-scan base-package="com.lanou3g.spring" /> 配置注解掃描包路徑 配置了掃描包路徑后就無需再配置上面那行了導(dǎo)入其他spring配置xml文件
<import resource="classpath:message_beans.xml" />引入外部的properties文件
方式一:<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="location" value="文件名" /></bean>方式二:<context:property-placeholder location="文件名"/>將外部properties文件中的屬性注入到bean中
<bean id="" class="文件路徑"> 例:com.lanou3g.spring.bean.JDBCConf<property name="" value="" /> 注入外部文件中的屬性值</bean>bean的name屬性
name屬性也是用于給bean起名,getBean的時(shí)候使用,與id屬性不同的是,
name屬性的值可以有多個(gè)且不能與其他id和name重復(fù)
p,c命名空間
要想使用p and c 命名空間,必須要在xml文件頭部加相應(yīng)的p、c schema, 還要有context通過p命名空間來注入屬性(通過setter方法)
在文件頭加上 xmlns:p="http://www.springframework.org/schema/p"原始寫法:<bean id="" class="文件路徑" ="" ><property name="" value="" /></bean>-->p命名空間:<bean id="" class="文件路徑" ="" p :name="value"> p相當(dāng)于上面的<property />通過c命名空間來注入構(gòu)造參數(shù)
在文件頭加上 xmlns:c="http://www.springframework.org/schema/c"原始寫法:<bean id="" class="文件路徑"><constructor-arg name="" value="" /></bean>c命名空間:<bean id="" class="文件路徑" c: name="value"/>注入集合、Map類型參數(shù)
<bean id="" class="文件路徑"><property name="屬性名"><list><value> 值 </value><value type=類型(例java.lang.Integer)">int類型數(shù)字</value> ***注入不同類型</list></property><property name="屬性名"><map><entry key="" value=""></entry><entry key=""><null /> *******注入null空值</entry></map></property><property name="屬性名"><null /> ******注入null 空集合</property><property name="屬性名"><bean class="文件路徑" /> ******注入一個(gè)匿名內(nèi)部bean</property><property name="文件名" value="" /> ## 通過靜態(tài)工廠方法實(shí)例化public class People{
private static People people = null;
private String pname;
private People() {}
private People(String pname) {
this.pname = pname;
}
這種方式適合需要讓Spring管理自己實(shí)現(xiàn)的單例類,用的很少。因?yàn)橥ㄟ^Spring IOC容器我們只需配置一下scope="singleton"就可以實(shí)現(xiàn)單例了。
通過對象工廠方法實(shí)例化
<!-- 如果工廠方法需要參數(shù),通過此標(biāo)簽傳參 --> <!-- <constructor-arg name="cname" value="TestService" /> -->public class DefaultServiceLocator {
private static ClientService clientService = new ClientServiceImpl();private static AccountService accountService = new AccountServiceImpl();public ClientService createClientServiceInstance() {return clientService; }public AccountService createAccountServiceInstance() {return accountService; }}
這種方式用的也不多,只有特定場合才會(huì)用到
在非web應(yīng)用中優(yōu)雅的關(guān)閉SpringIOC容器
我們可以通過registerShutdownHook()實(shí)現(xiàn)在JVM停止的同時(shí)優(yōu)雅的關(guān)閉IOC容器
ctx.registerShutdownHook();
懶加載
什么是懶加載
ioc中的bean默認(rèn)在ioc容器啟動(dòng)時(shí)初始化加載配置文件時(shí),而不是調(diào)用getBean()時(shí))
我們在spring容器啟動(dòng)的時(shí)候先不把所有的bean都加載到spring容器中,而是在需要用的時(shí)候,才把這個(gè)對象實(shí)例化到容器中
實(shí)現(xiàn)方式
用lazy-init。告訴spring容器是否以懶加載的方式創(chuàng)造對象。用的時(shí)候才加載構(gòu)造,不用的時(shí)候不加載
懶加載的優(yōu)缺點(diǎn)
不用時(shí)不加載,用到才會(huì)加載,節(jié)省空間、內(nèi)存。 init-lazy=“true”,
默認(rèn)懶加載
在頭部設(shè)置default-lazy-init="true"非單例scope=“prototype”(因?yàn)檎{(diào)用一次創(chuàng)建一個(gè)實(shí)例,所以相當(dāng)于懶加載)
總結(jié)
以上是生活随笔為你收集整理的Spring IOC学习的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 豆瓣链接打开之后总是跳转首页
- 下一篇: gradle idea java ssm