摘要: 简单介绍使用Spring+Shiro搭建基于Redis的分布式权限系统。
这篇主要介绍Shiro如何与redis结合搭建分布式权限系统,至于如何使用和配置Shiro就不多说了。
要实现分布式,主要需要解决2个大问题。
第一个解决shiro session共享的问题。这个可以通过自定义sessionDao实现。继承 CachingSessionDao。
public class RedisSessionDao extends CachingSessionDAO {
private final static Logger log = LoggerFactory.getLogger(RedisSessionDao.class);
</span><span style="color: #0000ff;">private</span> RedisTemplate<String, Object><span style="color: #000000;"> redisTemplate;
</span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">int</span> defaultExpireTime = <span style="color: #800080;">3600</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">private</span> CacheManager cm=<span style="color: #0000ff;">null</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">public</span> RedisSessionDao(RedisTemplate<String, Object> redisTemplate,<span style="color: #0000ff;">int</span><span style="color: #000000;"> defaultExpireTime) {
</span><span style="color: #0000ff;">this</span>.redisTemplate =<span style="color: #000000;"> redisTemplate;
</span><span style="color: #0000ff;">this</span>.defaultExpireTime =<span style="color: #000000;"> defaultExpireTime;
}
@Override
</span><span style="color: #0000ff;">protected</span><span style="color: #000000;"> Serializable doCreate(Session session) {
Serializable sessionId </span>=<span style="color: #000000;"> generateSessionId(session);
cm </span>=<span style="color: #000000;"> CacheManager.create();
</span><span style="color: #0000ff;">if</span>(cm==<span style="color: #0000ff;">null</span><span style="color: #000000;">){
cm </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> CacheManager(getCacheManagerConfigFileInputStream());
}
Ehcache ehCache </span>= cm.getCache(<span style="color: #800000;">"</span><span style="color: #800000;">sessioncache</span><span style="color: #800000;">"</span><span style="color: #000000;">);
assignSessionId(session, sessionId);
redisTemplate.opsForValue().</span><span style="color: #0000ff;">set</span><span style="color: #000000;">(sessionId.toString(), session);
redisTemplate.expire(sessionId.toString(), </span><span style="color: #0000ff;">this</span><span style="color: #000000;">.defaultExpireTime, TimeUnit.SECONDS);
ehCache.put(</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Element(sessionId.toString(),session));
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> sessionId;
}
@Override
</span><span style="color: #0000ff;">protected</span><span style="color: #000000;"> Session doReadSession(Serializable sessionId) {
</span><span style="color: #008000;">//</span><span style="color: #008000;">此方法不会执行,不用管</span>
<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">null</span><span style="color: #000000;">;
}
@Override
</span><span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> doUpdate(Session session) {
</span><span style="color: #008000;">//</span><span style="color: #008000;">该方法交给父类去执行</span>
}
@Override
</span><span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> doDelete(Session session) {
Serializable sessionId </span>=<span style="color: #000000;"> session.getId();
cm </span>=<span style="color: #000000;"> CacheManager.create();
</span><span style="color: #0000ff;">if</span>(cm==<span style="color: #0000ff;">null</span><span style="color: #000000;">){
cm </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> CacheManager(getCacheManagerConfigFileInputStream());
}
Ehcache ehCache </span>= cm.getCache(<span style="color: #800000;">"</span><span style="color: #800000;">sessioncache</span><span style="color: #800000;">"</span><span style="color: #000000;">);
redisTemplate.delete(sessionId.toString());
ehCache.remove(sessionId.toString());
}
</span><span style="color: #0000ff;">protected</span><span style="color: #000000;"> InputStream getCacheManagerConfigFileInputStream() {
String configFile </span>= <span style="color: #800000;">"</span><span style="color: #800000;">classpath:ehcache.xml</span><span style="color: #800000;">"</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> ResourceUtils.getInputStreamForPath(configFile);
} </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (IOException e) {
</span><span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> ConfigurationException(<span style="color: #800000;">"</span><span style="color: #800000;">Unable to obtain input stream for cacheManagerConfigFile [</span><span style="color: #800000;">"</span> +<span style="color: #000000;">
configFile </span>+ <span style="color: #800000;">"</span><span style="color: #800000;">]</span><span style="color: #800000;">"</span><span style="color: #000000;">, e);
}
}</span></pre>
第二个解决shiro cache的问题,这个可以通过自定义CacheManager实现,shiro默认的cache是基于ehcache的,自定义Redis实现的过程可以仿照shiro-ehcache.jar源码来实现。
附上spring中的完整配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.1.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.zhm.ssr"/>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
<value>classpath:redis.properties</value>
</list>
</property>
</bean>
<aop:aspectj-autoproxy />
<!-- spring 线程池配置 -->
<task:annotation-driven executor="executorWithCallerRunsPolicy"/>
<task:executor id="executorWithCallerRunsPolicy" pool-size="2-5" queue-capacity="50" rejection-policy="CALLER_RUNS"/>
<!-- redis缓存 -->
<bean id=<span style="color: #800000;">"</span><span style="color: #800000;">cacheManager</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">class</span>=<span style="color: #800000;">"</span><span style="color: #800000;">org.springframework.data.redis.cache.RedisCacheManager</span><span style="color: #800000;">"</span> c:template-<span style="color: #0000ff;">ref</span>=<span style="color: #800000;">"</span><span style="color: #800000;">redisTemplate</span><span style="color: #800000;">"</span>>
<property name=<span style="color: #800000;">"</span><span style="color: #800000;">defaultExpiration</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">31536000</span><span style="color: #800000;">"</span>></property>
</bean>
<!-- 支持注解缓存 -->
<cache:annotation-driven cache-manager=<span style="color: #800000;">"</span><span style="color: #800000;">cacheManager</span><span style="color: #800000;">"</span>/>
<bean <span style="color: #0000ff;">class</span>=<span style="color: #800000;">"</span><span style="color: #800000;">org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator</span><span style="color: #800000;">"</span>/>
<!-- 文件上传大小限制 -->
<bean id=<span style="color: #800000;">"</span><span style="color: #800000;">multipartResolver</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">class</span>=<span style="color: #800000;">"</span><span style="color: #800000;">org.springframework.web.multipart.commons.CommonsMultipartResolver</span><span style="color: #800000;">"</span>>
<property name=<span style="color: #800000;">"</span><span style="color: #800000;">maxUploadSize</span><span style="color: #800000;">"</span>>
<value><span style="color: #800080;">4097152000</span></value>
</property>
</bean>
<!-- ========shiro 配置开始 ======== -->
<bean id=<span style="color: #800000;">"</span><span style="color: #800000;">myRealm</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">class</span>=<span style="color: #800000;">"</span><span style="color: #800000;">com.zhm.ssr.shiro.CustomRealm</span><span style="color: #800000;">"</span>>
<!-- 配置AuthorizationInfo信息不用放redis缓存,直接放session即可 -->
<property name=<span style="color: #800000;">"</span><span style="color: #800000;">cachingEnabled</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">false</span><span style="color: #800000;">"</span>/>
<property name=<span style="color: #800000;">"</span><span style="color: #800000;">authenticationCachingEnabled</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">false</span><span style="color: #800000;">"</span> />
</bean>
<!-- session 保存到cookie,关闭浏览器下次可以直接登录认证,当maxAge为-1不会写cookie。-->
<bean id=<span style="color: #800000;">"</span><span style="color: #800000;">sessionIdCookie</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">class</span>=<span style="color: #800000;">"</span><span style="color: #800000;">org.apache.shiro.web.servlet.SimpleCookie</span><span style="color: #800000;">"</span>>
<constructor-arg value=<span style="color: #800000;">"</span><span style="color: #800000;">sid</span><span style="color: #800000;">"</span>/>
<property name=<span style="color: #800000;">"</span><span style="color: #800000;">httpOnly</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">true</span><span style="color: #800000;">"</span>/>
<!-- 浏览器关闭session失效,不计入cookie -->
<property name=<span style="color: #800000;">"</span><span style="color: #800000;">maxAge</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">-1</span><span style="color: #800000;">"</span>/>
</bean>
<!--<span style="color: #000000;"> 记住我功能,当关闭浏览器下次再访问的时候不需要登录也能查看。只对filterChainDefinitions设置为user级别的链接有效,
类似于淘宝看商品、添加购物车,不需要验证是否登录。但是提交订单就必须登录。
</span>-->
<bean id=<span style="color: #800000;">"</span><span style="color: #800000;">rememberMeCookie</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">class</span>=<span style="color: #800000;">"</span><span style="color: #800000;">org.apache.shiro.web.servlet.SimpleCookie</span><span style="color: #800000;">"</span>>
<constructor-arg value=<span style="color: #800000;">"</span><span style="color: #800000;">rememberMe</span><span style="color: #800000;">"</span>/>
<property name=<span style="color: #800000;">"</span><span style="color: #800000;">httpOnly</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">true</span><span style="color: #800000;">"</span>/>
<property name=<span style="color: #800000;">"</span><span style="color: #800000;">maxAge</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">2592000</span><span style="color: #800000;">"</span>/><!-- 30天 -->
</bean>
<bean id=<span style="color: #800000;">"</span><span style="color: #800000;">rememberMeManager</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">class</span>=<span style="color: #800000;">"</span><span style="color: #800000;">org.apache.shiro.web.mgt.CookieRememberMeManager</span><span style="color: #800000;">"</span>>
<property name=<span style="color: #800000;">"</span><span style="color: #800000;">cookie</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">ref</span>=<span style="color: #800000;">"</span><span style="color: #800000;">rememberMeCookie</span><span style="color: #800000;">"</span>/>
<!-- aes key。shiro默认的key是不安全的,可以使用工程utils包的GenerateAESKey生成一个自定义的key-->
<property name=<span style="color: #800000;">"</span><span style="color: #800000;">cipherKey</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">#{T(org.apache.shiro.codec.Base64).decode('XgGkgqGqYrix9lI6vxcrRw==')}</span><span style="color: #800000;">"</span>/>
</bean>
<bean id=<span style="color: #800000;">"</span><span style="color: #800000;">sessionManager</span><span style="color: #800000;">"</span>
<span style="color: #0000ff;">class</span>=<span style="color: #800000;">"</span><span style="color: #800000;">org.apache.shiro.web.session.mgt.DefaultWebSessionManager</span><span style="color: #800000;">"</span>>
<property name=<span style="color: #800000;">"</span><span style="color: #800000;">sessionDAO</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">ref</span>=<span style="color: #800000;">"</span><span style="color: #800000;">sessionDAO</span><span style="color: #800000;">"</span>/>
<property name=<span style="color: #800000;">"</span><span style="color: #800000;">sessionIdCookieEnabled</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">true</span><span style="color: #800000;">"</span>/>
<property name=<span style="color: #800000;">"</span><span style="color: #800000;">sessionIdCookie</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">ref</span>=<span style="color: #800000;">"</span><span style="color: #800000;">sessionIdCookie</span><span style="color: #800000;">"</span>/>
<property name=<span style="color: #800000;">"</span><span style="color: #800000;">deleteInvalidSessions</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">true</span><span style="color: #800000;">"</span> />
<property name=<span style="color: #800000;">"</span><span style="color: #800000;">sessionValidationSchedulerEnabled</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">true</span><span style="color: #800000;">"</span> />
</bean>
<bean id=<span style="color: #800000;">"</span><span style="color: #800000;">securityManager</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">class</span>=<span style="color: #800000;">"</span><span style="color: #800000;">org.apache.shiro.web.mgt.DefaultWebSecurityManager</span><span style="color: #800000;">"</span>>
<property name=<span style="color: #800000;">"</span><span style="color: #800000;">realm</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">ref</span>=<span style="color: #800000;">"</span><span style="color: #800000;">myRealm</span><span style="color: #800000;">"</span>/>
<!-- shiro使用redis缓存 -->
<property name=<span style="color: #800000;">"</span><span style="color: #800000;">cacheManager</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">ref</span>=<span style="color: #800000;">"</span><span style="color: #800000;">redisCacheManager</span><span style="color: #800000;">"</span> />
<property name=<span style="color: #800000;">"</span><span style="color: #800000;">sessionManager</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">ref</span>=<span style="color: #800000;">"</span><span style="color: #800000;">sessionManager</span><span style="color: #800000;">"</span> />
<!-- 客户端勾选记住 -->
<property name=<span style="color: #800000;">"</span><span style="color: #800000;">rememberMeManager</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">ref</span>=<span style="color: #800000;">"</span><span style="color: #800000;">rememberMeManager</span><span style="color: #800000;">"</span>/>
</bean>
<bean id=<span style="color: #800000;">"</span><span style="color: #800000;">redisCacheManager</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">class</span>=<span style="color: #800000;">"</span><span style="color: #800000;">com.zhm.ssr.shiro.RedisCacheManager</span><span style="color: #800000;">"</span> >
<property name=<span style="color: #800000;">"</span><span style="color: #800000;">redisManager</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">ref</span>=<span style="color: #800000;">"</span><span style="color: #800000;">redisManager</span><span style="color: #800000;">"</span> />
</bean>
<bean id=<span style="color: #800000;">"</span><span style="color: #800000;">redisManager</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">class</span>=<span style="color: #800000;">"</span><span style="color: #800000;">com.zhm.ssr.shiro.RedisManager</span><span style="color: #800000;">"</span>>
<property name=<span style="color: #800000;">"</span><span style="color: #800000;">expire</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">${redis.expireTime}</span><span style="color: #800000;">"</span> />
<property name=<span style="color: #800000;">"</span><span style="color: #800000;">host</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">${redis.host}</span><span style="color: #800000;">"</span> />
<property name=<span style="color: #800000;">"</span><span style="color: #800000;">password</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">${redis.pass}</span><span style="color: #800000;">"</span> />
<property name=<span style="color: #800000;">"</span><span style="color: #800000;">port</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">${redis.port}</span><span style="color: #800000;">"</span> />
<property name=<span style="color: #800000;">"</span><span style="color: #800000;">timeout</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">${redis.maxWait}</span><span style="color: #800000;">"</span> />
</bean>
<bean id=<span style="color: #800000;">"</span><span style="color: #800000;">shiroFilter</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">class</span>=<span style="color: #800000;">"</span><span style="color: #800000;">org.apache.shiro.spring.web.ShiroFilterFactoryBean</span><span style="color: #800000;">"</span>>
<property name=<span style="color: #800000;">"</span><span style="color: #800000;">securityManager</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">ref</span>=<span style="color: #800000;">"</span><span style="color: #800000;">securityManager</span><span style="color: #800000;">"</span>/>
<property name=<span style="color: #800000;">"</span><span style="color: #800000;">loginUrl</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">/login.html</span><span style="color: #800000;">"</span>/>
<property name=<span style="color: #800000;">"</span><span style="color: #800000;">unauthorizedUrl</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">/nolimit.html</span><span style="color: #800000;">"</span>/>
<property name=<span style="color: #800000;">"</span><span style="color: #800000;">filters</span><span style="color: #800000;">"</span>>
<map>
<entry key=<span style="color: #800000;">"</span><span style="color: #800000;">mainFilter</span><span style="color: #800000;">"</span>>
<bean <span style="color: #0000ff;">class</span>=<span style="color: #800000;">"</span><span style="color: #800000;">com.zhm.ssr.filters.CustomAuthorizationFilter</span><span style="color: #800000;">"</span> />
</entry>
</map>
</property>
<property name=<span style="color: #800000;">"</span><span style="color: #800000;">filterChainDefinitions</span><span style="color: #800000;">"</span>>
<!--<span style="color: #000000;"> 登录页面的所有请求,包括资源文件全部设定为匿名
修改用户信息功能需要验证登录,其他都使用user过滤器。即rememberMe功能。
</span>-->
<value>
/login**=<span style="color: #000000;">anon
</span>/user/doLogin**=<span style="color: #000000;">anon
</span>/lib/login.js=<span style="color: #000000;">anon
</span>/css/theme.css=<span style="color: #000000;">anon
</span>/favicon.ico=<span style="color: #000000;">anon
</span>/lib/font-awesome/css/font-awesome.css=<span style="color: #000000;">anon
</span>/js/html5.js=<span style="color: #000000;">anon
</span>/user/edit<span style="color: #008000;">/*</span><span style="color: #008000;">*=authc,perms[admin:manage]
/**=mainFilter,user
</value>
</property>
</bean>
<!-- 自定义shiro的sessionDao,把session写入redis -->
<bean id="sessionDAO" class="com.zhm.ssr.shiro.RedisSessionDao">
<constructor-arg ref="redisTemplate" />
<constructor-arg value="${redis.expireTime}" />
</bean>
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
<!-- @shiro注解抛出异常之后跳转的页面。-->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="org.apache.shiro.authz.UnauthorizedException">
redirect:/nolimit.html
</prop>
<prop key="org.apache.shiro.authz.UnauthenticatedException">
redirect:/login.html
</prop>
</props>
</property>
</bean>
<!-- ========shiro 配置结束 ======== -->
<!-- spring-data-redis配置,主要用作redis缓存 begin -->
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:timeout="5000"/>
<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" />
<bean id="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
<bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"
p:connection-factory-ref="connectionFactory">
<property name="KeySerializer" ref="stringRedisSerializer" />
<property name="ValueSerializer" ref="stringRedisSerializer" />
<property name="hashKeySerializer" ref="stringRedisSerializer" />
<property name="hashValueSerializer" ref="stringRedisSerializer" />
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref="connectionFactory">
<property name="KeySerializer" ref="stringRedisSerializer" />
<property name="ValueSerializer" ref="jdkSerializationRedisSerializer" />
<property name="hashKeySerializer" ref="stringRedisSerializer" />
<property name="hashValueSerializer" ref="jdkSerializationRedisSerializer" />
<property name="enableTransactionSupport" value="true" />
</bean>
<!-- spring-data-redis配置,主要用作redis缓存 end -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbch2.driverClassName}" />
<property name="url" value="${jdbch2.url}" />
<property name="username" value="${jdbch2.username}" />
<property name="password" value="${jdbch2.password}" />
<property name="validationQuery" value="select 1 " />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource"><ref bean="dataSource"/></property>
</bean>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
</beans>