hibernate4沒法保存數據css
author: hiuhtml
之後都發文章我都備註一下做者了,hiu就是我了java
紅色字體更新日期:2014-07-08mysql
初次使用hibernate4,使用getCurrentSession保存對象時沒法將對象的數據保存進數據庫,通過一番試驗後,發現原來要配置事務才幹保存數據。web
保存數據失敗緣由:spring
沒有配置事務,經過手動寫一個事務,才幹提交數據。手動寫一個事務,用getCurrentSession也沒法保存數據,僅僅能使用openSession才幹保存數據。sql
解決的方法:數據庫
配置spring聲明式事務,不建議使用註解來配置事務,註解配置事務,僅僅在xml配置切面事務失敗時,才用來測試。通常xml配置切面事務失敗緣由,找不到包,還有切面路徑找不到類。今天(2014-07-08)在保存數據時發現,假設作一對多的表保存數據的話,即便xml配置切面事務的切面不對,也能使用getCurrentSession保存數據,但事務就沒法回滾了:express
spring-hibernate.xmlapache
<aop:config> <!--我在Service前面加了1234,目的:不讓匹配到相應的類開啓事務--> <aop:pointcut id="transactionPointcut" expression="execution(* com..service.*1234Service.*(..))" /> <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" /> </aop:config>
service層代碼:
public EmployeeVo save(EmployeeVo employeeVo, Map<String, String> pageParam) throws SQLException { Employee employee =new Employee(); String keyjobno=employeeVo.getCompany().trim()+employeeVo.getJobno().trim(); //employee表 employeeVo.setKeyjobno(keyjobno.trim());//主鍵,公司+工號 employeeVo.setIfout("在會"); BeanUtils.copyProperties(employeeVo, employee); //in_union_his表 InUnionHis inunion=new InUnionHis(); BeanUtils.copyProperties(employeeVo, inunion); inunion.setEmployee(employee); Set<InUnionHis> inUnionHises = new HashSet<InUnionHis>(); inUnionHises.add(inunion); employee.setInUnionHises(inUnionHises); employeeDao.save(employee); return employeeVo; }
2014-07-08 18:33:46 [org.hibernate.SQL]-[DEBUG] insert into union_ssh.employee (bornboon, borndate, company, createdate, dept, getborndate, getmarrydate, id, identitycard, ifout, interest, jobno, lodging, marry, marryboon, name, nativename, operator, phone, politicsface, sex, keyjobno) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) Hibernate: insert into union_ssh.employee (bornboon, borndate, company, createdate, dept, getborndate, getmarrydate, id, identitycard, ifout, interest, jobno, lodging, marry, marryboon, name, nativename, operator, phone, politicsface, sex, keyjobno) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) 2014-07-08 18:33:46 [org.hibernate.engine.spi.ActionQueue]-[DEBUG] Executing identity-insert immediately 2014-07-08 18:33:46 [org.hibernate.SQL]-[DEBUG] insert into union_ssh.in_union_his (createdate, keyjobno, inuniondate, operator) values (?, ?, ?, ?) Hibernate: insert into union_ssh.in_union_his (createdate, keyjobno, inuniondate, operator) values (?, ?, ?, ?) 2014-07-08 18:33:46 [org.hibernate.id.IdentifierGeneratorHelper]-[DEBUG] Natively generated identity: 81 2014-07-08 18:33:46 [org.springframework.security.web.context.HttpSessionSecurityContextRepository]-[DEB
getCurrentSession保存數據是沒法運行的,在控制檯也沒有不論什麼的sql語句打印出來的。
說明 :
一、要注意,在web.xml配置openSessionInViewFilter用來啓動hibernate,在通過url請求後,這裏就會開啓hibernate的session,假設不配置,就沒法使用getCurrentSession了,具體配置,請看如下web.xml的配置,
二、如下的代碼是用AOP配置的事務,註解方式的事務我已經凝視掉了,請看UserService裏面凝視掉的事務註解
三、dao和service我都是用註解注入的
四、如下的幾個xml文件除了applicationContext-security.xml這個配置文件外,其餘的幾個xml文件都是參照easyUI教程中的孫宇老師的視頻配置的,有興趣的朋友可下載下來看看,比較適合剛入門的朋友
五、項目是用maven搭建的,比較方便的,不用再去找jar包,僅僅要在pom.xml文件配置,在鏈接網絡的前提下就可以本身主動下載jar包了,可以看看如下的pom.xml文件裏,我下載了那些jar包
六、因爲我在spring-hibernate.xml文件配置了<prop key="hibernate.show_sql">${hibernate.show_sql}</prop> ,這個值爲true時,假設運行了sql語句,就會在控制檯打印出sql語句的,我在測試時,假設數據沒保存進數據庫,控制檯是不會打印出sql語句的,成功保存數據後,打印出的信息:
2014-06-28 16:40:10 [org.hibernate.SQL]-[DEBUG] insert into union_ssh.pub_users (enabled, issys, user_account, user_desc, user_name, user_password, user_id) values (?, ?, ?, ?, ?, ?, ?) Hibernate: insert into union_ssh.pub_users (enabled, issys, user_account, user_desc, user_name, user_password, user_id) values (?, ?, ?, ?, ?, ?, ?)
如下貼出代碼:
UserService:
package com.user.service; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.user.dao.UserDaoI; import com.user.model.PubUsers; import com.user.pageModel.UsersPageModel; import framework.base.dao.BaseDaoI; import framework.base.model.DataGrid; import framework.base.model.GridModel; import framework.base.model.User; @Service("userService")
@Transactional 使用註解配置事務 public class UserService { private UserDaoI userDaoi; //如下3個註解都是用來配置事務的,僅僅要在這個類的頭部加上事務註解,就能保存數據進數據庫,無論使用那個,都不會影響數據是提交 /* * 假設有事務, 那麼增長事務, 沒有的話新建一個(默認狀況下) * @Transactional(propagation=Propagation.REQUIRED) */ /*不論是否存在事務,都建立一個新的事務,原來的掛起,新的運行完成,繼續運行老的事務 * @Transactional(propagation=Propagation.REQUIRES_NEW) */ /*容器不爲這種方法開啓事務 * @Transactional(propagation=Propagation.NOT_SUPPORTED) */ public User save(User user) throws Exception { PubUsers t = new PubUsers(); BeanUtils.copyProperties(user, t); t.setUserId(88); t.setEnabled(1); t.setIssys(1); userDaoi.save(t); //throw new RuntimeException();//用來測試事務回滾 return user; } public BaseDaoI<PubUsers> getUserDao() { return userDao; } public UserDaoI getUserDaoi() { return userDaoi; } @Autowired public void setUserDaoi(UserDaoI userDaoi) { this.userDaoi = userDaoi; } }
UserDaoI:
package com.user.dao; import java.util.List; import com.user.model.PubUsers; public interface UserDaoI { public void save(PubUsers t); }
UserDaoImpl:
package com.user.dao.impl; import java.util.Iterator; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import com.user.dao.UserDaoI; import com.user.model.PubUsers; @Repository(value="userDao") public class UserDaoImpl implements UserDaoI { private SessionFactory sessionFactory; public SessionFactory getSessionFactory() { return sessionFactory; } @Autowired public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } /** * 得到當前事物的session * * @return org.hibernate.Session */ public Session getCurrentSession() { return sessionFactory.getCurrentSession(); } //配置事務後,直接使用這裏,就可保存數據進數據庫了 @Override public void save(PubUsers t) { Session session=this.sessionFactory.getCurrentSession(); session.save(t); } //這種方法是在沒有配置spring事務時使用的測試,這裏我已經測試過了,在UserDaoI中已經刪除了這個接口 public void saveTest(PubUsers t){ //方法1,不能成功 /* Session session=this.sessionFactory.getCurrentSession(); session.save(t); */ //2,不成功 /* Session session=this.sessionFactory.getCurrentSession(); Transaction tx=session.beginTransaction(); session.save(t); tx.commit();//這裏一提交,數據就保存進數據庫了 */ //方法3,不成功 /* Session session=this.sessionFactory.openSession(); session.save(t); */ //方法4,事務提交後,成功保存數據到數據庫 Session session=this.sessionFactory.openSession(); Transaction tx=session.beginTransaction(); session.save(t); tx.commit();//這裏一提交,數據就保存進數據庫了 } }
spring-hibernate.xml,主要配置了數據庫鏈接,和事務:
<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <!-- JNDI方式配置數據源 --> <!-- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="${jndiName}"></property> </bean> --> <!-- 配置數據源 --> <!-- 阿里巴巴數據源,不用配置數據庫驅動類,會依據請求的url找到對應的驅動類 --> <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="url" value="${jdbc_url}" /> <property name="username" value="${jdbc_username}" /> <property name="password" value="${jdbc_password}" /> <!-- 初始化鏈接大小 --> <property name="initialSize" value="0" /> <!-- 鏈接池最大使用鏈接數量 --> <property name="maxActive" value="20" /> <!-- 鏈接池最大空暇 --> <property name="maxIdle" value="20" /> <!-- 鏈接池最小空暇 --> <property name="minIdle" value="0" /> <!-- 獲取鏈接最大等待時間 --> <property name="maxWait" value="60000" /> <!-- <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="33" /> --> <property name="validationQuery" value="${validationQuery}" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <property name="testWhileIdle" value="true" /> <!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空暇鏈接,單位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 配置一個鏈接在池中最小生存的時間,單位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="25200000" /> <!-- 打開removeAbandoned功能 --> <property name="removeAbandoned" value="true" /> <!-- 1800秒,也就是30分鐘 --> <property name="removeAbandonedTimeout" value="1800" /> <!-- 關閉abanded鏈接時輸出錯誤日誌 --> <property name="logAbandoned" value="true" /> <!-- 監控數據庫 --> <!-- <property name="filters" value="stat" /> --> <property name="filters" value="mergeStat" /> </bean> <!-- 配置hibernate session工廠 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="hibernateProperties"> <props> <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> </props> </property> <!-- 本身主動掃描註解方式配置的hibernate類文件(實體類) --> <property name="packagesToScan"> <list> <value>com.*.model</value> <value>com.*.pageModel</value> </list> </property> <!-- 本身主動掃描hbm方式配置的hibernate文件和.hbm文件 --> <!-- <property name="mappingDirectoryLocations"> <list> <value>classpath:sy/hbm</value> </list> </property> --> </bean> <!-- 配置事務管理器 --> <bean name="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 註解方式配置事物 --> <!-- <tx:annotation-driven transaction-manager="transactionManager" /> --> <!-- 攔截器方式配置事物 --> <tx:advice id="transactionAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" /> <!-- save這裏設置了事務回滾的異常,不用拋RuntimeException異常,拋出SQLException也可以回滾--> <tx:method name="save*" rollback-for="SQLException"/> <tx:method name="update*" /> <tx:method name="modify*" /> <tx:method name="edit*" /> <tx:method name="delete*" /> <tx:method name="remove*" /> <tx:method name="repair" /> <tx:method name="get*" propagation="SUPPORTS" /> <tx:method name="find*" propagation="SUPPORTS" /> <tx:method name="load*" propagation="SUPPORTS" /> <tx:method name="search*" propagation="SUPPORTS" /> <tx:method name="*" propagation="SUPPORTS" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="transactionPointcut" expression="execution(* com..service.*Service.*(..))" /> <!-- 我一開始切面使用了這個,在service中後面少了一層,致使沒法開啓事務,之後要注意 <aop:pointcut id="transactionPointcut" expression="execution(* com.*.service.*(..))" /> --> <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" /> </aop:config> <!-- 供spring security啓動時使用 --> <bean id="userDao" class="com.user.dao.impl.UserDaoImpl"> </bean> </beans>
<?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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd "> <!-- 引入屬性文件 <bean id="propertyConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>classpath:jdbc.properties</value> </property> </bean> --> <context:property-placeholder location="classpath:config.properties" /> <!-- 本身主動掃描包 ,主要爲了使用註解注入,假設不掃描,就沒法使用註解注入了,只是這裏掃不掃描和事務配置無關--> <context:component-scan base-package="com.*.dao.impl,com.*.service,framework.base.dao.impl"/> </beans>
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name></display-name> <!-- spring配置文件位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml,classpath:spring-hibernate.xml,classpath:applicationContext-security.xml</param-value> </context-param> <!-- spring security 過濾器, 這個的位置順序和spring的監聽器啓動位置沒有什麼關係,可以放在spring監聽器的前面,也可以放置在後面。 但必定要放在struts的過濾器前面,因爲假設有本身定義的登陸頁面,當登陸時,就會跳轉到了struts相應的action中, 致使沒法使用spring security的驗證登陸了,正常狀況下,應該登陸時,會通過本身定義的MyUsernamePasswordAuthenticationFilter類的attemptAuthentication方法進行驗證。 假設驗證成功,則登陸成功,再也不運行相應的action驗證登陸 ;spring security驗證失敗,則跳回指定登陸失敗的頁面。 --> <filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>springSecurityFilterChain</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- spring監聽器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- hibernate配置 --> <filter> <filter-name>openSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> <init-param> <param-name>singleSession</param-name> <param-value>true</param-value> </init-param> </filter> <!-- Struts2配置 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <!-- hibernate的session啓動過濾器,在url請求action時啓動 ,不配置這個,url請求時沒法啓動hibernate的session--> <filter-mapping> <filter-name>openSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- struts攔截的url後綴 --> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.action</url-pattern> <url-pattern>*.jsp</url-pattern> </filter-mapping> <!-- 修復數據庫監聽器 <listener> <listener-class>com.menu.listener.RepairListener</listener-class> </listener> --> <!-- 登陸驗證碼的servlet --> <servlet> <servlet-name>CheckCode</servlet-name> <servlet-class>framework.util.CheckCode</servlet-class> </servlet> <servlet-mapping> <servlet-name>CheckCode</servlet-name> <url-pattern>/servlet/CheckCode</url-pattern> </servlet-mapping> <!-- log4j ,用來啓動log4j --> <servlet> <servlet-name>Log4jInitServlet</servlet-name> <servlet-class> framework.util.Log4jInitServlet </servlet-class> <init-param> <param-name>log4jLocation</param-name> <param-value>WEB-INF/classes/log4j.properties</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet> <welcome-file-list> <welcome-file>/login.jsp</welcome-file> </welcome-file-list> </web-app>
applicationContext-security.xml,這個原本不打算貼出來的,因爲這是spring security權限管理的配置文件,僅僅供你們參考一下,之後有時間,再發一下這個配置的文章
<beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd"> <!-- No bean named 'springSecurityFilterChain' is defined 一、 這時公佈一下你的項目,查看tomcat的webapps目錄下,找到你的項目目錄的classes目錄有沒有相關的spring.xml文件存在,不存在就會報錯 二、查看web.xml文件<param-value>標籤有沒有引入applicationContext-security.xml這個文件 --> <!-- 不用通過spring security過濾,通常js、css都不需要過濾 --> <http pattern="/*/js/**" security="none"/> <http pattern="/common/js/**" security="none"/> <http pattern="/login.jsp" security="none"/> <!-- auto-config="true" --> <http use-expressions="true" entry-point-ref="authenticationProcessingFilterEntryPoint" > <!-- 再也不在這裏對url進行權限攔截,在數據庫中取出url中相應的權限 <intercept-url pattern="/**" access="ROLE_USER" /> --> <!-- 單用戶登錄 --> <session-management> <concurrency-control max-sessions="1" error-if-maximum-exceeded="true" /> </session-management> <!-- 這樣的本身定義的登陸頁面,不能通過security的用戶信息驗證,也就等於不能取出用戶的權限 <form-login login-page='/login.jsp' default-target-url="/index.jsp"/> --> <!-- 嘗試訪問沒有權限的頁面時跳轉的頁面 --> <access-denied-handler error-page="/403.jsp"/> <custom-filter ref="loginFilter" position="FORM_LOGIN_FILTER" /> <custom-filter ref="myFilter" before="FILTER_SECURITY_INTERCEPTOR"/> <!-- 檢測失效的sessionId,session超時時,定位到另一個URL --> <session-management invalid-session-url="/sessionTimeOut.jsp" /> <!-- <custom-filter ref="logoutFilter" before="LOGOUT_FILTER"/> --> <logout invalidate-session="true" logout-success-url="/" logout-url="/logout"/> </http> <!-- 登陸驗證器 --> <beans:bean id="loginFilter" class="framework.security.login.MyUsernamePasswordAuthenticationFilter"> <!-- value="/loginUser.action"處理登陸表單的action ,value值要以「/」開關,不然會報錯 : org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.filterChains': Cannot resolve reference to bean 'org.springframework.security.web.DefaultSecurityFilterChain#3' while setting bean property 'sourceList' with key [3]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.web.DefaultSecurityFilterChain#3': Cannot resolve reference to bean 'loginFilter' while setting constructor argument with key [4]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginFilter' defined in class path resource [applicationContext-security.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are: PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'filterProcessesUrl' threw exception; nested exception is java.lang.IllegalArgumentException: userAction!login.action isn't a valid redirect URL --> <beans:property name="filterProcessesUrl" value="/user/loginUser.action"></beans:property> <!-- 驗證成功後的處理 --> <beans:property name="authenticationSuccessHandler" ref="loginLogAuthenticationSuccessHandler"></beans:property> <!-- 驗證失敗後的處理 --> <beans:property name="authenticationFailureHandler" ref="simpleUrlAuthenticationFailureHandler"></beans:property> <beans:property name="authenticationManager" ref="authenticationManager"></beans:property> <!-- 注入DAO爲了查詢相應的用戶 --> <beans:property name="userDao" ref="userDao"></beans:property> </beans:bean> <beans:bean id="loginLogAuthenticationSuccessHandler" class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler"> <beans:property name="defaultTargetUrl" value="/index.jsp"></beans:property> </beans:bean> <beans:bean id="simpleUrlAuthenticationFailureHandler" class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler"> <!-- 可以配置相應的跳轉方式。屬性forwardToDestination爲true採用forward false爲sendRedirect --> <beans:property name="defaultFailureUrl" value="/login.jsp"></beans:property> </beans:bean> <!-- 認證過濾器 --> <beans:bean id="myFilter" class="framework.security.base.MyFilterSecurityInterceptor"> <beans:property name="authenticationManager" ref="authenticationManager" /> <beans:property name="accessDecisionManager" ref="myAccessDecisionManagerBean" /> <beans:property name="securityMetadataSource" ref="mySecurityMetadataSource" /> </beans:bean> <!-- spring security提供的用戶登陸驗證 ,alias的值相應上面的ref="authenticationManager" --> <authentication-manager alias="authenticationManager"> <!--userDetailServiceImpl 獲取登陸的用戶、用戶權限 --> <authentication-provider user-service-ref="userDetailServiceImpl" /> </authentication-manager> <!-- 獲取登陸的用戶、用戶權限 --> <beans:bean id="userDetailServiceImpl" class="framework.security.base.MyUserDetailsService"> <beans:property name="userDao" ref="userDao"></beans:property> </beans:bean> <!-- 推斷是否有權限訪問請求的url頁面 --> <beans:bean id="myAccessDecisionManagerBean" class="framework.security.base.MyAccessDecisionManager"> </beans:bean> <!-- 獲取數據庫中所有的url資源,讀出url資源與權限的相應關係 --> <beans:bean id="mySecurityMetadataSource" class="framework.security.base.MySecurityMetadataSource"> </beans:bean> <!-- 未登陸的切入點 --> <beans:bean id="authenticationProcessingFilterEntryPoint" class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint"> <beans:property name="loginFormUrl" value="/sessionTimeOut.jsp"></beans:property> </beans:bean> </beans:beans>
<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>SSHMS</groupId> <artifactId>SSHMS</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name/> <description/> <!-- 指定Maven倉庫 --> <repositories> <repository> <id>maven</id> <name>Maven Repository Switchboard</name> <layout>default</layout> <url>http://repo1.maven.org/maven2</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> <repository> <id>alibaba-opensource</id> <name>alibaba-opensource</name> <url>http://code.alibabatech.com/mvn/releases/</url> <layout>default</layout> </repository> <repository> <id>alibaba-opensource-snapshot</id> <name>alibaba-opensource-snapshot</name> <url>http://code.alibabatech.com/mvn/snapshots/</url> <layout>default</layout> </repository> </repositories> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!-- servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <!-- JUnit4 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11-beta-1</version> <scope>test</scope> </dependency> <!-- log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!-- fastJson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.24</version> </dependency> <!-- spring3 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.1.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.1.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>3.1.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>3.1.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>3.1.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>3.1.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>3.1.2.RELEASE</version> </dependency> <!-- struts2 --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.3.16.3</version> <!-- 假設有hibernate時要去除這個,因爲hibernate也有這個類,不能以這個取代hibernate的那個,不然會出錯 --> <exclusions> <exclusion> <groupId>javassist</groupId> <artifactId>javassist</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> <version>2.3.16.3</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-convention-plugin</artifactId> <version>2.3.16.3</version> </dependency> <!-- 增長mysql驅動依賴包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.27</version> </dependency> <!-- 增長druid數據源依賴包 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.0</version> </dependency> <!-- spring事務切面的包 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.7.4</version> </dependency> <!-- hibernate4 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.1.7.Final</version> </dependency> <!-- spring security --> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-config</artifactId> <version>3.2.4.RELEASE</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> <version>3.2.4.RELEASE</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-core</artifactId> <version>3.2.4.RELEASE</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>3.2.8.RELEASE</version> <scope>compile</scope> </dependency> <!-- org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.filterChains': Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/springframework/web/context/request/async/CallableProcessingInterceptor --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>3.2.8.RELEASE</version> <scope>compile</scope> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> <scope>compile</scope> <optional>true</optional> </dependency>
<!--註解事務要引入的包--> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-war-plugin</artifactId> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build> </project>
也貼上一張項目的架構圖吧