Spring学习——IoC 容器

     Spring IoC(Inversion of Control,控制反转) GitHub demo源码

       Spring 容器是 Spring 框架的核心。容器将创建对象、配置对象、操作对象、管理对象、连接对象,控制对象从创建到销毁的整个生命周期。

       Spring容器包括ApplicationContext容器BeanFactory容器

//ApplicationContext 
package com.spring.example.hello;

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

public class SpringMainContext {
	public static void main(String[] args){
		
		ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); //获取beans.xml配置文件
		
		HelloWorldBean bean = (HelloWorldBean) context.getBean("helloWorld");//通过id获取bean对象
		
		//bean.setMessage("Hi,Hello Spring");
		System.out.println(bean.getMessage());
	}
}
//BeanFactory 
package com.spring.example.hello;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;

public class SpringMainFactory {
	public static void main(String[] args){
		
		//BeanFactory  factory = new XmlBeanFactory(new ClassPathResource("Beans.xml")); //XmlBeanFactory方法过期
		BeanFactory factory = new ClassPathXmlApplicationContext("Beans.xml");//获取beans.xml配置文件
		HelloWorldBean bean = (HelloWorldBean) factory.getBean("helloWorld");//通过id获取bean对象
		
		//bean.setMessage("Hi,Hello Spring");
		System.out.println(bean.getMessage());
	}
}

beans.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-3.0.xsd">
	
	<!-- 声明一个bean对象,id是唯一识别,class是目标路径 -->
   	<bean id="helloWorld" class="com.spring.example.hello.HelloWorldBean" init-method="init" destroy-method="destroy">
   		<!-- 属性配置,name是bean对象中的属性,value是给该属性赋值 -->
       	<property name="message" value="Hello World!"/>
   	</bean>

</beans>

bean对象

package com.spring.example.hello;

public class HelloWorldBean {
	private String message;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	 public void init(){
		 System.out.println("Bean-init.");
	 }
	 public void destroy(){
		 System.out.println("Bean-destroy");
	 }
}

容器将创建对象,把它们连接在一起,配置它们,并管理他们的整个生命周期从创建到销毁。
Spring 容器使用依赖注入(DI)来管理组成一个应用程序的组件。这些对象被称为 Spring Beans

1、主程序启动入口
    1>通过读取beans.xml文件,获取生成工厂 bean
    
    2>获取beans.xml中注册的【目标对象】
    
    3>由于beans.xml已经对【目标对象】的部分属性进行赋值操作,可以直接获取该赋值
    
2、beans.xml 文件
    一个bean对应一个对象,id是唯一标志,class是类的包路径
        property对应属性,name是属性名,value是属性值
  

  <bean id="" class=""> 
        <property name="" value=""/>
    </bean>


3、beans.xml配置
    beans.xml
  

  <!-- 基础bean -->
       <bean id="..." class="...">
           <property name="..." value="..."/>
       </bean>
   
       <!-- 延迟初始化 -->
       <bean id="..." class="..." lazy-init="true">
          
       </bean>

       <!-- 初始化方法 -->
       <bean id="..." class="..." init-method="...">
          
       </bean>

   <!-- 销毁方法 -->
   <bean id="..." class="..." destroy-method="...">
      
   </bean>


   
4、生命周期
    init-method -> method -> destroy-method
   

猜你喜欢

转载自blog.csdn.net/qq_34819372/article/details/82385224