天天看點

spring(IOC)IOC

IOC

概念

通過控制反轉,對象在被建立的時候,由一個調控系統内所有對象的外界實體将其所依賴的對象的引用傳遞給它。也可以說,依賴被注入到對象中。

流程:

導包

  • 核心容器
    • spring-beans-4.0.0.RELEASE.jar
    • spring-context-4.0.0.RELEASE.jar
    • spring-core-4.0.0.RELEASE.jar
    • spring-expression-4.0.0.RELEASE.jar
  • 日志包
    • commons-logging-1.1.3.jar

寫配置:spring配置檔案中集合了spring的ioc容器的所有元件(類)

  1. 建立Person類
    package com.atguigu.bean;
    
    public class Person {
    
    	private String lastName;
    	private Integer age;
    	private String gender;
    	private String email;
    	
    	public Person() {
    		super();
    	}
    	public Person(String lastName, Integer age, String gender, String email) {
    		super();
    		this.lastName = lastName;
    		this.age = age;
    		this.gender = gender;
    		this.email = email;
    	}
    	public String getLastName() {
    		return lastName;
    	}
    	public void setLastName(String lastName) {
    		this.lastName = lastName;
    	}
    	public Integer getAge() {
    		return age;
    	}
    	public void setAge(Integer age) {
    		this.age = age;
    	}
    	public String getGender() {
    		return gender;
    	}
    	public void setGender(String gender) {
    		this.gender = gender;
    	}
    	public String getEmail() {
    		return email;
    	}
    	public void setEmail(String email) {
    		this.email = email;
    	}
    	@Override
    	public String toString() {
    		return "Person [lastName=" + lastName + ", age=" + age + ", gender=" + gender + ", email=" + email + "]";
    	}	
    	
    }
    
               
  2. 建立一個Spring Bean Configuration File(Spring中的bean配置檔案,路徑為src下的bean_ioc.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">
    
    	<!-- 注冊一個Person對象,Spring會自動建立這個Person對象  -->
    	<!--一個bean标簽可以注冊一個元件(對象、類) -->
    	<!-- class:要注冊的元件全類名 -->
    	<!-- id:這個對象的唯一辨別 -->
    	<bean id="person01" class="com.atguigu.bean.Person">
    		<!-- 使用property标簽為Person對象的屬性指派 -->
    		<!-- name="lastName":制定屬性名 -->
    		<!-- value="張三":為這個屬性指派 -->
    		<property name="lastName" value="張三"></property>
    		<property name="age" value="18"></property>
    		<property name="gender" value="男"></property>
    		<property name="email" value="[email protected]"></property>
    	</bean>
    </beans>
    
               

測試

package com.atguigu.test;

import static org.junit.Assert.*;

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

import com.atguigu.bean.Person;

import jdk.management.resource.internal.inst.SocketOutputStreamRMHooks;

public class IOCTest {

	/**
	 * 從容器中拿到這個元件
	 */
	@Test
	public void test() {
		//ApplicationContext:容器
		//目前應用的xml配置檔案在ClassPath下
		ApplicationContext context = new ClassPathXmlApplicationContext("bean_ioc.xml");
		//根據spring配置檔案得到ioc容器對象
		Person person = (Person) context.getBean("person01");
		System.out.println(person);
	}

}

           

需要注意的問題:

  1. src:源碼包開始的路徑稱為類路徑的開始,所有源碼包(source folder)裡面的東西都會被合并放在類路徑裡面
    • 類路徑:
      • java:/bin
      • web:WEB-INF/classes
  2. 導包commons-logging-1.1.3.jar(依賴)
  3. 先導包再建立配置檔案
  4. Spring容器的容器接管了标志了s的類
    spring(IOC)IOC

需要注意的細節:

  1. ApplicationContext接口:
    spring(IOC)IOC
    //ioc容器的配置檔案在類路徑下
    new ClassPathXmlApplicationContext("bean.xml");
    //ioc容器的配置檔案在磁盤磁盤路徑下
    new FileSystemXmlApplicationContext("F://bean.xml");
               
  2. 元件的建立是容器完成的,對象在容器建立完成時就已經建立了
  3. 同一個元件在ioc容器中是單執行個體的
  4. 容器中沒有元件,容器将報異常:org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named
  5. property是調用了setter方法為JavaBean的屬性指派
  6. JavaBean的屬性名是由getter/setter方法得到的,将set後面大寫字母變小寫就是屬性名(所有getter/setter都用自動生成)