Spring學習日記04_IOC_Bean管理_xml配置
阿新 • • 發佈:2021-07-26
工廠Bean
- Spring裡面有兩種型別bean
- 普通bean:在配置檔案中定義bean型別就是返回型別
- 工廠bean(FactoryBean):在配置檔案定義bean型別可以和返回型別不一樣
- 工廠bean
- 第一步 建立類,讓該類作為工廠bean,實現介面FactoryBean
- 第二步 實現接口裡面的方法,在實現的方法中定義返回的bean型別
Class
public class MyBean implements FactoryBean<Course> { //定義返回bean public Course getObject() throws Exception { Course course = new Course(); course.setCname("course"); return course; } public Class<?> getObjectType() { return null; } public boolean isSingleton() { return false; } } public void test4(){ ApplicationContext context = new ClassPathXmlApplicationContext("bean7.xml"); Course course = context.getBean("myBean",Course.class); System.out.println(course); }
xml
<bean id="myBean" class="Spring.Ioc.Day07.factorybean.MyBean">
</bean>
bean的作用域
- 在Spring裡面,設定建立bean例項是單例項還是多例項
- 在Spring裡面,預設情況下,bean是單例項物件
- 如何設定單例項與多例項
- 在spring配置檔案bean標籤裡面有屬性(scope)用於設定單例項還是多例項
- scope屬性值
- prototype
- singleton
<bean id="book" class="Spring.Ioc.Day05.collection.book" scope="prototype"> <property name="list" ref="bookList"></property> </bean>
- singleton和prototype區別
- singleton單例項,prototype多例項
- 設定scope值是singleton時,載入spring配置檔案時就會建立單例項物件
- 當scope值是prototype時,在呼叫getBean方法時才建立多例項物件
bean的生命週期
- 生命週期
- 從物件建立到物件銷燬的過程
-
bean的生命週期
(1)通過構造器建立bean例項(無引數構造)
(2)為bean的屬性設定值和對其他bean的引用(呼叫set方法)
(3)呼叫bean的初始化的方法(需要進行配置)
(4)bean可以使用了(物件獲取到了)
(5)當容器關閉時,呼叫bean的銷燬方法(需要進行配置銷燬的方法) -
演示bean的生命週期
class
public class Orders {
//無參構造
public Orders() {
System.out.println("第一步 執行無引數構造建立bean例項");
}
private String oname;
public void setOname(String oname) {
this.oname = oname;
System.out.println("第二步 呼叫set方法設定屬性值");
}
//建立執行的初始化方法
public void initMethod(){
System.out.println("第三部 執行初始化方法");
}
public void destoryMethod(){
System.out.println("第五步 執行銷燬方法");
}
}
xml
<bean id="orders" class="Spring.Ioc.Day11.bean.Orders" init-method="initMethod" destroy-method="destoryMethod">
<property name="oname" value="orderName"></property>
</bean>
test
public void testBean(){
// ApplicationContext context = new ClassPathXmlApplicationContext("bean8.xml");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean8.xml");
Orders orders = context.getBean("orders",Orders.class);
System.out.println("第四步 獲取建立bean例項物件");
System.out.println(orders);
context.close();
}
-
bean的後置處理器,bean的七步宣告週期
(1)通過構造器建立bean例項(無引數構造)
(2)為bean的屬性設定值和對其他bean的引用(呼叫set方法)
(3)把bean例項傳遞bean後置處理器方法
(4)呼叫bean的初始化的方法(需要進行配置)
(5)把bean例項傳遞bean後置處理器方法
(6)bean可以使用了(物件獲取到了)
(7)當容器關閉時,呼叫bean的銷燬方法(需要進行配置銷燬的方法) -
演示新增後置處理器效果
- 建立類,實現介面BeanPostProcessor,建立後置處理器
class
public class BeanPost implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("在初始化之前執行的方法");
return bean;
}
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("在初始化之後執行的方法");
return bean;
}
}
xml
</bean>
<!--為所有bean新增配置處理器-->
<bean id="beanPost" class="Spring.Ioc.Day11.bean.BeanPost"></bean>
xml自動裝配
- 根據指定裝配規則,Spring自動將匹配的屬性值進行注入
autowire="byName"
- 按屬性名稱注入,bean的id值要和類的屬性名稱相同
autowire="byType"
- 按屬性型別注入,相同型別的bean不能定義多個
xml
<bean id="emp" class="Spring.Ioc.Day25.autowire.Emp" autowire="byName">
<!--手動裝配
<property name="dept" ref="dept"></property>
-->
</bean>
<bean id="dept" class="Spring.Ioc.Day25.autowire.Dept"></bean>
引入外部屬性檔案
- 直接配置資料庫資訊
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/forest_blog?serverTimezone=UTC"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
- 引入外部屬性檔案配置資料庫連線池
- 建立外部屬性檔案,properties格式檔案
jdbc.properties
prop.driverClass =com.mysql.jdbc.Driver
prop.url = jdbc:mysql://127.0.0.1:3306/forest_blog?serverTimezone=UTC
prop.userName = root
prop.password = 123456
- 把外部檔案引入到Spring配置檔案中
-
引入名稱空間
xmlns:context="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
-
在spring配置檔案中使用標籤引入外部屬性檔案
-
<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>