org.springframework.beans.factory包提供了管理和控制bean的基本功能,包括使用编程的方式。org.springframework.context包的里面的ApplicationContext接口, 继承了BeanFactory 接口。
1.15.1 使用MessageSource国际化
String getMessage(String code, Object[] args, String default, Locale loc)
String getMessage(String code, Object[] args, Locale loc)
String getMessage(MessageSourceResolvable resolvable, Locale locale)
Spring提供了两种MessageSource 的实现, ResourceBundleMessageSource 和 StaticMessageSource 。 两个都继承自HierarchicalMessageSource 。
Resource 使用方式:
<beans>
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>format</value>
<value>exceptions</value>
<value>windows</value>
</list>
</property>
</bean>
</beans>
文件:
# in format.properties
m
message=Alligators rock!
# in exceptions.properties
a
argument.required=The {0} argument is required.
public static void main(String[] args) {
MessageSource resources = new ClassPathXmlApplicationContext("beans.xml");
String message = resources.getMessage("message", null, "Default", null);
System.out.println(message);
}
}
<?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:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/lang
http://www.springframework.org/schema/lang/spring-lang.xsd">
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>com/oscar999/chp15/format</value>
<value>exceptions</value>
<value>windows</value>
</list>
</property>
</bean>
</beans>
message=Hello, Oscar999!
/**
* @Title: MainTest.java
* @Package com.oscar999.chp15
* @Description: TODO
* @author oscar999
* @date Sep 3, 2018 2:32:09 PM
* @version V1.0
*/
package com.oscar999.chp15;
import org.springframework.context.MessageSource;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @ClassName: MainTest
* @Description: TODO
* @author oscar999
*/
public class MainTest {
public static void main(String[] args) {
MessageSource resources = new ClassPathXmlApplicationContext("com/oscar999/chp15/bean.xml");
String message = resources.getMessage("message", null, "Default", null);
System.out.println(message);
}
}
/**
* @Title: Example.java
* @Package com.oscar999.chp15
* @Description: TODO
* @author oscar999
* @date Sep 3, 2018 2:44:07 PM
* @version V1.0
*/
package com.oscar999.chp15;
import org.springframework.context.MessageSource;
/**
* @ClassName: Example
* @Description: TODO
* @author oscar999
*/
public class Example {
private MessageSource messages;
public void setMessages(MessageSource messages) {
this.messages = messages;
}
public void execute() {
String message = this.messages.getMessage("argument.required", new Object[] { "userDao" }, "Required", null);
System.out.println(message);
}
}
<?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:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/lang
http://www.springframework.org/schema/lang/spring-lang.xsd">
<!-- this MessageSource is being used in a web application -->
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="com/oscar999/chp15/exceptions" />
</bean>
<!-- lets inject the above MessageSource into this POJO -->
<bean id="example" class="com.oscar999.chp15.Example">
<property name="messages" ref="messageSource" />
</bean>
</beans>
argument.required=The {0} argument is required.
/**
* @Title: MainTest.java
* @Package com.oscar999.chp15
* @Description: TODO
* @author oscar999
* @date Sep 3, 2018 2:32:09 PM
* @version V1.0
*/
package com.oscar999.chp15;
import org.springframework.context.ApplicationContext;
import org.springframework.context.MessageSource;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @ClassName: MainTest
* @Description: TODO
* @author oscar999
*/
public class MainTest {
public static void main(String[] args) {
MessageSource resources = new ClassPathXmlApplicationContext("com/oscar999/chp15/bean.xml");
String message = resources.getMessage("message", null, "Default", null);
System.out.println(message);
ApplicationContext ctx = new ClassPathXmlApplicationContext("com/oscar999/chp15/bean1.xml");
Example example = (Example) ctx.getBean("example");
example.execute();
}
}
国际化的话,建立如下文件:
format_en_GB.properties, exceptions_en_GB.properties, and windows_en_GB.properties
1.15.2 标准和客制事件
Spring提供了如下标准时间
-ContextRefreshedEvent
-ContextStartedEvent
-ContextStoppedEvent
-ContextClosedEvent
-RequestHandledEvent
客制事件
public class BlackListEvent extends ApplicationEvent {
private final String address;
private final String test;
public BlackListEvent(Object source, String address, String test) {
super(source);
this.address = address;
this.test = test;
}
// accessor and other methods...
}
}
public class EmailService implements ApplicationEventPublisherAware {
private List<String> blackList;
private ApplicationEventPublisher publisher;
public void setBlackList(List<String> blackList) {
this.blackList = blackList;
}
public void setApplicationEventPublisher(ApplicationEventPublisher publisher) {
this.publisher = publisher;
}
public void sendEmail(String address, String text) {
if (blackList.contains(address)) {
BlackListEvent event = new BlackListEvent(this, address, text);
publisher.publishEvent(event);
return;
}
// send email...
}
}
}
1.15.3 方便访问低层级资源
使用Reousrce访问。
1.15.4 方便的web程式ApplicationContext初始化
使用 ContextLoaderListener注册应用上下文。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/daoContext.xml /WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
这个监听侦测contextConfigLocation参数, 如果不存在,则默认使用WEB-INF/applicationContext.xml。也可以使用路径匹配的方式, 类似
WEB-INF/*Context.xml, WEB-INF/**/*Context.xml
1.15.5 作为Java EE RAR 文件的方式部署Spring ApplicationContext
- 空白目录
- 0.环境准备
- 0.1基于maven的工程创建
- 1.控制反转容器
- 1.1 Spring控制反转容器和beans介绍
- 1.2 容器概览
- 1.3 Bean概览
- 1.4 依赖
- 1.5 Bean的范围
- 1.6 客制bean的特性
- 1.7 Bean定义的继承
- 1.8 容器扩展点
- 1.9 基于注解的容器配置
- 1.10 类路径扫描及组件管理
- 1.11 使用JSR 330标准的注解
- 1.12 基于Java的容器配置
- 1.12.1 基本概念: @Bean 和 @Configuration
- 1.13 环境抽象化
- 1.14 注册一个LoadTimeWeaver
- 1.15 ApplicationContext的附加功能
- 1.16 BeanFactory
- 2. 资源
- 3. 验证,数据绑定和类型转换
- 4. Spring表达式语言(SpEL)
- 5. Spring面向方面的切面编程
- 6. Spring AOP 接口
- 7. 空安全
- 8. 数据缓冲和编码
- 9. 附录