1、Spring容器的启动流程
2、代码如下
com.itheima.spring.initdestroy.helloworld.java
package com.itheima.spring.initdestroy;public class helloworld {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;System.out.println("set函数");}public helloworld(){System.out.println("构造函数");}public void init(){System.out.println("init");}public void destroy() {System.out.println("destroy");}public void hello() {System.out.println("你好");}}
com.itheima.spring.initdestroy.test.initdestroyTest.java
package com.itheima.spring.initdestroy.test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.itheima.spring.initdestroy.helloworld;public class initdestroyTest {@Testpublic void test() {ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml");helloworld helloworld1 = (helloworld)context.getBean("helloworld");helloworld1.hello();//Spring 容器关闭ClassPathXmlApplicationContext applicationContext = (ClassPathXmlApplicationContext)context;applicationContext.close();}}
applicationContext.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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><!--初始化方法:在构造函数执行之后,执行初始化方法init-method销毁方法:在spring容器关闭时,自动执行销毁方法destroy-method--><bean id="helloworld" class="com.itheima.spring.initdestroy.helloworld"init-method="init" destroy-method="destroy"></bean></beans>
运行结果:
构造函数set函数init你好destroy
(附件:有关Spring IOC的代码)
版权声明:本文为qq_31863071原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。