Spring的bean管理(xml方式)之Bean例項化的方式
阿新 • • 發佈:2019-01-03
1 在spring裡面通過配置檔案建立物件
2 bean例項化三種方式實現
第一種、使用類的無引數構造建立(重點)
(1) xml 檔案 bean1.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- 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">
- <!--ioc入門-->
- <bean id="user" class="com.liuyanzhao.ioc.User"></bean>
- </beans>
(2) User 類
- package com.liuyanzhao.ioc;
- public class User {
- public void add() {
- System.out.println("add..........");
- }
- }
(3)測試類 TestIOC 類
- package com.liuyanzhao.ioc;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class TestIOC {
- @Test
- public void testUser() {
- //1、載入Spring配置檔案,根據建立物件
- ApplicationContext context =
- new ClassPathXmlApplicationContext("bean1.xml"
- //2、得到配置建立的物件
- User user = (User) context.getBean("user");
- System.out.println(user);
- }
- }
第二種 、使用靜態工廠建立(瞭解)
(1)xml 檔案 bean1.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- 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">
- <!--使用靜態工廠建立物件-->
- <bean id="bean2" class="com.liuyanzhao.bean.Bean2Factory" factory-method="getBean2"></bean>
- </beans>
(2)Bean2 類
- package com.liuyanzhao.bean;
- public class Bean2 {
- public void add() {
- System.out.println("bean2.........");
- }
- }
(3)Bean2Factory 類
- package com.liuyanzhao.bean;
- public class Bean2Factory {
- //靜態方法,返回Bean2物件
- public static Bean2 getBean2() {
- return new Bean2();
- }
- }
(4) 測試類 TestIOC 類
- package com.liuyanzhao.bean;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class TestIOC {
- @Test
- public void testUser() {
- //1、載入Spring配置檔案,根據建立物件
- ApplicationContext context =
- new ClassPathXmlApplicationContext("bean1.xml");
- //2、得到配置建立的物件
- Bean2 bean2 = (Bean2) context.getBean("bean2");
- System.out.println(bean2);
- }
- }
第三種、使用例項工廠建立(瞭解)
(1)xml 檔案
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- 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">
- <!--使用例項工廠建立物件-->
- <!--建立工廠物件-->
- <bean id="bean3Factory" class="com.liuyanzhao.bean.Bean3Factory"></bean>
- <bean id="bean3" factory-bean="bean3Factory" factory-method="getBean3" ></bean>
- </beans>
(2) Bean3 類
- package com.liuyanzhao.bean;
- public class Bean3 {
- public void add() {
- System.out.println("bean3.........");
- }
- }
(3)Bean3Factory 類
- package com.liuyanzhao.bean;
- public class Bean3Factory {
- //普通方法,返回Bean3物件
- public Bean3 getBean3() {
- return new Bean3();
- }
- }
(4)測試類 TestIOC 類
- package com.liuyanzhao.bean;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class TestIOC {
- @Test
- public void testUser() {
- //1、載入Spring配置檔案,根據建立物件
- ApplicationContext context =
- new ClassPathXmlApplicationContext("bean1.xml");
- //2、得到配置建立的物件
- Bean3 bean3 = (Bean3) context.getBean("bean3");
- System.out.println(bean3);
- }
- }