Talking from BeanFactory to ApplicationContext

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);
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值