Bean的XML文件
可扩展标记语言(标准通用标记语言的子集)是一种简单的数据存储语言。
<?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:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
<!--为命名空间指定schema文件 -->
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/context/spring-aop-3.0.xsd">
<!--默认命名空间的配置 -->
<bean id="exerciseService" class="com.hellowin.b2c.user.service.ExerciseServiceImpl"/>
<!--自定义命名空间的配置 -->
<!-- 使用Annotation自动注册Bean,解决事物失效问题:在主容器中不扫描@Controller注解,在SpringMvc中只扫描@Controller注解。 -->
<context:component-scan base-package="com"/>
<!-- provider's application name, used for tracing dependency relationship -->
<dubbo:application name="spa-provider"/>
<!-- use multicast registry center to export service -->
<!--<dubbo:registry address="zookeeper://127.0.0.1:2181"/>-->
<dubbo:registry protocol="zookeeper" address="${zookeeper.host}" />
<!-- use dubbo protocol to export service on port 20880 -->
<dubbo:protocol name="dubbo" port="20860"/>
</beans>
我们在上面的代码中定义了三个命名空间:
- 首先我们定义了一个默认命名空间,他没有空间名,用于Spring Bean的定义。
- 接下来我们命名了一个xsi命名空间,这个命名空间用于为每个文档中命名空间指定相对应的schema的样式文件。是标准组织定义的标准命名空间。
- 我们还命名了一个aop的命名空间,这个命名空间是Spring配置aop的命名空间,是用户自定义的命名空间。
命名空间的定义分为了两个步骤:
- 指定命名空间的名称,需要指定命名空间的缩类名和全名
- 指定命名空间的schema文档样式文件的位置,用空格或回车行来进行分割。
指定命名空间schema地址有两个用途:
- xml解析器可以获取schema文件,并对文档进行格式合法性验证
- 在开发环境下,IDE可以用schema文件来对文档编辑器进行诱导功能。
Spring3.0 的配置Schema文件分布在各模块类包中,如果模块拥有对应的Schema文件,则可以在模块类包中找到一个config目录,Schema文件就位于该目录中,如下是对这些Schema文件的用途进行了简单说明:
示例说明:Spring-beans-3.0.xsd
命名空间:http://www.springframework.org/schema/beans
Schema 文件:http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
可以看出在Spring3.0当中,所有的Schema文件的命名空间以及对应的位置都和Beans这个Schema文件是类似的。
那么接下来来了解以下Spring当中其他Schema文件的用途:
- spring-beans-3.0.xsd:Spring3.0最主要的配置文件,主要是用于配置Bean
- spring-aop-3.0.xsd:aop配置定义的
- schema spring-tx-3.0.xsd:声明式事物配置定义的Schema
- spring-mvc-3.0.xsd:Spring3.0当中新增的
- spring-util-3.0.xsd:是为简化某些复杂的标准配置而提供的Schema
- spring-jee-3.0.xsd:是为简化J2EE中EJB等功能的配置而提供的Schema
- spring-jdbc-3.0.xsd:为Spring内接数据库而提供的Schema,3.0新增
- spring-jms-3.0.xsd:jms配置的Schema
- spring-lang-3.0.xsd:增加了对动态语言的支持,为集成动态语言而定义
- spring-oxm-3.0.xsd:配置对象xml映射到schema,3.0新增
- spring-task-3.0.xsd:任务调度的Schema
- spring-tool-3.0.xsd:为集成Schema一些有用工具而提供的Schema
BeanDefinition继承了AttributeAccessor,说明它具有处理属性的能力;
BeanDefinition继承了BeanMetadataElement,说明它可以持有Bean元数据元素,作用是可以持有XML文件的一个bean标签对应的Object。
Spring加载上下文
1:spring-mvc.xml中用import引入其他的配置文件
<import resource="user_spring.xml" />
2:在web.xml配置,应用服务去加载
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml,/WEB-INF/user_spring*.xml</param-value>
</context-param>
3:引用资源用XmlBeanFactory(不能实现多个文件相互引用)
Resource resource = new ClassPathResource("appcontext.xml");
XmlBeanFactory在3.1以后已经被废弃,不再推荐使用 可以使用父类DefaultListableBeanFactory
BeanFactory factory = new XmlBeanFactory(resource);
4:引用应用上下文用ClassPathXmlApplicationContext
ApplicationContext factory=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
ApplicationContext factory=new ClassPathXmlApplicationContext("conf/userConfig.xml");
ApplicationContext factory=new ClassPathXmlApplicationContext("file:G:/Test/src/appcontext.xml");
5:用文件系统的路径引用应用上下文用FileSystemXmlApplicationContext
ApplicationContext factory=new FileSystemXmlApplicationContext("src/applicationContext.xml");
ApplicationContext factory=new FileSystemXmlApplicationContext("classpath:appcontext.xml");
ApplicationContext factory=new FileSystemXmlApplicationContext("file:G:/Test/src/appcontext.xml");
ApplicationContext factory=new FileSystemXmlApplicationContext("G:/Test/src/appcontext.xml");
6:Web工程定制的加载方法 XmlWebApplicationContext
ServletContext servletContext = request.getSession().getServletContext();
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext );
SpringBean作用域
在Spring 容器当中,一共提供了5种作用域类型,在配置文件中,通过属性scope来设置bean的作用域范围。
1. singleton:
<bean id="userInfo" class="cn.lovepi.UserInfo" scope="singleton"></bean>
当Bean的作用域为singleton的时候,Spring容器中只会存在一个共享的Bean实例,所有对Bean的请求只要id与bean的定义相匹配,则只会返回bean的同一实例。单一实例会被存储在单例缓存中,为Spring的缺省作用域。
2. prototype:
<bean id="userInfo" class="cn.lovepi.UserInfo" scope=" prototype "></bean>
每次对该Bean请求的时候,Spring IoC都会创建一个新的作用域。
对于有状态的Bean应该使用prototype,对于无状态的Bean则使用singleton
3. request:
<bean id="userInfo" class="cn.lovepi.UserInfo" scope=" request "></bean>
Request作用域针对的是每次的Http请求,Spring容器会根据相关的Bean的
定义来创建一个全新的Bean实例。而且该Bean只在当前request内是有效的。
4. session:
<bean id="userInfo" class="cn.lovepi.UserInfo" scope=" session "></bean>
针对http session起作用,Spring容器会根据该Bean的定义来创建一个全新的Bean的实例。而且该Bean只在当前http session内是有效的。
5. global session:
<bean id="userInfo" class="cn.lovepi.UserInfo"scope=“globalSession"></bean>
类似标准的http session作用域,不过仅仅在基于portlet的web应用当中才有意义。Portlet规范定义了全局的Session的概念。他被所有构成某个portlet外部应用中的各种不同的portlet所共享。在global session作用域中所定义的bean被限定于全局的portlet session的生命周期范围之内。