一篇文章带你理解Spring框架( 二 )
从表面上看,application context如同bean factory一样具有bean定义、bean关联关系的设置,根据请求分发bean的功能 。但application context在此基础上还提供了其他的功能 。
- 提供了支持国际化的文本消息
- 统一的资源文件读取方式
- 已在监听器中注册的bean的事件
1、ClassPathXmlApplicationContext:从classpath的XML配置文件中读取上下文,并生成上下文定义 。应用程序上下文从程序环境变量中取得 。
ApplicationContext context = new ClassPathXmlApplicationContext(“bean.xml”);
2、FileSystemXmlApplicationContext :由文件系统中的XML配置文件读取上下文 。
ApplicationContext context = new FileSystemXmlApplicationContext(“bean.xml”);
3、XmlWebApplicationContext:由Web应用的XML文件读取上下文 。
6、Spring有几种配置方式?
将Spring配置到应用开发中有以下三种方式:
- 基于XML的配置
- 基于注解的配置
- 基于Java的配置
在Spring框架中,依赖和服务需要在专门的配置文件来实现,我常用的XML格式的配置文件 。这些配置文件的格式通常用<beans>开头,然后一系列的bean定义和专门的应用配置选项组成 。
SpringXML配置的主要目的时候是使所有的Spring组件都可以用xml文件的形式来进行配置 。这意味着不会出现其他的Spring配置类型(比如声明的方式或基于Java Class的配置方式)
Spring的XML配置方式是使用被Spring命名空间的所支持的一系列的XML标签来实现的 。Spring有以下主要的命名空间:context、beans、jdbc、tx、aop、mvc和aso 。
<beans><!-- JSON Support --><bean name="viewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver"/><bean name="jsonTemplate" class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"/><bean id="restTemplate" class="org.springframework.web.client.RestTemplate"/></beans>下面这个web.xml仅仅配置了DispatcherServlet,这件最简单的配置便能满足应用程序配置运行时组件的需求 。
<web-app><display-name>Archetype Created Web Application</display-name><servlet><servlet-name>spring</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>spring</servlet-name><url-pattern>/</url-pattern></servlet-mapping></web-app>8、如何用基于Java配置的方式配置Spring?
Spring对Java配置的支持是由@Configuration注解和@Bean注解来实现的 。由@Bean注解的方法将会实例化、配置和初始化一个新对象,这个对象将由Spring的IoC容器来管理 。@Bean声明所起到的作用与<bean/> 元素类似 。被@Configuration所注解的类则表示这个类的主要目的是作为bean定义的资源 。被@Configuration声明的类可以通过在同一个类的内部调用@bean方法来设置嵌入bean的依赖关系 。
最简单的@Configuration 声明类请参考下面的代码:
@Configuration public class AppConfig {@Beanpublic MyService myService() {return new MyServiceImpl();} }对于上面的@Beans配置文件相同的XML配置文件如下:
<beans><bean id="myService" class="com.howtodoinjava.services.MyServiceImpl"/> </beans>上述配置方式的实例化方式如下:利用AnnotationConfigApplicationContext 类进行实例化
public static void main(String[] args) {ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);MyService myService = ctx.getBean(MyService.class);myService.doStuff(); }要使用组件组建扫描,仅需用@Configuration进行注解即可:
@Configuration @ComponentScan(basePackages = "com.howtodoinjava") public class AppConfig {... }在上面的例子中,com.acme包首先会被扫到,然后再容器内查找被@Component 声明的类,找到后将这些类按照Sring bean定义进行注册 。
如果你要在你的web应用开发中选用上述的配置的方式的话,需要用AnnotationConfigWebApplicationContext 类来读取配置文件,可以用来配置Spring的Servlet监听器ContrextLoaderListener或者Spring MVC的DispatcherServlet 。
<web-app><!-- Configure ContextLoaderListener to use AnnotationConfigWebApplicationContext instead of the default XmlWebApplicationContext --><context-param><param-name>contextClass</param-name><param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value></context-param><!-- Configuration locations must consist of one or more comma- or space-delimited fully-qualified @Configuration classes. Fully-qualified packages may also be specified for component-scanning --><context-param><param-name>contextConfigLocation</param-name><param-value>com.howtodoinjava.AppConfig</param-value></context-param><!-- Bootstrap the root application context as usual using ContextLoaderListener --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- Declare a Spring MVC DispatcherServlet as usual --><servlet><servlet-name>dispatcher</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- Configure DispatcherServlet to use AnnotationConfigWebApplicationContext instead of the default XmlWebApplicationContext --><init-param><param-name>contextClass</param-name><param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value></init-param><!-- Again, config locations must consist of one or more comma- or space-delimited and fully-qualified @Configuration classes --><init-param><param-name>contextConfigLocation</param-name><param-value>com.howtodoinjava.web.MvcConfig</param-value></init-param></servlet><!-- map all requests for /app/* to the dispatcher servlet --><servlet-mapping><servlet-name>dispatcher</servlet-name><url-pattern>/app/*</url-pattern></servlet-mapping> </web-app>
推荐阅读
- Mysql中的数据分组和子查询
- 梦见天空出现彩光 解梦梦见天空出现发光的彩色
- 梦到修牙预示什么 做梦梦到修牙是什么意思
- 亚麻凉席比较好的品牌有哪些
- 猴面包树是什么 猴面包树果实可以吃吗
- 四季沐歌太阳能报价清单
- 科勒马桶配件怎么样 科勒马桶配件价格
- 万喜燃气灶报价行情走势
- 新古典装修风格的装修特点
- 龙血树养殖方法及注意事项