ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
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