您的位置:首页 > 编程语言 > Java开发

Spring 国际化支持 消息传递 以及 AOP 面向方面编程

2016-07-27 19:22 761 查看
国际化支持

照书上的流程下来.

设置config.xml
<?xml version="1.0" encoding="UTF8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org.dtd.spring-beans.dtd">
<beans>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename">
<value>messages</value>
</property>
</bean>
<bean id="HelloWorld" class="com.gc.action.HelloWorld">
</bean>
<bean id="date" class="java.util.Date"/>
</beans>


建messages.properties文件内容为

HelloWorld=问候语:{0} 问候时间:{1}

如果放书上说的src位置,我这边会报错,我是放在WEB-INF的classes文件夹下.

修改test文件
ApplicationContext actx = new FileSystemXmlApplicationContext("config.xml");
Object[] objs = new Object[]{"HelloWorld",Calendar.getInstance().getTime()};
String msg = actx.getMessage("HelloWorld",objs,Locale.CHINA);
System.out.println(msg);
运行后输出会有乱码出现.

照书上进行转码,native2ascii命令转码.因为在c盘所以cmd 要用管理员身份打开不然会拒绝访问.

然后可以正常输出

问候语:HelloWorld 问候时间:16-7-27 上午9:50

再新建一个messages_en_US.properties 内容为

HelloWorld=language:{0} time:{1}

修改getMessage的Locale.CHAINA为Locale.US

输出language:HelloWorld time:7/27/16 9:53 AM

这就实现了修改资源文件,而不用修改代码部分,就可以完成多语言的输出了.

资源访问

第一种,虚拟路径  把文件放在CLASSPATH下

ApplicationContext actx = new FileSystemXmlApplicationContext("config.xml");

Resource rs = actx.getResource("classpath:messages.properties");

第二种 标准URL

Resource rs = actx.getResource("file:C:\\workspaceandorid\\myApp\\WEB-INF\\classes\\messages.properties");

第三种 相对路径

Resource rs = actx.getResource("WEB-INF\\classes\\messages.properties");

消息传递

首先写个监听来获得消息,并处理.
public class LogListener implements ApplicationListener{

public LogListener() {
// TODO Auto-generated constructor stub
}

public void onApplicationEvent(ApplicationEvent arg0) {
// TODO Auto-generated method stub
if(arg0 instanceof LogEvent){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
format.setLenient(false);
String currentDateString = format.format(new Date());
System.out.println("时间"+currentDateString + "内容" + arg0.toString());
}
}
}


然后写个存放消息的类
public class LogEvent extends ApplicationEvent {
public LogEvent(Object source) {
super(source);
}
}


这个类负责消息的发送
public class Log implements ApplicationContextAware{
private  ApplicationContext applicationContext;

public void setApplicationContext(ApplicationContext applicationContext)throws BeansException{
this.applicationContext = applicationContext;
}

public int log(String log) {
LogEvent event = new LogEvent(log);
this.applicationContext.publishEvent(event);
return 0;
}
}


测试程序如下
public class TestHelloWorld {
public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException, IOException {
ApplicationContext actx = new FileSystemXmlApplicationContext("config.xml");
Log log = (Log)actx.getBean("log");
log.log("sdf");
}
}


不小心关了note,好多写好的没保存下来.第五章就简单记录一点吧

第五章 AOP

AOP的消息发送机制是通过实现InvocationHandle的接口,bind接口后,可以在invoke方法内发出在bind的接口的方法调用前后写日志.

实现代码如下
public class TimeBook implements TimeBookInterface{
public void doAuditing(String name) {
System.out.println("真正在处理的过程");
}
}
public interface TimeBookInterface {
public void doAuditing(String name);
}
public class LogProxy implements InvocationHandler{

private Logger logger = Logger.getLogger(this.getClass().getName());
private Object delegate;

//绑定代理对象
public Object bind(Object delegate) {
this.delegate = delegate;
return Proxy.newProxyInstance(delegate.getClass().getClassLoader(), delegate.getClass().getInterfaces(), this);
}
//针对接口编程
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Object result = null;
try {
logger.log(Level.INFO, args[0] + " 开始审核");
result = method.invoke(delegate, args);
logger.log(Level.INFO, args[0] + " 结束审核");
} catch (Exception e) {
logger.log(Level.INFO, e.toString());
}
return result;
}
}
public class TestHelloWorld {
public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException, IOException {
LogProxy logProxy = new LogProxy();
TimeBookInterface timbookProxyBookInterface = (TimeBookInterface)logProxy.bind(new TimeBook());
timbookProxyBookInterface.doAuditing("老王");
}
}


运行结果:

[INFO ]2016-07-27 14:07:25com.gc.action.LogProxy-老王 开始审核

真正在处理的过程

[INFO ]2016-07-27 14:07:25com.gc.action.LogProxy-老王 结束审核

切入点pointcut 是join point的集合

通知advice 是输出日志信息的代码

advisor 是将advice注入到pointcut位置的代码.

spring的三种切入点

静态切入点

只限给定的方法和目标类
<bean id="settersAndAbsquatulatePointcut"
class="org.springframework.aop.support.RegexpMethodPointcut">
<property name="patterns">
<!--设定切入点-->
<list>
<value>.*save.*</value>
<value>.*do.*</value>
</list>
</property>
</bean>


动态切入点

除了给定的方法和目标类,还可以指定参数,功能多,但是性能损耗也大.

大多是用静态切入点.

自定义切入点

写书当时AOP未成熟,所以作者没写,以后有机会学习. 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: