zoukankan      html  css  js  c++  java
  • Spring 学习总结二 Bean的生命周期

    文件结构可以参考上一节(使用工具MyEclipse)

    Bean的生命周期有方法有:init-method,destroy-method

    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"
    	xmlns:p="http://www.springframework.org/schema/p"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
    	<bean id="helloWorld" class="bu.example.com.HelloWorld"  init-method="init" destroy-method="destroy">
    		<property name="message" value="Hello World!!!" />
    	</bean>
    </beans>
    

      

    MainApp.java 文件

    public static void main(String[] args) {
    		AbstractApplicationContext context = 
    				new ClassPathXmlApplicationContext("applicationContext.xml");
    		HelloWorld obj = (HelloWorld)context.getBean("helloWorld");
    		obj.getMessage();
    		context.registerShutdownHook();
    
    	}
    

    HelloWorld.java

    public class HelloWorld {
    
    	private String message;
    
    	public void setMessage(String message) {
    		this.message = message;
    	}
    
    	public void getMessage() {
    		System.out.println("Your Message : " + message);
    	}
    
    	public void init() {
    		System.out.println("Bean is going through init.");
    	}
    
    	public void destroy() {
    		System.out.println("Bean will destroy now.");
    	}
    }
    

      

      

  • 相关阅读:
    过拟合问题详解
    C++数据结构原理和经典问题求解--绪论
    centos系统 anaconda3(python3)安装pygrib
    pycharm激活教程
    如何查看电脑是几核几线程(网传方法有错误)
    深度学习过程
    VS2010 编译 boost thread库
    windows多线程编程
    matplotlib画条形图
    matplotlib画折线图,并以时间作为横轴
  • 原文地址:https://www.cnblogs.com/linlf03/p/5177164.html
Copyright © 2011-2022 走看看