文件结构可以参考上一节(使用工具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 >
		<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.");
	}
}

  

  

相关文章:

  • 2021-10-15
  • 2022-03-01
  • 2021-11-12
  • 2021-09-24
  • 2021-08-23
  • 2022-12-23
  • 2021-11-20
  • 2022-03-03
猜你喜欢
  • 2021-08-22
  • 2021-12-20
  • 2021-04-16
  • 2021-10-22
  • 2021-06-18
  • 2021-08-01
  • 2022-12-23
相关资源
相似解决方案