上一篇咱們介紹《構建dubbo分佈式平臺-maven構建ant-parent項目》,框架使用maven進行構建,根據咱們的規劃,要將子項目所有構建出來,今天重點講解的是ant-config配置文件項目的構建過程。css
導語: 將ant-config項目獨立出來的目的是將全部的配置文件進行統一項目管理,其中包括:spring相關文件配置、mybatis相關文件配置、數據源相關文件配置、基礎環境文件配置(短信、消息、oss存儲、第三方登錄、郵件等)、redis或者ehcache緩存相關配置、log4j日誌文件相關配置、統一error異常配置、spring-shiro權限的相關配置、spring和redis緩存集成相關配置等。html
1. 建立ant-config子項目,繼承ant-parent項目,eclipse的建立過程我這邊省略了。pom.xml文件配置以下:java
<span style="font-size: 16px;"><?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.sml.sz</groupId>
<artifactId>ant-project</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>ant-config</artifactId>
<name>ant-config</name>
<url>http://maven.apache.org</url>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>mysql
<build>
<!-- maven 打包後的包名 -->
<finalName>ant-config</finalName>
<resources>
<!-- 指定 src/main/resources下全部文件及文件夾爲資源文件 -->
<resource>
<directory>src/main/resources</directory>
<targetPath>${project.build.directory}/classes</targetPath>
<includes>
<include>**/*</include>
</includes>
<filtering>true</filtering>
</resource>
<!-- 根據env部署環境值,把對應環境的配置文件拷貝到classes目錄 -->
<resource>
<directory>deployEnv/${env}</directory>
<targetPath>${project.build.directory}/classes</targetPath>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
</span>web
2. 建立log4j.properties文件,配置以下:redis
<span style="font-size: 16px;"># Output pattern : date [thread] priority category - message FATAL 0 ERROR 3 WARN 4 INFO 6 DEBUG 7
log4j.rootLogger=WARN, Console, RollingFilespring
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d %-5p [%c{5}] - %m%nsql
#RollingFile
log4j.appender.RollingFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.RollingFile.File=/logs/ant/ant.log
log4j.appender.RollingFile.layout=org.apache.log4j.PatternLayout
log4j.appender.RollingFile.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n數據庫
#log4j.logger.java.sql=DEBUGexpress
#Project defalult level
log4j.logger.com.sml.sz=DEBUG
log4j.logger.com.sml.sz.common.security.shiro=WARN
log4j.logger.com.sml.sz.JedisUtils=WARN</span>
3. 建立ant.properties文件,內容以下:
<span style="font-size: 16px;">#--------------Database sttings--------------
#mysql database setting
jdbc.type=mysql
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/ant-dubbo?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
#pool settings
jdbc.pool.init=10
jdbc.pool.minIdle=30
jdbc.pool.maxActive=60
#--------------redis settings--------------
redis.keyPrefix=ant
redis.host=127.0.0.1
redis.port=6379
#-------------- System settings --------------
#\u4ea7\u54c1\u4fe1\u606f\u8bbe\u7f6e
logoName=ant
productName=ant \u5206\u5E03\u5F0F\u4F01\u4E1A\u67B6\u6784
copyrightYear=2017
version=V1.0.0
#\u662f\u5426\u5141\u8bb8\u591a\u8d26\u53f7\u540c\u65f6\u767b\u5f55
user.multiAccountLogin=true
#\u5206\u9875\u914d\u7f6e
page.pageSize=10
#-------------- Framework settings --------------
#\u4f1a\u8bdd\u8d85\u65f6\uff0c \u5355\u4f4d\uff1a\u6beb\u79d2\uff0c 20m=1200000ms, 30m=1800000ms, 60m=3600000ms
session.sessionTimeout=1800000
#\u4f1a\u8bdd\u6e05\u7406\u95f4\u9694\u65f6\u95f4\uff0c \u5355\u4f4d\uff1a\u6beb\u79d2\uff0c2m=120000ms\u3002
session.sessionTimeoutClean=120000
#\u89c6\u56fe\u6587\u4ef6\u5b58\u653e\u8def\u5f84
web.view.prefix=/WEB-INF/views/
web.view.suffix=.jsp
web.maxUploadSize=10485760
#\u9759\u6001\u6587\u4ef6\u540e\u7f00
web.staticFile=.css,.js,.png,.jpg,.gif,.jpeg,.bmp,.ico,.swf,.psd,.htc,.htm,.html,.crx,.xpi,.exe,.ipa,.apk
#--------------Email SMTP --------------
mail.host=smtp.163.com
mail.port=25
mail.username=test@163.com
mail.password=test
mail.smtp.auth=true
mail.smtp.timeout=30000
mail.default.from=test@163.com
#-------------- JMS --------------
mq.brokerURL=failover\:(tcp\://127.0.0.1\:61616)?randomize\=false&initialReconnectDelay\=1000&maxReconnectDelay\=30000
mq.userName=ant
mq.password=ant
mq.pool.maxConnections=20
#queueName
queueName.task=task_queue_1
</span>
4. 建立mybatis-config.xml配置文件,配置以下:
<span style="font-size: 16px;"><?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 全局參數 -->
<settings>
<!-- 使全局的映射器啓用或禁用緩存。 -->
<setting name="cacheEnabled" value="true"/>
<!-- 全局啓用或禁用延遲加載。當禁用時,全部關聯對象都會即時加載。 -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- 當啓用時,有延遲加載屬性的對象在被調用時將會徹底加載任意屬性。不然,每種屬性將會按須要加載。 -->
<setting name="aggressiveLazyLoading" value="true"/>
<!-- 是否容許單條sql 返回多個數據集 -->
<setting name="multipleResultSetsEnabled" value="true"/>
<!-- 是否可使用列的別名 (取決於驅動的兼容性) default:true -->
<setting name="useColumnLabel" value="true"/>
<!-- 容許JDBC 生成主鍵。 -->
<setting name="useGeneratedKeys" value="false"/>
<!-- 指定 MyBatis 如何自動映射 -->
<setting name="autoMappingBehavior" value="PARTIAL"/>
<!-- 這是默認的執行類型 -->
<setting name="defaultExecutorType" value="SIMPLE"/>
<!-- 使用駝峯命名法轉換字段。 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- 設置本地緩存範圍 session:就會有數據的共享 -->
<setting name="localCacheScope" value="SESSION"/>
<!-- 設置但JDBC類型爲空時,某些驅動程序 要指定值-->
<setting name="jdbcTypeForNull" value="NULL"/
</settings>
<!-- 類型別名 -->
<!--分頁 -->
<typeAliases>
<typeAlias alias="Page" type="com.sml.sz.common.persistence.Page" />
</typeAliases>
<!-- 插件配置 -->
<plugins>
<plugin interceptor="com.sml.sz.common.persistence.interceptor.PaginationInterceptor" />
</plugins>
</configuration>
</span>
5. 建立spring-context.xml配置文件,配置以下:
<span style="font-size: 16px;"><?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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" 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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd"
default-lazy-init="true">
<description>Spring Configuration</description>
<!-- 加載配置屬性文件 -->
<context:property-placeholder ignore-unresolvable="true" location="classpath:ant.properties" />
<!-- 加載應用屬性實例-->
<util:properties id="APP_PROP" location="classpath:ant.properties" local-override="true"/>
<!-- 使用Annotation自動註冊Bean,解決事物失效問題:在主容器中不掃描@Controller註解,在SpringMvc中只掃描@Controller註解。 -->
<context:component-scan base-package="com.sml.sz"><!-- base-package 若是多個,用「,」分隔 -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- MyBatis begin -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.sml.sz"/>
<property name="typeAliasesSuperType" value="com.sml.sz.common.persistence.BaseEntity"/>
<property name="mapperLocations" value="classpath*:mappings/**/*.xml"/>
<property name="configLocation" value="classpath:/mybatis-config.xml"></property>
</bean>
<!-- 掃描basePackage下全部以@MyBatisDao註解的接口 -->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<property name="basePackage" value="com.sml.sz.*"/>
<property name="annotationClass" value="com.sml.sz.common.persistence.annotation.MyBatisDao"/>
</bean>
<!-- 定義事務 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置 Annotation 驅動,掃描@Transactional註解的類定義事務 -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
<!-- MyBatis end -->
<!-- 配置 JSR303 Bean Validator 定義 -->
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
<!-- 緩存配置 -->
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:${ehcache.configFile}" />
</bean>
<!-- 數據源配置, 使用 BoneCP 數據庫鏈接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!-- 數據源驅動類可不寫,Druid默認會自動根據URL識別DriverClass -->
<property name="driverClassName" value="${jdbc.driver}" />
<!-- 基本屬性 url、user、password -->
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="${jdbc.pool.init}" />
<property name="minIdle" value="${jdbc.pool.minIdle}" />
<property name="maxActive" value="${jdbc.pool.maxActive}" />
<!-- 配置獲取鏈接等待超時的時間 -->
<property name="maxWait" value="60000" />
<!-- 配置間隔多久才進行一次檢測,檢測須要關閉的空閒鏈接,單位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一個鏈接在池中最小生存的時間,單位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="validationQuery" value="${jdbc.testSql}" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<!-- 打開PSCache,而且指定每一個鏈接上PSCache的大小(Oracle使用)
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> -->
<!-- 配置監控統計攔截的filters -->
<property name="filters" value="stat" />
</bean>
</beans></span>
6 建立spring-context-shiro.xml文件,配置以下:
<span style="font-size: 16px;"><?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" 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"
default-lazy-init="true">
<description>Shiro Configuration</description>
<!-- 加載配置屬性文件 -->
<context:property-placeholder ignore-unresolvable="true" location="classpath:ant.properties" />
<!-- Shiro權限過濾過濾器定義 -->
<bean name="shiroFilterChainDefinitions" class="java.lang.String">
<constructor-arg>
<value>
/static/** = anon
/userfiles/** = anon
${adminPath}/login = authc
${adminPath}/logout = logout
${adminPath}/** = user
/act/rest/service/** = user
/ReportServer/** = user
</value>
</constructor-arg>
</bean>
<!-- 安全認證過濾器 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager" />
<property name="loginUrl" value="${adminPath}/login" />
<property name="successUrl" value="${adminPath}?login" />
<property name="filters">
<map>
<entry key="authc" value-ref="formAuthenticationFilter"/>
</map>
</property>
<property name="filterChainDefinitions">
<ref bean="shiroFilterChainDefinitions"/>
</property>
</bean>
<!-- 定義Shiro安全管理配置 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="systemAuthorizingRealm" />
<property name="sessionManager" ref="sessionManager" />
<property name="cacheManager" ref="shiroCacheManager" />
</bean>
<!-- 自定義會話管理配置 -->
<bean id="sessionManager" class="com.sml.sz.common.security.shiro.session.SessionManager">
<property name="sessionDAO" ref="sessionDAO"/>
<!-- 會話超時時間,單位:毫秒 -->
<property name="globalSessionTimeout" value="${session.sessionTimeout}"/>
<!-- 定時清理失效會話, 清理用戶直接關閉瀏覽器形成的孤立會話 -->
<property name="sessionValidationInterval" value="${session.sessionTimeoutClean}"/>
<property name="sessionValidationSchedulerEnabled" value="true"/>
<property name="sessionIdCookie" ref="sessionIdCookie"/>
<property name="sessionIdCookieEnabled" value="true"/>
</bean>
<bean id="sessionIdCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
<constructor-arg name="name" value="ant.session.id"/>
</bean>
<!-- 自定義Session存儲容器 -->
<!-- <bean id="sessionDAO" class="com.sml.sz.common.security.shiro.session.JedisSessionDAO">
<property name="sessionIdGenerator" ref="idGen" />
<property name="sessionKeyPrefix" value="${redis.keyPrefix}_session_" />
</bean> -->
<bean id="sessionDAO" class="com.sml.sz.common.security.shiro.session.CacheSessionDAO">
<property name="sessionIdGenerator" ref="idGen" />
<property name="activeSessionsCacheName" value="activeSessionsCache" />
<property name="cacheManager" ref="shiroCacheManager" />
</bean>
<!-- 定義受權緩存管理器 -->
<bean id="shiroCacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManager" ref="cacheManager"/>
</bean>
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
<!-- AOP式方法級權限檢查 -->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor">
<property name="proxyTargetClass" value="true" />
</bean>
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"/>
</bean>
</beans></span>
7. 代碼結構以下:
歡迎你們跟我一塊兒學習《構建dubbo分佈式平臺》,但願你們持續關注後面的文章!