1、IOC 操作 Bean 管理(FactoryBean)
1匿级、Spring 有兩種類型 bean空镜,一種普通 bean,另外一種工廠 bean(FactoryBean)
2咳焚、普通 bean:在配置文件中定義 bean 類型就是返回類型
3洽损、工廠 bean:在配置文件定義 bean 類型可以和返回類型不一樣 第一步 創(chuàng)建類漫贞,讓這個(gè)類作為工廠 bean潮太,實(shí)現(xiàn)接口 FactoryBean 第二步 實(shí)現(xiàn)接口里面的方法尔苦,在實(shí)現(xiàn)的方法中定義返回的 bean 類型
public class MyBean implements FactoryBean<Course> {
? ? //定義返回bean
? ? @Override
? ? public Course getObject() throws Exception {
? ? ? ? Course course = new Course();
? ? ? ? course.setCname("abc");
? ? ? ? return course;
? ?}
}
<bean id="myBean" class="com.atguigu.spring5.factorybean.MyBean">
</bean>
@Test
public void test3() {
????ApplicationContext context =
????????new ClassPathXmlApplicationContext("bean3.xml");
????Course course = context.getBean("myBean", Course.class);//返回值類型可以不是定義的bean類型署惯!
????System.out.println(course);
}
2、IOC 操作 Bean 管理(bean 作用域)
在 Spring 里面延刘,默認(rèn)情況下旅掂,bean 是單實(shí)例對(duì)象,下面進(jìn)行作用域設(shè)置:
(1)在 spring 配置文件 bean 標(biāo)簽里面有屬性(scope)用于設(shè)置單實(shí)例還是多實(shí)例
(2)scope 屬性值 第一個(gè)值 默認(rèn)值访娶,singleton商虐,表示是單實(shí)例對(duì)象 第二個(gè)值 prototype,表示是多實(shí)例對(duì)象
<bean id="book" class="com.atguigu.spring5.collectiontype.Book" scope="prototype"><!--設(shè)置為多實(shí)例-->?
? ? ? ?<property name="list" ref="bookList"></property>
</bean>
(3)singleton 和 prototype 區(qū)別
? ? ? ? a)singleton 單實(shí)例崖疤,prototype 多實(shí)例
? ? ? ? b)設(shè)置 scope 值是 singleton 時(shí)候秘车,加載 spring 配置文件時(shí)候就會(huì)創(chuàng)建單實(shí)例對(duì)象 ;設(shè)置 scope 值是 prototype 時(shí)候劫哼,不是在加載 spring 配置文件時(shí)候創(chuàng)建對(duì)象叮趴,在調(diào)用 getBean 方法時(shí)候創(chuàng)建多實(shí)例對(duì)象
3、IOC 操作 Bean 管理(bean 生命周期)
1权烧、生命周期 :從對(duì)象創(chuàng)建到對(duì)象銷毀的過(guò)程
2眯亦、bean 生命周期
(1)通過(guò)構(gòu)造器創(chuàng)建 bean 實(shí)例(無(wú)參數(shù)構(gòu)造)
(2)為 bean 的屬性設(shè)置值和對(duì)其他 bean 引用(調(diào)用 set 方法)
(3)調(diào)用 bean 的初始化的方法(需要進(jìn)行配置初始化的方法)
(4)bean 可以使用了(對(duì)象獲取到了)
(5)當(dāng)容器關(guān)閉時(shí)候,調(diào)用 bean 的銷毀的方法(需要進(jìn)行配置銷毀的方法)
3般码、演示 bean 生命周期 :
? ? ? ? public class Orders {
? ? ? ? ?//無(wú)參數(shù)構(gòu)造
? ? ? ? ?public Orders() {
? ? ? ? ?System.out.println("第一步 執(zhí)行無(wú)參數(shù)構(gòu)造創(chuàng)建 bean 實(shí)例");
? ? ? ? ?}
? ? ? ? ?private String oname;
? ? ? ? ?public void setOname(String oname) {
? ? ? ? ?this.oname = oname;
? ? ? ? ?System.out.println("第二步 調(diào)用 set 方法設(shè)置屬性值");
? ? ? ? ?}
? ? ? ? ?//創(chuàng)建執(zhí)行的初始化的方法
? ? ? ? ?public void initMethod() {
? ? ? ? ?System.out.println("第三步 執(zhí)行初始化的方法");
? ? ? ? ?}
? ? ? ? ?//創(chuàng)建執(zhí)行的銷毀的方法
? ? ? ? ?public void destroyMethod() {
? ? ? ? ?System.out.println("第五步 執(zhí)行銷毀的方法");
? ? ? ? ?}
? ? ? ? }
public class MyBeanPost implements BeanPostProcessor {//創(chuàng)建后置處理器實(shí)現(xiàn)類
? ? @Override
? ? public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
? ? ? ? System.out.println("在初始化之前執(zhí)行的方法");
? ? ? ? return bean;
? ? }
? ? @Override
? ? public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
? ? ? ? System.out.println("在初始化之后執(zhí)行的方法");
? ? ? ? return bean;
? ? }
}
<!--配置文件的bean參數(shù)配置-->
<bean id="orders" class="com.atguigu.spring5.bean.Orders" init-method="initMethod" destroy-method="destroyMethod"> <!--配置初始化方法和銷毀方法-->
? ? <property name="oname" value="手機(jī)"></property><!--這里就是通過(guò)set方式(注入屬性)賦值-->
</bean><!--配置后置處理器-->
<bean id="myBeanPost" class="com.atguigu.spring5.bean.MyBeanPost"></bean>
@Test
public void testBean3() {
// ApplicationContext context =
// new ClassPathXmlApplicationContext("bean4.xml");
ClassPathXmlApplicationContext context =
????????new ClassPathXmlApplicationContext("bean4.xml");
Orders orders = context.getBean("orders", Orders.class);
System.out.println("第四步 獲取創(chuàng)建 bean 實(shí)例對(duì)象");
System.out.println(orders);
//手動(dòng)讓 bean 實(shí)例銷毀
context.close();
}
4妻率、bean 的后置處理器,bean 生命周期有七步 (正常生命周期為五步板祝,而配置后置處理器后為七步)
(1)通過(guò)構(gòu)造器創(chuàng)建 bean 實(shí)例(無(wú)參數(shù)構(gòu)造)
(2)為 bean 的屬性設(shè)置值和對(duì)其他 bean 引用(調(diào)用 set 方法)
(3)把 bean 實(shí)例傳遞 bean 后置處理器的方法 postProcessBeforeInitialization
(4)調(diào)用 bean 的初始化的方法(需要進(jìn)行配置初始化的方法)
(5)把 bean 實(shí)例傳遞 bean 后置處理器的方法 postProcessAfterInitialization
(6)bean 可以使用了(對(duì)象獲取到了)
(7)當(dāng)容器關(guān)閉時(shí)候宫静,調(diào)用 bean 的銷毀的方法(需要進(jìn)行配置銷毀的方法)
4、IOC 操作 Bean 管理(外部屬性文件)
方式一:直接配置數(shù)據(jù)庫(kù)信息 :(1)配置Druid(德魯伊)連接池 (2)引入Druid(德魯伊)連接池依賴 jar 包
<!--直接配置連接池-->
? ? <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
? ? ? ? <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
? ? ? ? <property name="url" value="jdbc:mysql://localhost:3306/userDb"></property>
? ? ? ? <property name="username" value="root"></property>
? ? ? ? <property name="password" value="root"></property>
? ?</bean>
方式二:引入外部屬性文件配置數(shù)據(jù)庫(kù)連接池
(1)創(chuàng)建外部屬性文件券时,properties 格式文件孤里,寫(xiě)數(shù)據(jù)庫(kù)信息(jdbc.properties)
? ? prop.driverClass=com.mysql.jdbc.Driver
? ? prop.url=jdbc:mysql://localhost:3306/userDb
? ? prop.userName=root
? ? prop.password=root
(2)把外部 properties 屬性文件引入到 spring 配置文件中 —— 引入 context 名稱空間
<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
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? http://www.springframework.org/schema/context/spring-context.xsd"><!--引入context名稱空間-->? ? ? ? <!--引入外部屬性文件-->
? ? <context:property-placeholder location="classpath:jdbc.properties"/>? ? <!--配置連接池-->
? ? <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
? ? ? ? <property name="driverClassName" value="${prop.driverClass}"></property>
? ? ? ? <property name="url" value="${prop.url}"></property>
? ? ? ? <property name="username" value="${prop.userName}"></property>
? ? ? ? <property name="password" value="${prop.password}"></property>
? ? </bean></beans>
版權(quán)聲明:本文為CSDN博主「來(lái)點(diǎn)淦貨」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議橘洞,轉(zhuǎn)載請(qǐng)附上原文出處鏈接及本聲明捌袜。原文鏈接:https://blog.csdn.net/weixin_45496190/article/details/107067200