spring 配置文件分解

 1。 spring 多个配置文件的整合:

from: http://lihongyang66.javaeye.com/blog/493960

 

 

spring配置分解按模块分解,比把成百个bean写到一个文件中要清楚的多,找bean修改也方便的多,同时减少团队开发修改一个文件产生的冲突。

 

web中的配置

Java代码 复制代码
  1. <context-param>   
  2.         <param-name>contextConfigLocation</param-name>   
  3.         <param-value>WEB-INF/classes/spring*.xml</param-value>
  4. </context-param>   
  5.   
  6. <listener>   
  7.         <listener-class>   
  8.             org.springframework.web.context.ContextLoaderListener   
  9.         </listener-class>   
  10.  </listener>   

 

spring主文件spring.xml(默认为applicationContext.xml,也放到web.xml中指定的路径下:WEB-INF/classes/)

Java代码 复制代码
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">   
  4.        
  5.     <bean id="springUtil" class="com.cplat.util.SpringUtil"></bean>   
  6.   
  7.     <import resource="classpath:com/cplat/**/spring-*.xml" />   
  8.        
  9. </beans>  
     

 

模块中的配置文件

Java代码 复制代码
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">   
  4.   
  5.    <bean id="userDao" class="com.cplat.modules.user.UserDaoImpl">   
  6.         <property name="sessionFactory" ref="sessionFactory"></property>   
  7.    </bean>   
  8.    <bean id="userService" class="com.cplat.modules.user.UserServiceImpl">   
  9.         <property name="userDao" ref="userDao"></property>   
  10.    </bean>   
  11.    <bean id="userAction" class="com.cplat.modules.user.UserAction">   
  12.         <property name="userService" ref="userService"></property>   
  13.         <property name="roleService" ref="roleService"></property>   
  14.    </bean>   
  15. </beans>  

 

2。 spring读取jdbc配置文件:

from: http://fhqllt.javaeye.com/blog/505587

 

  1. <bean id="propertyConfigurer"         
  2.         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">          
  3.     <property name="location" value="/WEB-INF/jdbc.properties"/>          
  4. </bean>          
  5. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"           
  6.         destroy-method="close">          
  7.     <property name="driverClassName" value="${jdbc.driverClassName}" />          
  8.     <property name="url" value="${jdbc.url}" />          
  9.     <property name="username" value="${jdbc.username}" />          
  10.     <property name="password" value="${jdbc.password}" />          
  11. </bean>       
  12.   

 

    在jdbc.properties属性文件中定义属性值:    

  1.     jdbc.driverClassName= com.mysql.jdbc.Driver    
  2.     jdbc.url= jdbc:mysql://localhost:3309/sampledb    
  3.     jdbc.username=root    
  4.    jdbc.password=1234    

 

3。 指定hibernate映射文件

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource">
     <ref local="dataSource"/>
    </property>
    <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.OracleDialect
                </prop>
                <prop key="hibernate.show_sql">
                  true
                </prop>
            </props>
        </property>
        <property name="mappingDirectoryLocations">
            <list>
                <value>/WEB-INF/config/hibernate/</value>
            </list>
        </property>

  </bean>

 

其中斜体部分制定的是映射文件所在的目录,也可列出每个文件:

<property name="mappingResources">
   <list>
    <value>/WEB-INF/config/hibernate/User.hbm.xml</value>

    <value>/WEB-INF/config/hibernate/Book.hbm.xml</value>

      ......
   </list>
  </property>

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud 是一个用于构建分布式系统的开发工具集合,它基于 Spring Framework 提供了一系列的模块,每个模块都负责特定的功能。下面是 Spring Cloud 的主要模块: 1. Spring Cloud Config:提供了集中式的外部配置管理,可以将配置文件存储在版本控制系统中,并提供了服务端和客户端的支持。 2. Spring Cloud Netflix:包含了一些 Netflix 开源项目的集成,包括 Eureka(服务注册与发现)、Ribbon(客户端负载均衡)、Hystrix(断路器)等,这些组件可以帮助开发者构建高可用、弹性和可靠的分布式系统。 3. Spring Cloud OpenFeign:基于 Netflix Feign 的封装,提供了声明式的服务调用方式,简化了服务之间的通信,支持负载均衡、服务熔断等功能。 4. Spring Cloud Ribbon:提供了客户端负载均衡的能力,可以根据不同的负载均衡策略来分发请求。 5. Spring Cloud Gateway:基于 Spring 5、Project Reactor 和 Spring Boot 2 构建的网关,可以用于构建统一的 API 网关,实现路由、过滤、限流等功能。 6. Spring Cloud Sleuth:提供了分布式跟踪解决方案,可以追踪和记录请求在分布式系统中的调用链路。 7. Spring Cloud Stream:基于 Spring Boot 的消息微服务开发框架,提供了一套统一的编程模型,简化了消息驱动微服务的开发。 8. Spring Cloud Bus:用于在分布式系统中传播状态变化的消息总线,可以将配置文件的变更通知到各个微服务实例。 这些模块可以根据具体的需求进行选择和组合,帮助开发者构建弹性、可靠和可扩展的分布式系统。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值