Spring based on xml configuration

Spring 配置文件示例

每次新建一個項目都要配置相關文件,非常麻煩,於是記下來,下次使用直接拷貝:

代碼塊

代碼塊語法遵循標準markdown代碼,例如:

<?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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-
            4.0.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-
            context-4.0.xsd
            http://www.springframework.org/schema/aop 
            http://www.springframework.org/schema/aop/spring-aop-
            4.0.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-
            4.0.xsd">

	<context:annotation-config />
	<context:component-scan base-package="com.alibaba.**" />

	<context:property-placeholder
location="classpath*:jdbc/jdbc.properties,classpath*:redis/redis.properties" />

	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="mapperLocations" value="classpath:mybatis/mapper/*Mapper.xml" />
		<property name="configLocation" value="classpath:mybatis/config/MybatisConfig.xml" />
	</bean>
	
	<bean id="batchSqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory" />
        <constructor-arg index="1" value="REUSE" />
    </bean>

	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="org.apache.blog.lander.repository.**" />
		<property name="annotationClass" value="org.springframework.stereotype.Repository" />
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
	</bean>

	<!-- transaction -->
	<bean id="transactionManager"
	class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>

	<tx:annotation-driven transaction-manager="transactionManager" />

	<!-- c3p0 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<property name="driverClass" value="${datasource.c3p0.driverClass}" />
		<property name="jdbcUrl" value="${datasource.c3p0.url}" />
		<property name="user" value="${datasource.c3p0.user}" />
		<property name="password" value="${datasource.c3p0.password}" />
		<property name="acquireIncrement" value="1" />
		<property name="initialPoolSize" value="3" />
		<property name="maxIdleTime" value="60" />
		<property name="maxPoolSize" value="20" />
		<property name="minPoolSize" value="5" />
	</bean>

	<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<property name="maxIdle" value="${redis.maxIdle}" />
		<property name="minIdle" value="${redis.minIdle}" />
		<property name="maxTotal" value="${redis.maxTotal}" />
		<property name="testOnBorrow" value="${redis.testOnBorrow}" />
	</bean>

	<bean id="connectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
		<property name="hostName" value="localhost" />
		<property name="port" value="6379" />
	</bean>

	<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
		<property name="connectionFactory" ref="connectionFactory" />
	</bean>
</beans>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章