spring boot推荐使用注解方式,不过个人对数据源等固定配置仍然倾向于xml来做。总的来说,注解和xml各有优势,具体采用哪种取决于实际应用场景。
一) spring boot 入口类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
@ComponentScan(basePackages = "com.magic.acme.assist")
@ImportResource({ "classpath:META-INF/spring/applicationContext*.xml" })
public class AssistApplication {
public static void main(String[] args) {
SpringApplication.run(AssistApplication.class, args);
}
}
二)properties配置参数
## DB properties:
spring.datasource.kit.url=jdbc:postgresql://192.168.3.168:5432/kit
spring.datasource.kit.username=jetty
spring.datasource.kit.password=jetty
spring.datasource.kit.driverClassName=org.postgresql.Driver
spring.datasource.kit.initialSize=10
spring.datasource.kit.minIdle=10
spring.datasource.kit.maxActive=100
spring.datasource.kit.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.datasource.xtt.url=jdbc:postgresql://192.168.3.168:5432/otmsxtt
spring.datasource.xtt.username=jetty
spring.datasource.xtt.password=jetty
spring.datasource.xtt.driverClassName=org.postgresql.Driver
spring.datasource.xtt.initialSize=10
spring.datasource.xtt.minIdle=10
spring.datasource.xtt.maxActive=100
spring.datasource.xtt.dialect=org.hibernate.dialect.PostgreSQLDialect
三)applicationContext.xml 基础配置
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!--load properties-->
<context:property-placeholder location="classpath:META-INF/spring/application.properties" file-encoding="UTF-8"/>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="defaultEncoding" value="UTF-8"></property>
<property name="basenames">
<list>
<value>META-INF/locale/message</value>
</list>
</property>
</bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="cookieMaxAge" value="604800"/>
<property name="defaultLocale" value="zh_CN"/>
<property name="cookieName" value="language"></property>
</bean>
<!--数据绑定支持, 如@Valid支持 -->
<mvc:annotation-driven validator="validator">
</mvc:annotation-driven>
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="providerClass" value="org.hibernate.validator.HibernateValidator"/>
<property name="validationMessageSource" ref="messageSource"/>
</bean>
</beans>
四)applicationContext_datasource.xml数据源配置 (多数据源时必须指定一个首选项 primary="true")
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--指定实现JPA的适配器-->
<bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></bean>
<!--DataSource kit-->
<bean id="dataSourceKit" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close" primary="true">
<property name="driverClassName" value="${spring.datasource.kit.driverClassName}"/>
<property name="url" value="${spring.datasource.kit.url}"/>
<property name="username" value="${spring.datasource.kit.username}"/>
<property name="password" value="${spring.datasource.kit.password}"/>
<property name="initialSize" value="${spring.datasource.kit.initialSize}"/>
<property name="maxActive" value="${spring.datasource.kit.maxActive}"/>
<property name="minIdle" value="${spring.datasource.kit.minIdle}"/>
</bean>
<bean id="entityManagerFactoryKit" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
primary="true">
<property name="dataSource" ref="dataSourceKit"/>
<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">${spring.datasource.kit.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">none</prop>
<prop key="hibernate.autoReconnect">true</prop>
</props>
</property>
<property name="packagesToScan" value="com.magic.acme.assist.jpa.kit.entity"/>
</bean>
<bean id="transactionManagerKit" class="org.springframework.orm.jpa.JpaTransactionManager" primary="true">
<property name="entityManagerFactory" ref="entityManagerFactoryKit"></property>
</bean>
<!--DataSource xtt-->
<bean id="dataSourceXtt" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
<property name="driverClassName" value="${spring.datasource.xtt.driverClassName}"/>
<property name="url" value="${spring.datasource.xtt.url}"/>
<property name="username" value="${spring.datasource.xtt.username}"/>
<property name="password" value="${spring.datasource.xtt.password}"/>
<property name="initialSize" value="${spring.datasource.xtt.initialSize}"/>
<property name="maxActive" value="${spring.datasource.xtt.maxActive}"/>
<property name="minIdle" value="${spring.datasource.xtt.minIdle}"/>
</bean>
<bean id="entityManagerFactoryXtt" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSourceXtt"/>
<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">${spring.datasource.xtt.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">none</prop>
<prop key="hibernate.autoReconnect">true</prop>
</props>
</property>
<property name="packagesToScan" value="com.magic.acme.assist.jpa.xtt.entity"/>
</bean>
<bean id="transactionManagerXtt" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactoryXtt"></property>
</bean>
<!--JPA reponsitories-->
<jpa:repositories base-package="com.magic.acme.assist.jpa.kit.repository"
entity-manager-factory-ref="entityManagerFactoryKit"
transaction-manager-ref="transactionManagerKit"></jpa:repositories>
<jpa:repositories base-package="com.magic.acme.assist.jpa.xtt.repository"
entity-manager-factory-ref="entityManagerFactoryXtt"
transaction-manager-ref="transactionManagerXtt"></jpa:repositories>
<!-- 启用 annotation事务-->
<tx:annotation-driven transaction-manager="transactionManagerKit"/>
<tx:annotation-driven transaction-manager="transactionManagerXtt"/>
</beans>
五)log4j2配置
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="error">
<!-- 先定义所有的appender -->
<appenders>
<!--这个输出控制台的配置 -->
<Console name="Console" target="SYSTEM_OUT">
<!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch) -->
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
<!--这个都知道是输出日志的格式 -->
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
</Console>
<Console name="SQLConsole" target="SYSTEM_OUT">
<!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch) -->
<ThresholdFilter level="TRACE" onMatch="ACCEPT" onMismatch="DENY"/>
<!--这个都知道是输出日志的格式 -->
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
</Console>
<!--文件会打印出所有信息,这个log每次运行程序会自动清空,由append属性决定,这个也挺有用的,适合临时测试用 -->
<!--append为TRUE表示消息增加到指定文件中,false表示消息覆盖指定的文件内容,默认值是true -->
<File name="log" fileName="log/test.log" append="false">
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
</File>
<!-- 添加过滤器ThresholdFilter,可以有选择的输出某个级别以上的类别 onMatch="ACCEPT" onMismatch="DENY"意思是匹配就接受,否则直接拒绝 -->
<File name="ERROR" fileName="logs/error.log">
<ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="%d{yyyy.MM.dd 'at' HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n"/>
</File>
<!--这个会打印出所有的信息,每次大小超过size,则这size大小的日志会自动存入按年份-月份建立的文件夹下面并进行压缩,作为存档 -->
<RollingFile name="RollingFile" fileName="logs/web.log" filePattern="logs/$${date:yyyy-MM}/%d{yyyy-MM-dd}-%i.log.gz">
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n"/>
<SizeBasedTriggeringPolicy size="10MB"/>
</RollingFile>
</appenders>
<!-- 然后定义logger,只有定义了logger并引入的appender,appender才会生效 -->
<loggers>
<logger name="org.hibernate.type.descriptor.sql.BasicBinder" level = "INFO">
<appender-ref ref="SQLConsole"/>
</logger>
<!--建立一个默认的root的logger -->
<root level="INFO">
<!--<appender-ref ref="RollingFile"/>-->
<appender-ref ref="Console"/>
<appender-ref ref="ERROR"/>
<!--<appender-ref ref="log"/>-->
</root>
</loggers>
</configuration>
六)pom.xml - maven
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.magic.acme</groupId>
<artifactId>assist</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
<relativePath/>
</parent>
<properties>
<postgres.version>8.4-702.jdbc4</postgres.version>
<fastjson.version>1.2.16</fastjson.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgres.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>