1. 程式人生 > >07.Spring-配置詳解-Spirng建立物件的三種方式

07.Spring-配置詳解-Spirng建立物件的三種方式

測試類

package vc.helloworld.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import vc.helloworld.bean.User;
import vc.helloworld.create.UserFactory;

public class TestDemo {
	@Test
	public void
fun1() { // 1.首先要把容器創建出來, ApplicationContext applicationContext = new ClassPathXmlApplicationContext("ApplicationContext.xml"); // 2.向容器裡面獲取到user物件 User user = (User) applicationContext.getBean("user"); // 3.列印獲取到的user物件. System.out.println(user); } // 測試ApplicationContext容器初始化就例項化了所有物件 @Test
public void fun0() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("ApplicationContext.xml"); } @Test // 空參構造的方法測試,建立方式1 public void fun2() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "vc/helloworld/create/ApplicationContext.xml"
); User user = (User) applicationContext.getBean("user"); System.err.println(user); } @Test // 2.靜態工廠建立方式 public void fun3() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "vc/helloworld/create/ApplicationContext.xml"); UserFactory user2 = (UserFactory) applicationContext.getBean("user2"); System.out.println(user2); } @Test //動態工廠的建立方式 public void fun4() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("vc/helloworld/create/ApplicationContext.xml"); UserFactory user3 = (UserFactory) applicationContext.getBean("user3"); System.out.println(user3); } }

ApplicationContext

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:c="http://www.springframework.org/schema/c"
	xmlns:cache="http://www.springframework.org/schema/cache"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:lang="http://www.springframework.org/schema/lang"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
		http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.2.xsd">

UserFactory

package vc.helloworld.create;

import vc.helloworld.bean.User;

public class UserFactory {
	public static User createUser() {
		// 在這個方法當中手動把這個物件創建出來,
		// 呼叫這個方法,建立User物件,然後再給容器管理
		// 就是不要Spring幫我們自己`建立,我們自己建立

		System.out.println("靜態工廠建立User");

		return new User();

	}
	//例項工廠建立方式
	public User createUser2() {

		System.out.println("例項工廠建立方式");

		return new User();

	}
}