1、概述
2、BeanFactory
3、ApplicationContext
4、Bean管理
5、创建Bean
6、Aware相关接口
HelloWorld:
package com.ljb.spring;import org.springframework.beans.factory.BeanNameAware;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class HelloWorld implements BeanNameAware{ private String message; private String name; private String career; private String beanName = null; public HelloWorld() {} public HelloWorld(String name , String career) { this.name = name; this.career = career; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCareer() { return career; } public void setCareer(String career) { this.career = career; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public void print() { System.out.println(this.getMessage()); System.out.println(this.getName() + "的职业是:" + this.getCareer()); } @Override public void setBeanName(String beanName) { // TODO Auto-generated method stub this.beanName = beanName; System.out.println("回调setName方法:id is "+beanName); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloWorld obj = (HelloWorld)context.getBean("helloWorld"); obj.print(); }}
applicationContext.xml
"林冲" "军人"
执行结果(可以看出在初始化HelloWorld之前就初始化了beanName):
回调setName方法:id is helloWorld
Hello World!"林冲"的职业是:"军人"package com.ljb.spring;import org.springframework.beans.BeansException;import org.springframework.beans.factory.BeanNameAware;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;import org.springframework.context.support.ClassPathXmlApplicationContext;public class HelloWorld implements BeanNameAware,ApplicationContextAware{ private String message; private String name; private String career; private String beanName = null; private static ApplicationContext applicationContext= null; public HelloWorld() {} public HelloWorld(String name , String career) { this.name = name; this.career = career; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCareer() { return career; } public void setCareer(String career) { this.career = career; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public void print() { System.out.println(this.getMessage()); System.out.println(this.getName() + "的职业是:" + this.getCareer()); } @Override public void setBeanName(String beanName) { // TODO Auto-generated method stub System.out.println("this.beanName is "+this.beanName); this.beanName = beanName; System.out.println("回调setName方法:id is "+beanName); } @Override public void setApplicationContext(ApplicationContext context) throws BeansException { // TODO Auto-generated method stub this.applicationContext = context; System.out.println("正在创建applicationContext实例"); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); System.out.println(context==applicationContext); HelloWorld obj = (HelloWorld)context.getBean("helloWorld"); obj.print(); }}
执行结果:
回调setName方法:id is helloWorld
正在创建applicationContext实例trueHello World!"林冲"的职业是:"军人"
留个小问题:这种注入日期,获取日期并没有按配置中的格式显示日期,这是怎么回事
text:2015-06-17
format:yyyy-MM-dd
结果:Wed Jun 17 00:00:00 CST 2015