Java Spring 后端项目搭建
生活随笔
收集整理的這篇文章主要介紹了
Java Spring 后端项目搭建
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
參考了幾位同行的Blogs和StackOverflow上的許多問答,搭建了此后端項目,替換原來的node.js后端,和前一篇中搭建的Vue Web App項目配合使用,后端準備只提供服務,不包含后端裝配的頁面,打算只使用MongoDb和Redis,所以JPA部分注釋掉了,可打開使用,核心文件如下,供需要的人參考。
pom.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>JavaWeb</groupId> 8 <artifactId>JavaWeb</artifactId> 9 <version>1.0</version> 10 <build> 11 <plugins> 12 <plugin> 13 <groupId>org.apache.maven.plugins</groupId> 14 <artifactId>maven-compiler-plugin</artifactId> 15 <configuration> 16 <source>1.8</source> 17 <target>1.8</target> 18 </configuration> 19 </plugin> 20 </plugins> 21 </build> 22 23 <properties> 24 <org.springframework.version>5.0.5.RELEASE</org.springframework.version> 25 </properties> 26 27 <dependencies> 28 <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --> 29 <dependency> 30 <groupId>org.apache.commons</groupId> 31 <artifactId>commons-lang3</artifactId> 32 <version>3.7</version> 33 </dependency> 34 35 <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 --> 36 <dependency> 37 <groupId>org.apache.commons</groupId> 38 <artifactId>commons-collections4</artifactId> 39 <version>4.1</version> 40 </dependency> 41 42 <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-compress --> 43 <dependency> 44 <groupId>org.apache.commons</groupId> 45 <artifactId>commons-compress</artifactId> 46 <version>1.16.1</version> 47 </dependency> 48 49 <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-math3 --> 50 <dependency> 51 <groupId>org.apache.commons</groupId> 52 <artifactId>commons-math3</artifactId> 53 <version>3.6.1</version> 54 </dependency> 55 56 <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-io --> 57 <dependency> 58 <groupId>org.apache.commons</groupId> 59 <artifactId>commons-io</artifactId> 60 <version>1.3.2</version> 61 </dependency> 62 63 <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 --> 64 <dependency> 65 <groupId>org.apache.commons</groupId> 66 <artifactId>commons-pool2</artifactId> 67 <version>2.5.0</version> 68 </dependency> 69 70 <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-text --> 71 <dependency> 72 <groupId>org.apache.commons</groupId> 73 <artifactId>commons-text</artifactId> 74 <version>1.3</version> 75 </dependency> 76 77 <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-configuration2 --> 78 <dependency> 79 <groupId>org.apache.commons</groupId> 80 <artifactId>commons-configuration2</artifactId> 81 <version>2.2</version> 82 </dependency> 83 84 <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-proxy --> 85 <dependency> 86 <groupId>org.apache.commons</groupId> 87 <artifactId>commons-proxy</artifactId> 88 <version>1.0</version> 89 </dependency> 90 91 <!-- https://mvnrepository.com/artifact/commons-cli/commons-cli --> 92 <dependency> 93 <groupId>commons-cli</groupId> 94 <artifactId>commons-cli</artifactId> 95 <version>1.4</version> 96 </dependency> 97 98 99 <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core --> 100 <dependency> 101 <groupId>com.fasterxml.jackson.core</groupId> 102 <artifactId>jackson-core</artifactId> 103 <version>2.9.5</version> 104 </dependency> 105 106 <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --> 107 <dependency> 108 <groupId>com.fasterxml.jackson.core</groupId> 109 <artifactId>jackson-databind</artifactId> 110 <version>2.9.5</version> 111 </dependency> 112 113 <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations --> 114 <dependency> 115 <groupId>com.fasterxml.jackson.core</groupId> 116 <artifactId>jackson-annotations</artifactId> 117 <version>2.9.5</version> 118 </dependency> 119 120 <!-- https://mvnrepository.com/artifact/com.fasterxml.uuid/java-uuid-generator --> 121 <dependency> 122 <groupId>com.fasterxml.uuid</groupId> 123 <artifactId>java-uuid-generator</artifactId> 124 <version>3.1.5</version> 125 </dependency> 126 127 <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api --> 128 <dependency> 129 <groupId>org.slf4j</groupId> 130 <artifactId>slf4j-api</artifactId> 131 <version>1.7.25</version> 132 </dependency> 133 134 <!-- https://mvnrepository.com/artifact/org.slf4j/jcl-over-slf4j --> 135 <dependency> 136 <groupId>org.slf4j</groupId> 137 <artifactId>jcl-over-slf4j</artifactId> 138 <version>1.7.25</version> 139 </dependency> 140 141 <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core --> 142 <dependency> 143 <groupId>org.apache.logging.log4j</groupId> 144 <artifactId>log4j-core</artifactId> 145 <version>2.11.0</version> 146 </dependency> 147 148 <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api --> 149 <dependency> 150 <groupId>org.apache.logging.log4j</groupId> 151 <artifactId>log4j-api</artifactId> 152 <version>2.11.0</version> 153 </dependency> 154 155 <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j-impl --> 156 <dependency> 157 <groupId>org.apache.logging.log4j</groupId> 158 <artifactId>log4j-slf4j-impl</artifactId> 159 <version>2.11.0</version> 160 </dependency> 161 162 <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-web --> 163 <dependency> 164 <groupId>org.apache.logging.log4j</groupId> 165 <artifactId>log4j-web</artifactId> 166 <version>2.11.0</version> 167 </dependency> 168 169 <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> 170 <dependency> 171 <groupId>org.projectlombok</groupId> 172 <artifactId>lombok</artifactId> 173 <version>1.16.20</version> 174 <scope>provided</scope> 175 </dependency> 176 177 <!-- https://mvnrepository.com/artifact/junit/junit --> 178 <dependency> 179 <groupId>junit</groupId> 180 <artifactId>junit</artifactId> 181 <version>4.12</version> 182 <scope>test</scope> 183 </dependency> 184 185 186 <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> 187 <dependency> 188 <groupId>javax.servlet</groupId> 189 <artifactId>javax.servlet-api</artifactId> 190 <version>4.0.0</version> 191 <scope>provided</scope> 192 </dependency> 193 194 <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api --> 195 <dependency> 196 <groupId>javax.servlet.jsp</groupId> 197 <artifactId>javax.servlet.jsp-api</artifactId> 198 <version>2.3.1</version> 199 <scope>provided</scope> 200 </dependency> 201 202 <!-- https://mvnrepository.com/artifact/javax.servlet/jstl --> 203 <dependency> 204 <groupId>javax.servlet</groupId> 205 <artifactId>jstl</artifactId> 206 <version>1.2</version> 207 </dependency> 208 209 <!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> 210 <dependency> 211 <groupId>org.springframework</groupId> 212 <artifactId>spring-core</artifactId> 213 <version>${org.springframework.version}</version> 214 </dependency> 215 216 <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --> 217 <dependency> 218 <groupId>org.springframework</groupId> 219 <artifactId>spring-beans</artifactId> 220 <version>${org.springframework.version}</version> 221 </dependency> 222 223 <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx --> 224 <dependency> 225 <groupId>org.springframework</groupId> 226 <artifactId>spring-tx</artifactId> 227 <version>${org.springframework.version}</version> 228 </dependency> 229 230 <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> 231 <dependency> 232 <groupId>org.springframework</groupId> 233 <artifactId>spring-context</artifactId> 234 <version>${org.springframework.version}</version> 235 </dependency> 236 237 <!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support --> 238 <dependency> 239 <groupId>org.springframework</groupId> 240 <artifactId>spring-context-support</artifactId> 241 <version>${org.springframework.version}</version> 242 </dependency> 243 244 <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop --> 245 <dependency> 246 <groupId>org.springframework</groupId> 247 <artifactId>spring-aop</artifactId> 248 <version>${org.springframework.version}</version> 249 </dependency> 250 251 <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects --> 252 <dependency> 253 <groupId>org.springframework</groupId> 254 <artifactId>spring-aspects</artifactId> 255 <version>${org.springframework.version}</version> 256 </dependency> 257 258 <!-- https://mvnrepository.com/artifact/org.springframework/spring-web --> 259 <dependency> 260 <groupId>org.springframework</groupId> 261 <artifactId>spring-web</artifactId> 262 <version>${org.springframework.version}</version> 263 </dependency> 264 265 <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> 266 <dependency> 267 <groupId>org.springframework</groupId> 268 <artifactId>spring-webmvc</artifactId> 269 <version>${org.springframework.version}</version> 270 </dependency> 271 272 <!-- https://mvnrepository.com/artifact/org.springframework/spring-test --> 273 <dependency> 274 <groupId>org.springframework</groupId> 275 <artifactId>spring-test</artifactId> 276 <version>${org.springframework.version}</version> 277 <scope>test</scope> 278 </dependency> 279 280 <!-- https://mvnrepository.com/artifact/org.springframework.session/spring-session-core --> 281 <dependency> 282 <groupId>org.springframework.session</groupId> 283 <artifactId>spring-session-core</artifactId> 284 <version>2.0.2.RELEASE</version> 285 </dependency> 286 287 <!-- https://mvnrepository.com/artifact/org.springframework.session/spring-session-data-redis --> 288 <dependency> 289 <groupId>org.springframework.session</groupId> 290 <artifactId>spring-session-data-redis</artifactId> 291 <version>2.0.2.RELEASE</version> 292 </dependency> 293 294 295 <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa --> 296 <dependency> 297 <groupId>org.springframework.data</groupId> 298 <artifactId>spring-data-jpa</artifactId> 299 <version>2.0.6.RELEASE</version> 300 </dependency> 301 302 <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core --> 303 <dependency> 304 <groupId>org.hibernate</groupId> 305 <artifactId>hibernate-core</artifactId> 306 <version>5.2.16.Final</version> 307 </dependency> 308 309 <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager --> 310 <dependency> 311 <groupId>org.hibernate</groupId> 312 <artifactId>hibernate-entitymanager</artifactId> 313 <version>5.2.16.Final</version> 314 </dependency> 315 316 <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator --> 317 <dependency> 318 <groupId>org.hibernate</groupId> 319 <artifactId>hibernate-validator</artifactId> 320 <version>6.0.9.Final</version> 321 </dependency> 322 323 <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> 324 <dependency> 325 <groupId>mysql</groupId> 326 <artifactId>mysql-connector-java</artifactId> 327 <version>5.1.6</version> 328 </dependency> 329 330 <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-mongodb --> 331 <dependency> 332 <groupId>org.springframework.data</groupId> 333 <artifactId>spring-data-mongodb</artifactId> 334 <version>2.0.6.RELEASE</version> 335 </dependency> 336 337 <!-- https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver --> 338 <dependency> 339 <groupId>org.mongodb</groupId> 340 <artifactId>mongo-java-driver</artifactId> 341 <version>3.6.3</version> 342 </dependency> 343 344 <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-mongodb --> 345 <dependency> 346 <groupId>org.springframework.data</groupId> 347 <artifactId>spring-data-redis</artifactId> 348 <version>2.0.6.RELEASE</version> 349 </dependency> 350 351 <!-- https://mvnrepository.com/artifact/redis.clients/jedis --> 352 <dependency> 353 <groupId>redis.clients</groupId> 354 <artifactId>jedis</artifactId> 355 <version>2.9.0</version> 356 </dependency> 357 358 359 <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient --> 360 <dependency> 361 <groupId>org.apache.httpcomponents</groupId> 362 <artifactId>httpclient</artifactId> 363 <version>4.5.5</version> 364 </dependency> 365 366 <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore --> 367 <dependency> 368 <groupId>org.apache.httpcomponents</groupId> 369 <artifactId>httpcore</artifactId> 370 <version>4.4.9</version> 371 </dependency> 372 373 <!-- https://mvnrepository.com/artifact/net.sf.ehcache/ehcache --> 374 <dependency> 375 <groupId>net.sf.ehcache</groupId> 376 <artifactId>ehcache</artifactId> 377 <version>2.10.5</version> 378 </dependency> 379 380 <!-- https://mvnrepository.com/artifact/com.corundumstudio.socketio/netty-socketio --> 381 <dependency> 382 <groupId>com.corundumstudio.socketio</groupId> 383 <artifactId>netty-socketio</artifactId> 384 <version>1.7.15</version> 385 </dependency> 386 387 <!-- https://mvnrepository.com/artifact/org.bytedeco.javacpp-presets/ffmpeg --> 388 <dependency> 389 <groupId>net.bramp.ffmpeg</groupId> 390 <artifactId>ffmpeg</artifactId> 391 <version>0.6.2</version> 392 </dependency> 393 394 <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --> 395 <dependency> 396 <groupId>org.apache.poi</groupId> 397 <artifactId>poi</artifactId> 398 <version>3.17</version> 399 </dependency> 400 401 <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml --> 402 <dependency> 403 <groupId>org.apache.poi</groupId> 404 <artifactId>poi-ooxml</artifactId> 405 <version>3.17</version> 406 </dependency> 407 408 <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-scratchpad --> 409 <dependency> 410 <groupId>org.apache.poi</groupId> 411 <artifactId>poi-scratchpad</artifactId> 412 <version>3.17</version> 413 </dependency> 414 415 </dependencies> 416 417 </project>web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 5 version="3.1"> 6 7 <!-- Session --> 8 <filter> 9 <filter-name>springSessionRepositoryFilter</filter-name> 10 <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 11 </filter> 12 <filter-mapping> 13 <filter-name>springSessionRepositoryFilter</filter-name> 14 <url-pattern>/*</url-pattern> 15 <dispatcher>REQUEST</dispatcher> 16 <dispatcher>ERROR</dispatcher> 17 </filter-mapping> 18 19 <servlet> 20 <servlet-name>dispatcher</servlet-name> 21 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 22 <!--<init-param>--> 23 <!--<param-name>contextConfigLocation</param-name>--> 24 <!--<param-value>/WEB-INF/applicationContext.xml</param-value>--> 25 <!--</init-param>--> 26 <load-on-startup>1</load-on-startup> 27 </servlet> 28 29 <servlet-mapping> 30 <servlet-name>dispatcher</servlet-name> 31 <url-pattern>/</url-pattern> 32 </servlet-mapping> 33 34 <filter> 35 <filter-name>encodingFilter</filter-name> 36 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 37 <init-param> 38 <param-name>encoding</param-name> 39 <param-value>UTF-8</param-value> 40 </init-param> 41 <init-param> 42 <param-name>forceEncoding</param-name> 43 <param-value>true</param-value> 44 </init-param> 45 </filter> 46 <filter-mapping> 47 <filter-name>encodingFilter</filter-name> 48 <url-pattern>/*</url-pattern> 49 </filter-mapping> 50 51 <context-param> 52 <param-name>contextConfigLocation</param-name> 53 <param-value>/WEB-INF/applicationContext.xml</param-value> 54 </context-param> 55 56 <context-param> 57 <param-name>log4jConfiguration</param-name> 58 <!-- 日志配置文件路徑,請根據具體項目自行調整 --> 59 <param-value>classpath:META-INF/log4j2.xml</param-value> 60 </context-param> 61 62 <listener> 63 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 64 </listener> 65 66 </web-app>applicationContext.xml
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" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mongo="http://www.springframework.org/schema/data/mongo" 6 xmlns:cache="http://www.springframework.org/schema/cache" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context-2.5.xsd 11 http://www.springframework.org/schema/data/mongo 12 http://www.springframework.org/schema/data/mongo/spring-mongo.xsd 13 http://www.springframework.org/schema/cache 14 http://www.springframework.org/schema/cache/spring-cache-3.1.xsd"> 15 16 <context:property-placeholder location="classpath*:META-INF/config.properties"/> 17 18 <!-- SpringData類型轉換器 --> 19 <!--<mongo:mapping-converter id="mongoConverter">--> 20 <!--<mongo:custom-converters>--> 21 <!--<mongo:converter>--> 22 <!--<bean class="com.jastar.demo.converter.TimestampConverter" />--> 23 <!--</mongo:converter>--> 24 <!--</mongo:custom-converters>--> 25 <!--</mongo:mapping-converter>--> 26 27 <mongo:mongo-client id="mongo" host="${mongo.host}" port="${mongo.port}"> 28 <mongo:client-options connections-per-host="${mongo.connectionsPerHost}" 29 threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}" 30 connect-timeout="${mongo.connectTimeout}" max-wait-time="${mongo.maxWaitTime}" 31 socket-keep-alive="${mongo.socketKeepAlive}" socket-timeout="${mongo.socketTimeout}"/> 32 </mongo:mongo-client> 33 <!-- mongo的工廠,通過它來取得mongo實例,dbname為mongodb的數據庫名,沒有的話會自動創建 --> 34 <mongo:db-factory id="mongoDbFactory" dbname="${mongo.dbname}" 35 mongo-ref="mongo"/> 36 37 <!-- mongodb的主要操作對象,所有對mongodb的增刪改查的操作都是通過它完成 --> 38 <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> 39 <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/> 40 <!--<constructor-arg name="mongoConverter" ref="mongoConverter" />--> 41 </bean> 42 43 <context:component-scan base-package="com.company.framework.dao"/> 44 45 <mongo:repositories base-package="com.company.framework.dao"/> 46 47 <!-- redis數據源 --> 48 <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> 49 <!-- 池中可借的最大數 --> 50 <property name="maxTotal" value="${redis.maxTotal}"/> 51 <!-- 允許池中空閑的最大連接數 --> 52 <property name="maxIdle" value="${redis.maxIdle}"/> 53 <!-- 允許池中空閑的最小連接數 --> 54 <property name="minIdle" value="${redis.minIdle}"/> 55 <!-- 獲取連接最大等待時間(毫秒) --> 56 <property name="maxWaitMillis" value="${redis.maxWaitMillis}"/> 57 <!-- 當maxActive到達最大數,獲取連接時的操作 是否阻塞等待 --> 58 <property name="blockWhenExhausted" value="${redis.blockWhenExhausted}"/> 59 <!-- 在獲取連接時,是否驗證有效性 --> 60 <property name="testOnBorrow" value="${redis.testOnBorrow}"/> 61 <!-- 在歸還連接時,是否驗證有效性 --> 62 <property name="testOnReturn" value="${redis.testOnReturn}"/> 63 <!-- 當連接空閑時,是否驗證有效性 --> 64 <property name="testWhileIdle" value="${redis.testWhileIdle}"/> 65 <!-- 設定間隔沒過多少毫秒進行一次后臺連接清理的行動 --> 66 <property name="timeBetweenEvictionRunsMillis" value="${redis.timeBetweenEvictionRunsMillis}"/> 67 <!-- 每次檢查的連接數 --> 68 <property name="numTestsPerEvictionRun" value="${redis.numTestsPerEvictionRun}"/> 69 </bean> 70 71 <!-- Spring-redis連接池管理工廠 --> 72 <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> 73 <!-- IP地址 --> 74 <property name="hostName" value="${redis.hostName}"/> 75 <!-- 端口號 --> 76 <property name="port" value="${redis.port}"/> 77 <!-- 超時時間 默認2000--> 78 <property name="timeout" value="${redis.timeout}"/> 79 <!-- 連接池配置引用 --> 80 <property name="poolConfig" ref="jedisPoolConfig" /> 81 <!-- usePool:是否使用連接池 --> 82 <property name="usePool" value="true"/> 83 </bean> 84 85 <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> 86 <property name="connectionFactory" ref="jedisConnectionFactory"/> 87 <property name="keySerializer"> 88 <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> 89 </property> 90 <property name="valueSerializer"> 91 <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> 92 </property> 93 <property name="hashKeySerializer"> 94 <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> 95 </property> 96 <property name="hashValueSerializer"> 97 <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> 98 </property> 99 <!--開啟事務 --> 100 <property name="enableTransactionSupport" value="true"></property> 101 </bean> 102 103 <bean id="redisHttpSessionConfiguration" 104 class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"/> 105 106 107 <cache:annotation-driven cache-manager="cacheManager" /> 108 109 <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> 110 <property name="cacheManager" ref="ehcache"></property> 111 </bean> 112 113 <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> 114 <property name="configLocation" value="classpath:META-INF/ehcache-setting.xml"></property> 115 </bean> 116 117 </beans>dispatcher-servlet.xml
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" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xmlns:jpa="http://www.springframework.org/schema/data/jpa" 7 xmlns:tx="http://www.springframework.org/schema/tx" 8 xsi:schemaLocation="http://www.springframework.org/schema/beans 9 http://www.springframework.org/schema/beans/spring-beans.xsd 10 http://www.springframework.org/schema/context 11 http://www.springframework.org/schema/context/spring-context.xsd 12 http://www.springframework.org/schema/mvc 13 http://www.springframework.org/schema/mvc/spring-mvc.xsd 14 http://www.springframework.org/schema/data/jpa 15 http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd 16 http://www.springframework.org/schema/tx 17 http://www.springframework.org/schema/tx/spring-tx.xsd"> 18 19 <context:component-scan base-package="com.company.framework.component"/> 20 21 <context:component-scan base-package="com.company.framework.service"/> 22 23 <!--指明 controller 所在包,并掃描其中的注解--> 24 <context:component-scan base-package="com.company.framework.controller"/> 25 26 <!-- 靜態資源(js、image等)的訪問 --> 27 <mvc:default-servlet-handler/> 28 29 <!-- 開啟注解 --> 30 <mvc:annotation-driven/> 31 32 <!--ViewResolver 視圖解析器--> 33 <!--用于支持Servlet、JSP視圖解析--> 34 <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 35 <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> 36 <property name="prefix" value="/WEB-INF/pages/"/> 37 <property name="suffix" value=".jsp"/> 38 </bean> 39 40 <!-- 表示JPA Repository所在的包 --> 41 <!--<jpa:repositories base-package="com.company.framework.dao2"/>--> 42 43 <!--<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">--> 44 <!--<property name="persistenceUnitName" value="defaultPersistenceUnit"/>--> 45 <!--</bean>--> 46 47 <!-- 事務管理 --> 48 <!--<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">--> 49 <!--<property name="entityManagerFactory" ref="entityManagerFactory"/>--> 50 <!--</bean>--> 51 52 <!--<tx:annotation-driven transaction-manager="transactionManager"/>--> 53 54 <!-- make spring look up annotation --> 55 <context:annotation-config/> 56 57 </beans>ehcache-setting.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <ehcache> 3 <!-- 指定一個文件目錄,當EhCache把數據寫到硬盤上時,將把數據寫到這個文件目錄下 --> 4 <diskStore path="java.io.tmpdir"/> 5 6 <!--name:緩存名稱--> 7 <!--maxElementsInMemory:內存中最大緩存對象數--> 8 <!--maxElementsOnDisk:硬盤中最大緩存對象數,若是0表示無窮大--> 9 <!--eternal:true表示對象永不過期,此時會忽略timeToIdleSeconds和timeToLiveSeconds屬性,默認為false--> 10 <!--overflowToDisk:true表示當內存緩存的對象數目達到了--> 11 <!--maxElementsInMemory界限后,會把溢出的對象寫到硬盤緩存中。注意:如果緩存的對象要寫入到硬盤中的話,則該對象必須實現了Serializable接口才行。--> 12 <!--diskSpoolBufferSizeMB:磁盤緩存區大小,默認為30MB。每個Cache都應該有自己的一個緩存區。--> 13 <!--diskPersistent:是否緩存虛擬機重啟期數據,是否持久化磁盤緩存,當這個屬性的值為true時,系統在初始化時會在磁盤中查找文件名 為cache名稱,后綴名為index的文件,這個文件中存放了已經持久化在磁盤中的cache的index,找到后會把cache加載到內存,要想把 cache真正持久化到磁盤,寫程序時注意執行net.sf.ehcache.Cache.put(Element element)后要調用flush()方法。--> 14 <!--diskExpiryThreadIntervalSeconds:磁盤失效線程運行時間間隔,默認為120秒--> 15 <!--timeToIdleSeconds: 設定允許對象處于空閑狀態的最長時間,以秒為單位。當對象自從最近一次被訪問后,如果處于空閑狀態的時間超過了timeToIdleSeconds屬性 值,這個對象就會過期,EHCache將把它從緩存中清空。只有當eternal屬性為false,該屬性才有效。如果該屬性值為0,則表示對象可以無限 期地處于空閑狀態--> 16 <!--timeToLiveSeconds:設定對象允許存在于緩存中的最長時間,以秒為單位。當對象自從被存放到緩存中后,如果處于緩存中的時間超過了 timeToLiveSeconds屬性值,這個對象就會過期,EHCache將把它從緩存中清除。只有當eternal屬性為false,該屬性才有 效。如果該屬性值為0,則表示對象可以無限期地存在于緩存中。timeToLiveSeconds必須大于timeToIdleSeconds屬性,才有 意義--> 17 <!--memoryStoreEvictionPolicy:當達到maxElementsInMemory限制時,Ehcache將會根據指定的策略去清理內存。可選策略有:LRU(最近最少使用,默認策略)、FIFO(先進先出)、LFU(最少訪問次數)。--> 18 19 <!-- 設定緩存的默認數據過期策略 --> 20 <defaultCache 21 maxElementsInMemory="10000" 22 eternal="false" 23 overflowToDisk="true" 24 timeToIdleSeconds="10" 25 timeToLiveSeconds="20" 26 diskPersistent="false" 27 diskExpiryThreadIntervalSeconds="120"/> 28 29 <cache name="cache" 30 maxElementsInMemory="10000" 31 eternal="false" 32 overflowToDisk="true" 33 timeToIdleSeconds="10" 34 timeToLiveSeconds="20"/> 35 36 </ehcache>log4j2.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!--日志級別以及優先級排序: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL --> 3 <!--Configuration后面的status,這個用于設置log4j2自身內部的信息輸出,可以不設置,當設置成trace時,你會看到log4j2內部各種詳細輸出--> 4 <!--monitorInterval:Log4j能夠自動檢測修改配置 文件和重新配置本身,設置間隔秒數--> 5 <configuration status="WARN" monitorInterval="30"> 6 <!--先定義所有的appender--> 7 <appenders> 8 <!--這個輸出控制臺的配置--> 9 <console name="Console" target="SYSTEM_OUT"> 10 <!--輸出日志的格式--> 11 <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/> 12 </console> 13 <!--文件會打印出所有信息,這個log每次運行程序會自動清空,由append屬性決定,這個也挺有用的,適合臨時測試用--> 14 <File name="log" fileName="log/test.log" append="false"> 15 <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/> 16 </File> 17 <!-- 這個會打印出所有的info及以下級別的信息,每次大小超過size,則這size大小的日志會自動存入按年份-月份建立的文件夾下面并進行壓縮,作為存檔--> 18 <RollingFile name="RollingFileInfo" fileName="${sys:user.home}/logs/info.log" 19 filePattern="${sys:user.home}/logs/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log"> 20 <!--控制臺只輸出level及以上級別的信息(onMatch),其他的直接拒絕(onMismatch)--> 21 <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/> 22 <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/> 23 <Policies> 24 <TimeBasedTriggeringPolicy/> 25 <SizeBasedTriggeringPolicy size="100 MB"/> 26 </Policies> 27 </RollingFile> 28 <RollingFile name="RollingFileWarn" fileName="${sys:user.home}/logs/warn.log" 29 filePattern="${sys:user.home}/logs/$${date:yyyy-MM}/warn-%d{yyyy-MM-dd}-%i.log"> 30 <ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/> 31 <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/> 32 <Policies> 33 <TimeBasedTriggeringPolicy/> 34 <SizeBasedTriggeringPolicy size="100 MB"/> 35 </Policies> 36 <!-- DefaultRolloverStrategy屬性如不設置,則默認為最多同一文件夾下7個文件,這里設置了20 --> 37 <DefaultRolloverStrategy max="20"/> 38 </RollingFile> 39 <RollingFile name="RollingFileError" fileName="${sys:user.home}/logs/error.log" 40 filePattern="${sys:user.home}/logs/$${date:yyyy-MM}/error-%d{yyyy-MM-dd}-%i.log"> 41 <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/> 42 <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/> 43 <Policies> 44 <TimeBasedTriggeringPolicy/> 45 <SizeBasedTriggeringPolicy size="100 MB"/> 46 </Policies> 47 </RollingFile> 48 </appenders> 49 <!--然后定義logger,只有定義了logger并引入的appender,appender才會生效--> 50 <loggers> 51 <!--過濾掉spring和mybatis的一些無用的DEBUG信息--> 52 <logger name="org.springframework" level="INFO"></logger> 53 <logger name="org.hibernate" level="INFO"></logger> 54 <logger name="org.mongodb.driver" level="INFO" /> 55 <logger name="org.mongodb.driver.cluster" level="OFF" /> 56 <root level="debug"> 57 <appender-ref ref="Console"/> 58 <appender-ref ref="RollingFileInfo"/> 59 <appender-ref ref="RollingFileWarn"/> 60 <appender-ref ref="RollingFileError"/> 61 </root> 62 </loggers> 63 </configuration>?config.properties
1 ###---The mongodb settings--- 2 mongo.dbname=admin 3 mongo.host=127.0.0.1 4 mongo.port=27017 5 mongo.connectionsPerHost=8 6 mongo.threadsAllowedToBlockForConnectionMultiplier=4 7 mongo.connectTimeout=1000 8 mongo.maxWaitTime=1500 9 mongo.autoConnectRetry=true 10 mongo.socketKeepAlive=true 11 mongo.socketTimeout=1500 12 mongo.slaveOk=true 13 mongo.writeNumber=1 14 mongo.writeTimeout=0 15 mongo.writeFsync=true 16 17 ###---The redis settings--- 18 redis.hostName=127.0.0.1 19 redis.port=6379 20 redis.password="" 21 redis.maxTotal=6000 22 redis.maxIdle=400 23 redis.minIdle=2 24 redis.maxWaitMillis=1000 25 redis.blockWhenExhausted=true 26 redis.testOnBorrow=true 27 redis.testOnReturn=true 28 redis.testWhileIdle=true 29 redis.timeBetweenEvictionRunsMillis=1800000 30 redis.numTestsPerEvictionRun=5 31 redis.timeout=100000 32 defaultCacheExpireTime=60?
轉載于:https://www.cnblogs.com/clockdotnet/p/JavaSpringProject.html
總結
以上是生活随笔為你收集整理的Java Spring 后端项目搭建的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Tp3.1 文件上传到七牛云
- 下一篇: 小米14 Pro屏幕细节曝光:华星光电供