自己也开始做了项目,但只是跟着学习,感觉学起来很乱,因此有必要进行一些小结了!
一、WEB-INF --> web.xml中的配置
1.Spring的配置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
2.Struts2的配置
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern> *.action</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
3.WebService利用CXF的框架配置
<servlet>
<servlet-name>CXFService</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFService</servlet-name>
<url-pattern>/services/**/</url-pattern>
</servlet-mapping>
4.利用Shiro做权限校验的配置
<!-- shiro的Filter -->
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/**/</url-pattern>
</filter-mapping>
5.Hibernate引起的延迟加载解决为题配置
<!-- OpenEntityManagerInView -->
<filter>
<filter-name>OpenEntityManagerInView</filter-name>
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenEntityManagerInView</filter-name>
<url-pattern>/**/</url-pattern>
</filter-mapping>
6.解决编码问题的配置
<!-- 解决控制台webservice Encoding:ISO-8859-1 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/**/</url-pattern>
</filter-mapping>
二、applicationContext.xml中的配置
1.常用的名称空间
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/context
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/data/jpa
(beans 、 aop 、 context) Spring的核心 (jdbc 、tx 、data-jpa)整合数据库必须的
表现层的配置在web.xml中,持久层的配置在applicationContext中
2.申明项目中归Spring容器管理的类(DI)
<context:component-scan base-package="com.vinci"/>
3.申明启动AOP
<aop:aspectj-autoproxy/>
4.加载工程的配置文件(一般是连接数据的配置文件)
<context:property-placeholder location="classpath:config.properties" />
对applicationContext.xml中的配置文件按类别单独配置文件,如下
<import resource="applicationContext-dataSource.xml"/>
<!-- 引入WebService配置 -->
<import resource="applicationContext-webService.xml"/>
<!-- 引入quartz配置 -->
<import resource="applicationContext-quartz.xml"/>
<!-- 引入 mq配置 -->
<import resource="applicationContext-mq-producer.xml"/>
<import resource="applicationContext-mq-consumer.xml"/>
<!-- 引入 elasticsearch 配置 -->
<import resource="applicationContext-elasticsearch.xml"/>
<!-- 引入 shiro权限控制配置 -->
<import resource="applicationContext-shiro.xml"/>
<!-- 引入cache 配置 -->
<import resource="applicationContext-encache.xml"/>
三、就第二部分applicationContext.xml引入其他配置文件做详解
这里涉及到
1)连接数据库的dataSource
2)系统间通信的WebService
3)定时调度框架quartz
4)消息队列mq
5)全文索引搜索elasticSearch
6)权限控制shiro
7)缓存技术ehcache
在这里,你把自己想象成一个公司职员,而上面的7个技术都是安装在公司服务器软件(这里的说法也不正确,插件、中间件、数据平台)。
所以,现在就很清楚了:①在你编写java代码中,你要连接到公司的服务器上,所以需要连接IP。②你需要驱动jar包来
使你的java程序能操作这些技术(服务器软件)。
现在一个一个讲解其配置
1. applicationContext-dataSource(数据平台,需要注意dao包和domain包)
你会发现配置顺序为
1.1 连接池 (c3p0)
1.2 实体类 实体类管理工厂(需要c3p0,需要扫描实体类domain所在包)
1.3 JPA事务 管理器(需要实体类管理工厂)
1.4 注解 驱动事务
1.5 springDataJPA 对dao层中的类整合
<beans ....xmlns(第二部分的六个名称空间极其约束beans、context、aop、jdbc、tx、jpa-data)>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.vinci.domain" />
........(为突出结构,将具体内容放在下面)
</bean>
<!-- JPA事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" >
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- 注解管理事务 -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
<!-- 整合spring data jpa -->
<jpa:repositories base-package="com.vinci.dao" />
</beans>
|-------------------------------------------------------------------------------------------------------------------|
| <!-- 整合JPA配置 -->
| <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> |
| <property name="dataSource" ref="dataSource" /> |
| <property name="packagesToScan" value="com.vinci.domain" /> |
| <!-- 指定一个持久化提供者 --> |
| <property name="persistenceProvider">
| <bean class="org.hibernate.jpa.HibernatePersistenceProvider" /> |
| </property>
| <!-- 指定Jpa持久化实现厂商类,这里以Hibernate为例 -->
| <property name="jpaVendorAdapter">
| <!-- Hibernate对Jpa的实现 -->
| <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> |
| <property name="generateDdl" value="true" />
| <property name="database" value="ORACLE" />
| <!--<property name="database" value="MYSQL" /> -->
| <!-- <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" /> --> |
| <property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect" /> |
| <property name="showSql" value="true" /> |
| </bean> |
| </property>
| <property name="jpaDialect">
| <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" /> |
| </property>
| <property name="jpaPropertyMap">
| <map>
| <entry key="hibernate.query.substitutions" value="true 1, false 0" /> |
| <entry key="hibernate.default_batch_fetch_size" value="16" /> |
| <entry key="hibernate.max_fetch_depth" value="2" />
| <entry key="hibernate.generate_statistics" value="true" /> |
| <entry key="hibernate.bytecode.use_reflection_optimizer" value="true" /> |
| <entry key="hibernate.cache.use_second_level_cache" value="false" />
| <entry key="hibernate.cache.use_query_cache" value="false" /> |
| </map>
| </property> |
| </bean> |
|-------------------------------------------------------------------------------------------------------------------
2. applicationContex-webService(跨系统访问)
这里基于jaxrs restful风格进行的系统通信,这时候说明你编写的代码工程将对外提供访问资源。
①并且已经在web.xml中配置了关于CXFservice的servlet。
②配置中的address声明了访问路径。(可以配置多个服务的如 /eatService /sleepService)
<jaxrs:server id="customerService" address="/customerService">
<jaxrs:serviceBeans>
<bean class="com.vinci.crm.service.impl.CustomerServiceImpl" />
</jaxrs:serviceBeans>
<jaxrs:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
</jaxrs:inInterceptors>
<jaxrs:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
</jaxrs:outInterceptors>
</jaxrs:server>
3. applicationContex-quartz(定时插件)
quartz的三个组成 Job Trigger Scheduler
<bean id="helloJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="helloBean"></property>
<property name="targetMethod" value="updateStatus"></property>
</bean>
<bean id="helloTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="helloJob" />
<property name="cronExpression" value="*/5 * * ? * *"></property>
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="helloTrigger" />
</list>
</property>
</bean>
4. applictionContext-mq (中间件)
mq属于消息队列,为生产者消费者模型 producer-consumer
mq ---- jms --- jmsTemplate
4.1 mq-producer 配置文件
<bean id="amqConnectionFactory"
class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616"></property>
<property name="userName" value="admin"></property>
<property name="password" value="admin"></property>
</bean>
<!-- Spring整合mq -->
<bean id="mqConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<property name="targetConnectionFactory" ref="amqConnectionFactory"></property>
<property name="sessionCacheSize" value="100" />
</bean>
<!-- 定义JmsTemplate的Queue类型 队列模式-->
<bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
<constructor-arg ref="mqConnectionFactory" />
<property name="pubSubDomain" value="false" />
</bean>
4.2 mq-consumer 配置文件
destination的值为生产者发送消息的名字,ref值为定义的处理消息的类的注解名
<bean id="amqConnectionFactory"
class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616"></property>
<property name="userName" value="admin"></property>
<property name="password" value="admin"></property>
</bean>
<bean id="mqConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<property name="targetConnectionFactory" ref="amqConnectionFactory"></property>
<property name="sessionCacheSize" value="100" />
</bean>
<jms:listener-container destination-type="queue" container-type="default"
connection-factory="connectionFactory" acknowledge="auto">
<jms:listener destination="helloworld" ref="helloWorld"/>
</jms:listener-container>
5. applicationContext-elasticSearch (数据平台,需要连接索引库)
<!-- 搜索DAO 扫描 -->
<elasticsearch:repositories base-package="com.vinci.index" />
<!-- 配置Client -->
<elasticsearch:transport-client id="client" cluster-nodes="127.0.0.1:9300"/>
<!-- 配置搜索模板 -->
<bean id="elasticsearchTemplate"
class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
<constructor-arg name="client" ref="client" />
</bean>
6. applicationContext-shiro(插件)
<bean id="shiroFilter"
class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!-- 安全管理器 -->
<property name="securityManager" ref="securityManager" />
<!-- 未认证,跳转到哪个页面 -->
<property name="loginUrl" value="/login.html" />
<!-- 登录成功跳转页面 -->
<property name="successUrl" value="/index.html" />
<!-- 认证后,没有权限跳转页面 -->
<property name="unauthorizedUrl" value="/unauthorized.html" />
<!-- shiro URL控制过滤器规则 -->
<property name="filterChainDefinitions">
<value>
/login.html* = anon
/css/**/ = anon
/js/**/ = anon
/images/**/ = anon
/validatecode.jsp* = anon
/services/**/ = anon
/user_login.action* = anon
/**/ = authc
</value>
</property>
</bean>
<!-- 安全管理器 -->
<bean id="securityManager"
class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="bosRealm" />
<property name="cacheManager" ref="shiroCacheManager" />
</bean>
<!-- 配置Realm -->
<bean id="bosRealm" class="com.vinci.realm.BosRealm">
<!-- 缓存区的名字 就是 ehcache.xml 自定义 cache的name -->
<property name="authorizationCacheName" value="bos" />
</bean>
<!-- shiro封装cacheManager -->
<bean id="shiroCacheManager"
class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManager" ref="ehCacheManager" />
</bean>
<!-- Shiro生命周期处理器 -->
<bean id="lifecycleBeanPostProcessor"
class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
<!-- 开启shiro注解模式 -->
<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>
7. applicationContext-apache(缓存平台)
<!-- 缓存配置 -->
<bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml" />
</bean>
<!-- spring 封装ehcache缓存管理器 -->
<bean id="springCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehCacheManager" />
</bean>
<!-- 激活spring 缓存注解 -->
<cache:annotation-driven cache-manager="springCacheManager"/>