applicationContext.xml文件配置小结(1)

Spring配置文件是用于指导Spring工厂进行Bean生产、依赖关系注入(装配)及Bean实例分发的"图纸"Java EE程序员必须学会并灵活应用这份"图纸"准确地表达自己的"生产意图"Spring配置文件是一个或多个标准的XML文档,applicationContext.xmlSpring的默认配置文件,当容器启动时找不到指定的配置文档时,将会尝试加载这个默认的配置文件。

    在学会动手"绘制图纸"之前,先要学会"阅读图纸",熟能生巧讲的就是这个道理,"熟读唐诗三百首,不会作诗也会吟"

           下面列举的是一份比较完整的配置文件模板,文档中各XML标签节点的基本用途也给出了详细的解释,这些XML标签节点在后续的知识点中均会用到,熟练掌握了这些XML节点及属性的用途后,为我们动手编写配置文件打下坚实的基础。

一)applicationContext.xml文件标配模板

1、一个OA系统的applicationContext.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:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">


	<!-- 自动扫描与装配bean,扫描web包,将带有注解的类纳入spring容器管理 -->
	<!-- <context:component-scan base-package="cn.itcast.oa">作用
	Spring容器初始化时,会扫描cn.itcast.oa目录下标有@Component;@Service;@Controller;@Repository
	注解的类纳入Spring容器管理
    在类上,使用以下注解,实现bean的声明:
	  @Component:泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
	  @Service 用于标注业务层组件
	  @Controller 用于标注控制层组件(如springMvc的controller,struts中的action)
	  @Repository用于标注数据访问组件,即DAO组件
			
    在类的成员变量上,使用以下注解,实现属性的自动装配
	  @Autowired :按类的类型进行装配
	  @Resource:
  1.如果同时指定了name和type,那么从Spring上下文中找到唯一匹配的bean进行装配
  2. 如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常 
  3.如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常 
  4.如果既没有指定name,又没有指定type,则自动按照byName方式进行装配;如果没有匹配,则回退为一个原始类型进行匹配,如果匹配则自动装配;
	 -->
   <context:component-scan base-package="cn.itcast.oa"></context:component-scan>


	<!-- 加载外部的properties配置文件(引入jdbc的配置文件) -->
	<context:property-placeholder location="classpath:jdbc.properties"/>


	<!-- 配置数据库连接池(c3p0)这个可以在hibernate.cfg.xml中配置 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
	
		<!-- 基本信息 :jdbc的url、驱动名、数据库名字、密码-->
		<property name="jdbcUrl" value="${jdbcUrl}"></property>
		<property name="driverClass" value="${driverClass}"></property>
		<property name="user" value="${username}"></property>
		<property name="password" value="${password}"></property>
		
		<!-- 其他配置 -->
    <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
		<property name="initialPoolSize" value="3"></property>
		<!--连接池中保留的最小连接数。Default: 3 -->
		<property name="minPoolSize" value="3"></property>
		<!--连接池中保留的最大连接数。Default: 15 -->
		<property name="maxPoolSize" value="5"></property>
		<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
		<property name="acquireIncrement" value="3"></property>
<!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
		<property name="maxStatements" value="8"></property>
<!-- maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
		<property name="maxStatementsPerConnection" value="5"></property>
		<!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
		<property name="maxIdleTime" value="1800"></property>
	</bean>
	
	<!-- 配置SessionFactory 
	(把数据源注入给session工厂)、配置映射文件将Spring与hibernate初步整合起来
	-->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
	</bean>

	<!-- 配置声明式的事务管理(采用基于注解的方式) 
	session工厂注入到事务管理器transactionManager使Spring与Hinbernate整合实现业务逻辑
	-->
	
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<tx:annotation-driven transaction-manager="transactionManager"/>
	
</beans>

2、一个网上摘录下来的applicationContext.xml文件

<?xml version="1.0" encoding="UTF-8"?>   
<beans   
xmlns="http://www.springframework.org/schema/beans"  
xmlns  si="http://www.w3.org/2001/XMLSchema-instance"  
xmlns:context="http://www.springframework.org/schema/context"  
xsi:schemaLocation="http://www.springframework.org/schema/beans   
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
http://www.springframework.org/schema/context   
http://www.springframework.org/schema/context/spring-context-2.5.xsd   ">   
<context:component-scan base-package="com.persia">   
<!-- 开启组件扫描 -->   
</context:component-scan>   
  
<context:annotation-config>   
<!--开启注解处理器-->   
</context:annotation-config>   
  
<!-- 使用注解,省去了propertity的xml配置,减少xml文件大小 -->   
<bean id="personServiceAnno"></bean>   
<bean id="personDaoBeanAnno"></bean>   
<bean id="personDaoBeanAnno2"></bean>   
  
<!-- 自动注解 -->   
<bean id="personServiceAutoInject" autowire="byName"></bean>   
<bean id="personService">   
<!-- 由spring容器去创建和维护,我们只要获取就可以了 -->   
</bean>   
  
<bean id="personService2" factory-method="createInstance" lazy-init="true"  
init-method="init"  destroy-method="destory">   
<!-- 静态工厂获取bean -->   
</bean>   
  
