Spring扫描配置文件的两种方式及获取配置文件属性参数的方式

配置文件:

database=192.168.21.236
database_port=3306
database_name=peep
database_user=peep
database_pwd=peep
KY_HOME=D://peep
VIRTUAL_DIR_NAME=/attachment/userfiles
ACCESS_DOMAIN=http://localhost:8080/

mail.smtp.host=mail.scbit.org
mail.smtp.auth=true
mail.smtp.timeout=25000
mail.smtp.from=lifecenter@scbit.org
mail.smtp.username=lifecenter@scbit.org
mail.smtp.password=LGQRs3V$BaEQ

#mongo.host=10.10.31.13
#mongo.port=27017
#mongo.dbName=metacyc
#mongo.connectionsPerHost=8
#mongo.threadsAllowedToBlockForConnectionMultiplier=4
#mongo.connectTimeout=1000
#mongo.maxWaitTime=1500
#mongo.autoConnectRetry=true
#mongo.socketKeepAlive=true
#mongo.socketTimeout=1500
#mongo.slaveOk=true

Spring扫描方式一:Hibernate框架

<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mongo="http://www.springframework.org/schema/data/mongo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context  
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop  
        http://www.springframework.org/schema/aop/spring-aop.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/data/mongo
        http://www.springframework.org/schema/data/mongo/spring-mongo.xsd"> 
 
    <context:component-scan base-package="org.scbit.lsbi.peep">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    
    <context:property-placeholder location="classpath:config.properties" />
    
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass">
            <value>com.mysql.jdbc.Driver</value>
        </property>
        <property name="jdbcUrl">
            <value>jdbc:mysql://${database}:${database_port}/${database_name}?autoReconnect=true&amp;characterEncoding=UTF-8&amp;characterSetResults=UTF-8&amp;zeroDateTimeBehavior=convertToNull</value>
        </property>
        <property name="user">
            <value>${database_user}</value>
        </property>
        <property name="password">
            <value>${database_pwd}</value>
        </property>
        <property name="minPoolSize" value="5" />
        <property name="maxPoolSize" value="30" />
        <property name="initialPoolSize" value="3" />
        <property name="maxIdleTime" value="60" />
        <property name="acquireIncrement" value="3" />
        <property name="maxStatements" value="0" />
        <property name="maxStatementsPerConnection" value="0" />
        <property name="idleConnectionTestPeriod" value="180" />
        <property name="acquireRetryAttempts" value="30" />
        <property name="breakAfterAcquireFailure" value="false" />
        <property name="acquireRetryDelay" value="1000" />
        <property name="autoCommitOnClose" value="false" />
        <property name="checkoutTimeout" value="10000" />
        <property name="numHelperThreads" value="3" />
        <property name="testConnectionOnCheckin" value="true" />
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.jdbc.lob.non_contextual_creation">true</prop>
            </props>
        </property>
        <property name="packagesToScan"> 
            <list> 
                <value>org.scbit.lsbi.peep.pojo</value> 
            </list> 
        </property> 
    </bean>
    
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    

    <!-- 声明式容器事务管理 ,transaction-manager指定事务管理器为transactionManager -->
     
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
            <tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
            <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
            <tx:method name="create*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
            <tx:method name="add*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
            <tx:method name="edit*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
            <tx:method name="login*" propagation="REQUIRED"/>
            <tx:method name="*" read-only="true" />
        </tx:attributes>
    </tx:advice>
    <aop:config expose-proxy="true">
        <aop:pointcut id="txPointcut" expression="execution(* org.scbit.lsbi.peep.service..*.*(..))" />
        <aop:advisor pointcut-ref="txPointcut" advice-ref="txAdvice" />
    </aop:config>
    
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="${mail.smtp.host}"></property>
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
                <prop key="mail.smtp.timeout">${mail.smtp.timeout}</prop>
            </props>
        </property>
        <property name="username" value="${mail.smtp.username}"></property>
        <property name="password" value="${mail.smtp.password}"></property>
    </bean>
    
<!--     <mongo:mongo-client host="${mongo.host}" port="${mongo.port}"> -->
<!--         <mongo:client-options  -->
<!--             write-concern="MAJORITY" -->
<!--             connections-per-host="${mongo.connectionsPerHost}" -->
<!--             threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}" -->
<!--             connect-timeout="${mongo.connectTimeout}" -->
<!--             max-wait-time="${mongo.maxWaitTime}" -->
<!--             socket-keep-alive="${mongo.socketKeepAlive}" -->
<!--             socket-timeout="${mongo.socketTimeout}" /> -->
<!--      </mongo:mongo-client> -->
    
<!--     <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> -->
<!--         <constructor-arg ref="mongo" /> -->
<!--         <constructor-arg name="databaseName" value="${mongo.dbName}" /> -->
<!--     </bean>  -->
</beans>

后台获取配置文件参数方式一:

@Value("${ACCESS_DOMAIN}")
  private String ACCESS_DOMAIN;

Spring 扫描方式二:Mybatis框架 数据源参数变化

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans    
  7.                         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd    
  8.                         http://www.springframework.org/schema/context    
  9.                         http://www.springframework.org/schema/context/spring-context-3.1.xsd    
  10.                         http://www.springframework.org/schema/mvc    
  11.                         http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">  
  12.     <!-- 自动扫描 -->  
  13.     <context:component-scan base-package="com.cn.hnust" />  
  14.     <!-- 引入配置文件 -->  
  15.     <bean id="propertyConfigurer"  
  16.         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  17.         <property name="location" value="classpath:jdbc.properties" />  
  18.     </bean>  
  19.   
  20.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  
  21.         destroy-method="close">  
  22.         <property name="driverClassName" value="${driver}" />  
  23.         <property name="url" value="${url}" />  
  24.         <property name="username" value="${username}" />  
  25.         <property name="password" value="${password}" /> 
  26.         <!-- 初始化连接大小 -->  
  27.         <property name="initialSize" value="${initialSize}"></property>  
  28.         <!-- 连接池最大数量 -->  
  29.         <property name="maxActive" value="${maxActive}"></property>  
  30.         <!-- 连接池最大空闲 -->  
  31.         <property name="maxIdle" value="${maxIdle}"></property>  
  32.         <!-- 连接池最小空闲 -->  
  33.         <property name="minIdle" value="${minIdle}"></property>  
  34.         <!-- 获取连接最大等待时间 -->  
  35.         <property name="maxWait" value="${maxWait}"></property>  
  36.     </bean>  
  37.   
  38.     <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->  
  39.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  40.         <property name="dataSource" ref="dataSource" />  
  41.         <!-- 自动扫描mapping.xml文件 -->  
  42.         <property name="mapperLocations" value="classpath:com/cn/hnust/mapping/*.xml"></property>  
  43.     </bean>  
  44.   
  45.     <!-- DAO接口所在包名,Spring会自动查找其下的类 -->  
  46.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
  47.         <property name="basePackage" value="com.cn.hnust.dao" />  
  48.         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>  
  49.     </bean>  
  50.   
  51.     <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->  
  52.     <bean id="transactionManager"  
  53.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  54.         <property name="dataSource" ref="dataSource" />  
  55.     </bean>  
  56.   
  57. </beans>  

后台获取参数方式二:

@Value("#{propertyConfigurer['ACCESS_DOMAIN']}")
  private String ACCESS_DOMAIN;

转载于:https://my.oschina.net/dyl226/blog/706040

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值