1. BeanFactory
==Get bean via BeanFactory:
InputStream is = new FileInputStream("bean.xml");
XmlBeanFactory factory = new XmlBeanFactory(is);
Action action - (Action) factory.getBean("TheAction");
==Principle:
BeanFactory read class name, attributes(names / values) from bean.xml, then load bean and set via Reflection.
2. ApplicationContext
--------------------------------------------------------------------------------------------------------------------------------------
==Support Internationalization
Current Spring provide the following two MessageSource interface:
ResourceBundleMessageSource
ReloadableResourceBundleMessageSource
Note:
The posterior needn't reboot to reload messages.
eg:
Message files:
messages_zh_CN.properties:
userinfo = 当前登录用户:[ { 0 } ] 登录时间:[ { 1 } ]
Use JDK transcode tool to switch messages_zh_CN.properties:
native2ascii messages_zh_CN.properties msg.txt
Replace contents of messages_zh_CN.properties by msg.txt.
Note:
Batch messages via "Apache Ant's Native2Ascii".
messages_en_US.properties:
userinfo = Current login user: [ { 0 } ] Login time: [ { 1 } ]
Test:
ApplicationContext ctx = new FileSystemXmlApplicationContext("bean.xml");
Object[] arg = new Object[] {"aaa", Calendar.getInstance().getTime()};
String msg = ctx.getMessage("userinfo", arg);
System.out.println("Message is: " + msg);
--------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------
== Resource Visit
Source code e.g.:
ApplicationContext ctx = new FileSystemXmlApplicationContext();
Resource rs = ctx.getResource("classpath:log.properties");
try {
File file = rs.getFile();
if (file.exists()) {
System.out.println("File Name: " + file.getName()
+ "\nFile Parent Name: " + file.getParent()
+ "\nFile Path: " + file.getPath()
+ "\n=========================");
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String str;
while ((str = br.readLine()) != null) {
System.out.println(str);
}
}
} catch (IOException e) {
e.printStackTrace();
}
--------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------
==Event Broadcast
Source Code e.g.:
package spring;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class LoginAction implements ApplicationContextAware {
private ApplicationContext applicationContext;
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
}
public int login(String username, String password) {
ActionEvent event = new ActionEvent(username);
this.applicationContext.publishEvent(event);
return 0;
}
}
==========================================================
package spring;
import org.springframework.context.ApplicationEvent;
public class ActionEvent extends ApplicationEvent {
public ActionEvent(Object source) {
super(source);
}
}
==========================================================
package spring;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
public class ActionListener implements ApplicationListener {
// Handle an application event
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ActionEvent) {
System.out.println("==============" + event.toString()
+ "==============");
}
}
}
==========================================================
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<description>Spring Quick Start</description>
<bean id="loginaction" class="spring.LoginAction" />
<bean id="listener" class="spring.ActionListener" />
</beans>
==========================================================
package spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class LoginActionTest {
/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext ctx = new FileSystemXmlApplicationContext(
"./bean.xml");
LoginAction action = (LoginAction) ctx.getBean("loginaction");
action.login("Zhou Shengshuai", Shuai@123);
}
}