您的位置:首页 > 编程语言 > Java开发

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

2016-07-05 00:00 771 查看
配置文件:

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&characterEncoding=UTF-8&characterSetResults=UTF-8&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框架 数据源参数变化

<?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:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 自动扫描 -->

<context:component-scan base-package="com.cn.hnust" />

<!-- 引入配置文件 -->

<bean id="propertyConfigurer"

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="location" value="classpath:jdbc.properties" />

</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"

destroy-method="close">

<property name="driverClassName" value="${driver}" />

<property name="url" value="${url}" />

<property name="username" value="${username}" />

<property name="password" value="${password}" />

<!-- 初始化连接大小 -->

<property name="initialSize" value="${initialSize}"></property>

<!-- 连接池最大数量 -->

<property name="maxActive" value="${maxActive}"></property>

<!-- 连接池最大空闲 -->

<property name="maxIdle" value="${maxIdle}"></property>

<!-- 连接池最小空闲 -->

<property name="minIdle" value="${minIdle}"></property>

<!-- 获取连接最大等待时间 -->

<property name="maxWait" value="${maxWait}"></property>

</bean>

<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<property name="dataSource" ref="dataSource" />

<!-- 自动扫描mapping.xml文件 -->

<property name="mapperLocations" value="classpath:com/cn/hnust/mapping/*.xml"></property>

</bean>

<!-- DAO接口所在包名,Spring会自动查找其下的类 -->

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

<property name="basePackage" value="com.cn.hnust.dao" />

<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>

</bean>

<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->

<bean id="transactionManager"

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource" ref="dataSource" />

</bean>

</beans>

后台获取参数方式二:

@Value("#{propertyConfigurer['ACCESS_DOMAIN']}")
private String ACCESS_DOMAIN;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: