028-applicationContext.xml配置文件
版本一
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd "> <!--1 读取database.properties文件 --> <context:property-placeholder location="classpath:database.properties"/> <!--2 准备druid连接池 --> <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driverClass}"></property> <property name="url" value="${jdbc.jdbcUrl}"></property> <property name="username" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- c3p0连接池 --> <!-- <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="jdbcurl" value="${jdbc.jdbcUrl}"></property> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> </bean> --> <!--3 将SessionFactory配置到spring容器中 --> <!-- 加载配置方案1:仍然使用外部的hibernate.cfg.xml配置信息 --> <!-- <bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> </bean> --> <!-- 加载配置方案2:在spring配置中放置hibernate配置信息 --> <bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <!-- 将连接池注入到sessionFactory, hibernate会通过连接池获得连接 --> <property name="dataSource" ref="dataSource"></property> <!-- 配置hibernate基本信息 --> <property name="hibernateProperties"> <props> <!-- 必选配置 --> <!-- <prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop> <prop key="hibernate.connection.url">jdbc:mysql:///hibernate</prop> <prop key="hibernate.connection.username">root</prop> <prop key="hibernate.connection.password">root</prop> --> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <!-- 可选配置 --> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <!-- 引入orm元数据,指定orm元数据所在的包路径,spring会自动读取包中的所有配置 --> <property name="mappingDirectoryLocations" value="classpath:www/test/domain"></property> <!-- 使用maven工厂的时候因为domain会打包成一个jar配置目录就不好使了,需要使用下面的配置 --> <!-- <property name="mappingLocations"> <list> <value>classpath:www/test/domain/*.xml</value> </list> </property> --> </bean> <!--4 核心事务管理器 依赖于SessionFactory--> <bean name="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!--5 配置通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/> <tx:method name="persist*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/> <tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/> <tx:method name="modify*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/> <tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/> <tx:method name="remove*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/> <tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/> <tx:method name="get*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/> <tx:method name="*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/> </tx:attributes> </tx:advice> <!--6 配置将通知织入目标对象 配置切点+配置切面 --> <aop:config> <aop:pointcut expression="execution(* www.test.service.impl.*ServiceImpl.*(..))" id="txPc"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPc"/> </aop:config> <!--7 action --> <bean name="userAction" class="www.test.web.action.UserAction" scope="prototype"> <property name="us" ref="userService"></property> </bean> <bean name="customerAction" class="www.test.web.action.CustomerAction" scope="prototype"> <property name="cs" ref="customerService"></property> </bean> <bean name="baseDictAction" class="www.test.web.action.BaseDictAction" scope="prototype"> <property name="baseDictService" ref="baseDictService"></property> </bean> <bean name="linkManAction" class="www.test.web.action.LinkManAction" scope="prototype"> <property name="lms" ref="linkManService"></property> <property name="cs" ref="customerService"></property> </bean> <bean name="saleVisitAction" class="www.test.web.action.SaleVisitAction" scope="prototype"> <property name="svs" ref="saleVisitService"></property> </bean> <!--8 service --> <bean name="userService" class="www.test.service.impl.UserServiceImpl"> <property name="ud" ref="userDao"></property> </bean> <bean name="customerService" class="www.test.service.impl.CustomerServiceImpl"> <property name="cd" ref="customerDao"></property> </bean> <bean name="baseDictService" class="www.test.service.impl.BaseDictServiceImpl"> <property name="baseDictDao" ref="baseDictDao"></property> </bean> <bean name="linkManService" class="www.test.service.impl.LinkManServiceImpl"> <property name="lmd" ref="linkManDao"></property> </bean> <bean name="saleVisitService" class="www.test.service.impl.SaleVisitServiceImpl"> <property name="svd" ref="saleVisitDao"></property> </bean> <!--9 dao --> <bean name="userDao" class="www.test.dao.impl.UserDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean name="customerDao" class="www.test.dao.impl.CustomerDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean name="baseDictDao" class="www.test.dao.impl.BaseDictDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean name="linkManDao" class="www.test.dao.impl.LinkManDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean name="saleVisitDao" class="www.test.dao.impl.SaleVisitDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> </beans>
版本二 bos
<?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:tx="http://www.springframework.org/schema/tx" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:soap="http://cxf.apache.org/bindings/soap" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd "> <!-- 1 加载属性文件 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 2 配置数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}"/> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"/> <property name="user" value="${jdbc.user}"/> <property name="password" value="${jdbc.password}"/> </bean> <!--3 配置LocalSessionFactoryBean,spring提供的用于整合hibernate的工厂bean --> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 注入hibernate相关的属性配置 --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> <!-- 注入hibernate的映射文件 --> <property name="mappingLocations"> <list> <value>classpath:com/test/bos/domain/*.xml</value> </list> </property> <!--这种方式注入hibernate的映射文件在maven开发中是不可以的,domain是要打成jar包的 --> <!-- <property name="mappingDirectoryLocations" value="classpath:www/test/domain"></property> --> </bean> <!-- 4 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- 5 组件扫描 --> <context:component-scan base-package="com.test.bos"/> <!-- 6 支持spring注解 --> <context:annotation-config /> <!-- 7 注解事务 --> <tx:annotation-driven/> <!-- 注册crm客户端代理对象 --> <jaxws:client id="crmClient" address="http://localhost:8080/bos-crm/service/customer" serviceClass="com.test.bos.utils.cxf.ICustomerService"> </jaxws:client> <!--配置shiro框架的过滤器工厂对象 --> <bean name="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager"></property> <property name="loginUrl" value="/login.jsp"></property> <property name="successUrl" value="/index.jsp"></property> <property name="unauthorizedUrl" value="/unauthorized.jsp"></property> <!-- 指定url级别的拦截策略 --> <property name="filterChainDefinitions"> <value> /css/** = anon /js/** = anon /images/** = anon /login.jsp* = anon /validatecode.jsp* = anon /UserAction_login.action = anon /page_base_staff.action = perms["staff"] /* = authc </value> </property> </bean> <!-- 注册安全管理器对象 --> <bean name="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realm" ref="bosRealm"></property> <!--注入缓存管理器 --> <property name="cacheManager" ref="cacheManager"></property> </bean> <!--注册缓存管理器 --> <bean name="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"> <!-- 注册ehcache的配置文件 --> <property name="cacheManagerConfigFile" value="classpath:ehcache.xml"></property> </bean> <!-- 注册自定义realm --> <bean name="bosRealm" class="com.test.bos.service.realm.BOSRealm"></bean> <!-- <property name="proxyTargetClass" value="false"/>其中 value="false" 会出现 NoSuchMethodException: com.sun.proxy.$Proxy86.pageQuery 异常, 为 false 的时候, 使用的 jdk 代理, JDK 代理也就是实现一个接口, 实现的 ModelDriven<T>接口。 间接实现的, 因为 StaffAction 继承了 BaseAction,BaseAction 又实现了 ModelDriven 接口, 而 ModelDriven 里面只有一个 getModel 方法, 根本没有 pageQuery 方法。 所以必须使用 cglib 代理方式。 因为使用 cglib 创建代理会继承 StaffAction,所以就肯定能有 pageQuery 方法。 所以这里需要强制使用 cglib 代理。 --> <!--开启 shiro 框架注解支持--> <bean name="defaultAdvisorAutoProxyCreator" class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"> <!-- 必须使用 cglib 方式为 Action 对象创建代理对象 --> <property name="proxyTargetClass" value="true"></property> </bean> <!-- 配置 shiro 框架提供的切面类, 用于创建代理对象 --> <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"></bean> <!-- quartz配置-1 注册自定义作业类 --> <bean name="myJob" class="com.test.jobs.MailJob"> <property name="username" value="luojun281@126.com"/> <property name="password" value="123456ljp"/> <property name="smtpServer" value="smtp.126.com"/> <property name="protocol" value="SMTP"/> </bean> <!-- quartz配置-2 配置JobDetail --> <bean name="myJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!-- 注入目标对象 --> <property name="targetObject" ref="myJob"></property> <!-- 注入目标方法 --> <property name="targetMethod" value="execute"></property> </bean> <!--quartz配置-3 配置触发器Trigger --> <bean name="myTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <!-- 注入任务详情对象 --> <property name="jobDetail" ref="myJobDetail"></property> <!-- 注入cron表达式,通过这个表达式指定触发的时间点 --> <property name="cronExpression"> <!-- 每过两分钟发送一次 --> <value>0 0/2 * * * ? 2020-2099</value> </property> </bean> <!--quartz配置-4 配置scheduler调度工厂--> <bean name="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <!-- 注入触发器 --> <property name="triggers"> <list> <ref bean="myTrigger"/> </list> </property> </bean> </beans>
版本三 Mybatis-Spring
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 1加载配置文件 --> <context:property-placeholder location="classpath:db.properties" /> <!-- 2配置连接池 --> <bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="maxActive" value="10" /> <property name="maxIdle" value="5" /> </bean> <!--3 配置SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 配置mybatis核心配置文件 --> <property name="configLocation" value="classpath:SqlMapConfig.xml" /> <!-- 配置数据源 --> <property name="dataSource" ref="dataSource" /> </bean> <!-- 原始开发方式中,配置dao到spring中 --> <bean name="userDao" class="com.mybatis.spring.dao.impl.UserDaoImpl"> <!-- 注入SqlSessionFatory --> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> <!-- Mapper代理的方式开发方式一,配置Mapper代理对象 --> <bean name="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <!-- 配置mapper接口 --> <property name="mapperInterface" value="com.mybatis.spring.mapper.IUserMapper"></property> <!-- 配置sqlSessionFactory --> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> <!-- Mapper代理的方式开发方式二,扫描包方式配置代理 每个mapper代理对象的id就是类名,首字母小写。 om.mybatis.spring.mapper下面就算还有子包,也会扫描到的。 --> <!-- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.mybatis.spring.mapper"></property> </bean> --> </beans>