<bean id="fac"></bean>   
<bean id="personService3" factory-bean="fac" factory-method="createInstance" scope="prototype">   
<!-- 实例工厂获取bean,先实例化工厂再实例化bean-->   
</bean>   
<!-- ref方式注入属性 -->   
<bean id="personDao"></bean>   
<bean id="personService4">   
<property name="personDao" ref="personDao"></property>   
</bean>   
  
<!-- 内部bean方式注入 -->   
<bean id="personService5">   
<property name="personDao">   
<bean></bean>   
</property>   
<property name="name" value="persia"></property>   
<property name="age" value="21"></property>   
  
<property name="sets">   
<!-- 集合的注入 -->   
<set>   
<value>第一个</value>   
<value>第二个</value>   
<value>第三个</value>   
</set>   
</property>   
  
<property name="lists">   
<!-- 集合的注入 -->   
<list>   
<value>第一个l</value>   
<value>第二个l</value>   
<value>第三个l</value>   
</list>   
  
</property>   
  
<property name="properties">   
<props>   
<prop key="key1">value1</prop>   
<prop key="key2">value2</prop>   
<prop key="key3">value3</prop>   
</props>   
</property>   
  
<property name="map">   
<map>   
<entry key="key1" value="value-1"></entry>   
<entry key="key2" value="value-2"></entry>   
<entry key="key3" value="value-3"></entry>   
</map>   
</property>   
</bean>   
  
<bean id="personService6">   
<constructor-arg index="0" value="构造注入的name" ></constructor-arg>   
<!-- 基本类型可以不写type -->   
<constructor-arg index="1" type="com.persia.IDaoBean" ref="personDao">   
</constructor-arg>   
</bean>   
  
</beans>

     3、一个新闻管理系统的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans  
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
	<!-- 定义使用C3P0连接池的数据源 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<!-- 指定连接数据库的JDBC驱动 -->
		<property name="driverClass">
			<value>com.mysql.jdbc.Driver</value>
		</property>
		<!-- 连接数据库所用的URL -->
		<property name="jdbcUrl">
			<value>jdbc:mysql://localhost:3306/eportal?useUnicode=
				true&characterEncoding=gbk</value>
		</property>
		<!-- 连接数据库的用户名 -->
		<property name="user">
			<value>root</value>
		</property>
		<!-- 连接数据库的密码 -->
		<property name="password">
			<value>root</value>
		</property>
		<!-- 设置数据库连接池的最大连接数 -->
		<property name="maxPoolSize">
			<value>20</value>
		</property>
		<!-- 设置数据库连接池的最小连接数 -->
		<property name="minPoolSize">
			<value>2</value>
		</property>
		<!-- 设置数据库连接池的初始化连接数 -->
		<property name="initialPoolSize">
			<value>2</value>
		</property>
		<!-- 设置数据库连接池的连接的最大空闲时间,单位为秒 -->
		<property name="maxIdleTime">
			<value>20</value>
		</property>
	</bean>
	<!-- 定义Hibernate的SessionFactory -->
	<bean id="sessionFactory"
		class="org.springframework.orm.
hibernate3.LocalSessionFactoryBean">
		<!-- 依赖注入上面定义的数据源dataSource -->
		<property name="dataSource" ref="dataSource" />
		<!-- 注册Hibernate的ORM映射文件 -->
		<property name="mappingResources">
			<list>
				<value>com/eportal/ORM/News.hbm.xml</value>
				<value>com/eportal/ORM/Category.hbm.xml</value>
				<value>com/eportal/ORM/Memberlevel.hbm.xml</value>
				<value>com/eportal/ORM/Cart.hbm.xml</value>
				<value>com/eportal/ORM/Traffic.hbm.xml</value>
				<value>com/eportal/ORM/Newsrule.hbm.xml</value>
				<value>com/eportal/ORM/Merchandise.hbm.xml</value>
				<value>com/eportal/ORM/Admin.hbm.xml</value>
			</list>
		</property>
		<!-- 设置Hibernate的相关属性 -->
		<property name="hibernateProperties">
			<props>
				<!-- 设置Hibernate的数据库方言 -->
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<!-- 设置Hibernate是否在控制台输出SQL语句,开发调试阶段通常设为true -->
				<prop key="show_sql">true</prop>
				<!-- 设置Hibernate一个提交批次中的最大SQL语句数 -->
				<prop key="hibernate.jdbc.batch_size">50</prop>
				<prop key="show_sql">50</prop>
			</props>
		</property>
	</bean>
	<!--定义Hibernate的事务管理器HibernateTransactionManager -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<!-- 依赖注入上面定义的sessionFactory -->
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	<!--定义Spring的事务拦截器TransactionInterceptor -->
	<bean id="transactionInterceptor"
		class="org.springframework.transaction.interceptor.TransactionInterceptor">
		<!-- 依赖注入上面定义的事务管理器transactionManager -->
		<property name="transactionManager" ref="transactionManager" />
		<!-- 定义需要进行事务拦截的方法及所采用的事务控制类型 -->
		<property name="transactionAttributes">
			<props>
				<!-- 以browse、list、load、get及is开头的所有方法采用只读型事务控制类型 -->
				<prop key="browse*">PROPAGATION_REQUIRED,readOnly</prop>
				<prop key="list*">PROPAGATION_REQUIRED,readOnly</prop>
				<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
				<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
				<prop key="is*">PROPAGATION_REQUIRED,readOnly</prop>
				<!-- 所有方法均进行事务控制,如果当前没有事务,则新建一个事务 -->
				<prop key="*">PROPAGATION_REQUIRED</prop>
			</props>
		</property>
	</bean>
	<!-- 定义BeanNameAutoProxyCreatorf进行Spring的事务处理 -->
	<bean
		class="org.springframework.aop.framework.autoproxy.  
BeanNameAutoProxyCreator">
		<!-- 针对指定的bean自动生成业务代理 -->
		<property name="beanNames">
			<list>
				<value>adminService</value>
				<value>columnsService</value>
				<value>newsService</value>
				<value>crawlService</value>
				<value>memberLevelService</value>
			</list>
		</property>
		<!-- 这个属性为true时,表示被代理的是目标类本身而不是目标类的接口 -->
		<property name="proxyTargetClass">
			<value>true</value>
		</property>
		<!-- 依赖注入上面定义的事务拦截器transactionInterceptor -->
		<property name="interceptorNames">
			<list>
				<value>transactionInterceptor</value>
			</list>
		</property>
	</bean>
	<!-- 装配通用数据库访问类BaseDAOImpl -->
	<bean id="dao" class="com.eportal.DAO.BaseDAOImpl">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	<!-- 部署系统用户管理业务逻辑组件AdminServiceImpl -->
	<bean id="adminService" class="com.eportal.service.AdminServiceImpl">
		<property name="dao" ref="dao" />
	</bean>
	<!-- 部署新闻栏目管理业务逻辑组件ColumnsServiceImpl -->
	<bean id="columnsService" class="com.eportal.service.ColumnsServiceImpl">
		<property name="dao" ref="dao" />
	</bean>
	
	
		
</beans> 

二)ApplicationContext.xml配置文件的基础知识

1、存放位置:
1)src下面
         需要在web.xml中定义如下:
<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext.xml</param-value>
</context-param>

2)WEB-INF下面
         需要在web.xml中定义如下:
<context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>WEB-INF/applicationContext*.xml</param-value>
</context-param>

 

2、web.xml 通过contextConfigLocation配置spring 的方式 
        SSI框架配置文件路径问题:

struts2的 1个+N个   路径:src+src(可配置)      名称: struts.xml  + N 
spring 的 1个           路径: src                          名称: applicationContext.xml 
ibatis 的 1个+N个  路径: src+src(可配置)     名称: SqlMapConfig.xml + N


          部署到tomcat后,src目录下的配置文件会和class文件一样,自动copy到应用的 classes目录下

          spring的 配置文件在启动时,加载的是web-inf目录下的applicationContext.xml, 
          运行时使用的是web-inf/classes目录下的applicationContext.xml。

          配置web.xml使这2个路径一致:

<context-param> 
  <param-name>contextConfigLocation</param-name> 
  <param-value>/WEB-INF/classes/applicationContext.xml</param-value> 
</context-param>

3、多个配置文件的加载 

<context-param> 
        <param-name>contextConfigLocation</param-name> 
        <param-value> 
            classpath*:conf/spring/applicationContext_core.xml, 
            classpath*:conf/spring/applicationContext_dict.xml, 
            classpath*:conf/spring/applicationContext_hibernate.xml, 
            classpath*:conf/spring/applicationContext_staff*.xml, 
            classpath*:conf/spring/applicationContext_security*.xml 
        </param-value> 
    </context-param>


1)contextConfigLocation 参数定义了要装入的 Spring 配置文件

           首先与Spring相关的配置文件必须要以"applicationContext-"开头,要符合约定优于配置的思想,这样在效率上和出错率上都要好很多。 
           还有最好把所有Spring配置文件都放在一个统一的目录下,如果项目大了还可以在该目录下分模块建目录,正如struts.xml文件中的namespace,hibernate.cfg.xml中也有这样的配置。这样程序看起来不会很乱。 
           在web.xml中的配置如下: 

<context-param> 
           <param-name>contextConfigLocation</param-name> 
           <param-value>classpath*:**/applicationContext-*.xml</param-value>  
    </context-param>


2)配置文件路径中的“**”什么意思?

         "**/"表示的是任意目录; 
         "**/applicationContext-*.xml"表示任意目录下的以"applicationContext-"开头的XML文件。 
         你自己可以根据需要修改。最好把所有Spring配置文件都放在一个统一的目录下,如:

<!-- Spring 的配置 --> 
<context-param> 
 <span style="white-space:pre">	</span> <param-name>contextConfigLocation</param-name> 
 <span style="white-space:pre">	</span> <param-value>classpath:/spring/applicationContext-*.xml</param-value> 
</context-param>

3)web.xml中classpath:和classpath*:, 有什么区别? 

       classpath:只会到你的class路径中查找找文件; 
       classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找.


4、外挂xxx.properties文件



  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值