Java Spring 后端项目搭建
Posted ClockDotNet
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
以上是关于Java Spring 后端项目搭建的主要内容,如果未能解决你的问题,请参考以下文章
spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段
如何使用 Keycloak 保护 Angular 8 前端和使用网关、eureka 的 Java Spring Cloud 微服务后端