1.xml配置web
1.1 springIoC配置redis
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:task="http://www.springframework.org/schema/task" xmlns:redis="http://www.springframework.org/schema/redis" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/redis http://www.springframework.org/schema/redis/spring-redis-1.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- Spring IOC容器 掃描 base-package 註解:@Repository @Service @Controller @Component--> <context:component-scan base-package="com.wise.tiger" use-default-filters="true"> <!-- exclude-filter是針對include-filter裏的內容進行排除 --> <context:exclude-filter type="annotation" expression="com.wise.tiger.controller" /> </context:component-scan> <!-- 開啓AOP自動代理 --> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> <!--添加DataSource的配置文件路徑--> <context:property-placeholder location="classpath:pool-config.properties"/> <!--向IoC容器注入DataSource--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName" value="${driver}" /> <property name="url" value="${url}" /> <property name="username" value="${user}" /> <property name="password" value="${password}" /> <property name="initialSize" value="5" /> <property name="minIdle" value="3" /> <property name="maxActive" value="10" /> <property name="maxWait" value="10000" /> <!-- 配置間隔多久才進行一次檢測,檢測須要關閉的空閒鏈接,單位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 配置一個鏈接在池中最小生存的時間,單位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="300000" /> <property name="testWhileIdle" value="true" /> <!-- 這裏建議配置爲TRUE,防止取到的鏈接不可用 --> <property name="testOnBorrow" value="true" /> <property name="testOnReturn" value="false" /> <!-- 打開PSCache,而且指定每一個鏈接上PSCache的大小 --> <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> <!-- 這裏配置提交方式,默認就是TRUE,能夠不用配置 --> <property name="defaultAutoCommit" value="fasle" /> <!-- 配置監控統計攔截的filters --> <property name="filters" value="stat" /> </bean> <!--2 Spring JDBC模版 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 3 Spring JPA整合JPA核心配置文件,獲得EntityManagerFactory事務管理--> <bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> </bean> <!-- 定義實體管理器工廠 Jpa配置 LocalContainerEntityManagerFactoryBean這個選項Spring扮演了容器的角色。徹底掌管JPA --> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <!-- 指定數據源 --> <property name="dataSource" ref="dataSource"/> <!-- 指定Jpa持久化實現廠商類,這裏以Hibernate爲例 --> <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/> <!-- 指定Entity實體類包路徑 --> <property name="packagesToScan" value="com.wise.tiger.domain"></property> <!-- 指定JPA屬性;如Hibernate中指定是否顯示SQL的是否顯示、方言等 --> <property name="jpaProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</prop> <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> </bean> <!-- 4事務管理 --> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory"></property> </bean> <!--XML配置事務聲明方式 開啓註解聲明事務 --> <tx:annotation-driven transaction-manager="transactionManager" /> <!-- 5最重要配置:啓用掃描並自動建立代理的功能(spring-data-jpa)--> <jpa:repositories base-package="com.wise.tiger.repository" entityManagerFactoryRef="entityManagerFactory", transactionManagerRef="txManager" /> </beans>
1.2 springMVC配置spring
<?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:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--開啓mvc註解驅動--> <mvc:annotation-driven/> <!--mvc掃描的包--> <context:component-scan base-package="com.wise.tiger.controller"/> <!--配置視圖解析器--> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/"/> <property name="suffix" value=".jsp"/> </bean> </beans>