Bean的四種實例化方式(也可以說是三種)
bean的實例化方式:
①.構造器實例化(無參數構造器,與構造器的訪問權限無關),最標准,使用最多。
②.靜態工廠方法實例化(了解)
③.實例工廠方法實例化(了解)
④.實現FactoryBean接口實例化:實例工廠變種:集成其他框架使用:SqlSessionFactoryBean
1、構造器實例化
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("App.xml") //不寫名字默認為App-context.xml public class App { @Autowired private ApplicationContext context; @Test public void testOld(){ Teacher teacher1 = new Teacher(); Teacher teacher2 = new Teacher(); System.out.println(teacher1); System.out.println(teacher2); } @Test public void testSpring(){ //用來檢驗scope是singleton或prototype的不同 Teacher teacher1 = context.getBean(Teacher.class); Teacher teacher2 = context.getBean(Teacher.class); System.out.println(teacher1); System.out.println(teacher2); } }
<?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="teacher" class="com.practice.test_04.constructor.Teacher" scope="singleton"/> </beans>
public class Teacher { public Teacher(){ System.out.println("======構造函數執行了======="); } }
知識點補充:
scope作用域

2、靜態工廠方法實例化
public class Teacher2 { public Teacher2(){ System.out.println("======構造函數執行了======="); } }
public class Teacher2Factory { public static Teacher2 getTeacher2(){ System.out.println("====靜態工廠執行了====="); return new Teacher2(); } }
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("App.xml") //不寫名字默認為App-context.xml public class App { @Autowired private ApplicationContext context; @Test public void testOld(){ Teacher2 teacher2 = new Teacher2(); System.out.println(teacher2); } @Test public void testSpring(){ Teacher2 teacher2 = context.getBean(Teacher2.class); System.out.println(teacher2); } }
<?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="teacher2" class="com.practice.test_04.staticfactory.Teacher2Factory" factory-method="getTeacher2"/> </beans>
3、實例工廠方法實例化
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("App.xml") //不寫名字默認為App-context.xml public class App { @Autowired private ApplicationContext context; @Test public void testOld(){ Teacher3 teacher3 = new Teacher3(); System.out.println(teacher3); } @Test public void testSpring(){ Teacher3 teacher3 = context.getBean(Teacher3.class); System.out.println(teacher3); } }
public class Teacher3 { public Teacher3(){ System.out.println("======構造函數執行了======="); } }
public class Teacher3Factory { public Teacher3 getObject(){ System.out.println("====實例化工廠執行了====="); return new Teacher3(); } }
<?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="teacher3Factory" class="com.practice.test_04.instancefactory.Teacher3Factory"/> <bean id="teacher" factory-bean="teacher3Factory" factory-method="getObject"/> </beans>
4、實現FactoryBean接口實例化
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("App.xml") //不寫名字默認為App-context.xml public class App { @Autowired private ApplicationContext context; @Test public void testSpring(){ Teacher4 teacher4 = context.getBean(Teacher4.class); System.out.println(teacher4); } }
public class Teacher4 { public Teacher4(){ System.out.println("======構造函數執行了======="); } }
public class Teacher4Factory implements FactoryBean<Teacher4> { public Teacher4 getObject(){ System.out.println("====實例化工廠執行了====="); return new Teacher4(); } public Class<?> getObjectType() { return Teacher4.class; } }
<?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="teacher" class="com.practice.test_04.factorybean.Teacher4Factory"/> </beans>
知識點補充:
BeanFactory和FactoryBean 的區別:
BeanFactory:Spring的容器對象,本質是一個factory,是Spring管理對象的工廠 FactoryBean:是Spring框架的一個接口,本質是一個bean,用來約束創建對象的工廠的的行為。
bean的出始化和銷毀
1、<bean id="someBean" class="......" init-method="該類中初始化方法名" destroy-method="該類中銷毀方法名"> </bean>
2、init-method:bean生命周期初始化方法,對象創建后就進行調用
3、destroy-method:容器被銷毀的時候,如果bean被容器管理,會調用該方法。
4、default-init-method,default-destroy-method.配置文件中所有的bean元素的初始化方法和銷毀方法
5、分析原理: 如果bean的scope="prototype",那么容器只負責創建和初始化,銷毀方法它並不會被spring容器管理。交給用戶自己調用。因為當bean的scope="prototype"時,Spring容器在啟動時,並不會將創建出來的對象放在容器當中。而是在每次獲取對象時,都來創建一個新的對象。因此,在容器銷毀的時候,並不知道要銷毀該對象。因此就不會調用對象的銷毀方法。
6、代碼示例
public class Wow { public Wow(){ System.out.println("創建對象"); } public void doWork(){ System.out.println("開始運行"); } public void init(){ System.out.println("初始化資源"); } public void close(){ System.out.println("關閉資源"); } }
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("App.xml") public class App { @Autowired private ApplicationContext context; @Test public void testWow(){ Wow wow1 = context.getBean(Wow.class); Wow wow2 = context.getBean(Wow.class); wow1.doWork(); } }
<?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="wow" class="com.practice.test_05.Wow" init-method="init" destroy-method="close" scope="prototype"/> </beans>
