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. 附錄