正在开发弹簧应用。我也在使用带有弹簧安全性的JPA。在运行服务器之后,不会创建表。
这是我的web.xml配置。请指导我的配置.security-config.xml应该由ContextLoader监听器或DispatcherServlet加载???
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- Bootstraps the root web application context before servlet initialization -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/jpaContext.xml</param-value>
</context-param>
<!--Load spring security config file -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/security-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>
<!--Spring MVC config -->
<servlet>
<servlet-name>FitnessTrackerTest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/servlet-context.xml , /WEB-INF/config/jpaContext.xml </param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>FitnessTrackerTest</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<!--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>
<display-name>Archetype Created Web Application</display-name>
</web-app>
jpaContext.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: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-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- Tells spring that annotation based is used -->
<context:annotation-config/>
<!-- tells spring Persistence annotation based is used -->
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<!-- Jpa configuration -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="punit"/>
<property name="dataSource" ref="dataSource"/>
<!-- name of the jpa provider -->
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true"/>
</bean>
</property>
<!-- Hibernate configuration-->
<property name="jpaPropertyMap">
<map>
<!-- Tells the hibernate the type of the query language is going to use -->
<entry key="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<!-- tells hibernate if the table is not craeted ,create or update it -->
<entry key="hibernate.hbm2ddl.auto" value="update"/>
<!-- runs sql query behind -->
<entry key="hibernate.format_sql" value="true"/>
</map>
</property>
</bean>
<!-- Jpa transaction -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<!-- wraps the jpa business is with the transaction process -->
<property name="entityManagerFactory" ref="entityManagerFactory"></property>
</bean>
<!-- Configuring the transaction using annotation -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!--database connection -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/fitnesstracker?autoReconnect=true"/>
<property name="username" value="general"/>
<property name="password" value="mindfire"/>
</bean>
</beans>
安全-config.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.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-3.2.xsd">
<http auto-config="true" use-expressions="true">
<intercept-url pattern="/loginFailure.html" access="permitAll"/>
<intercept-url pattern="/login.html" access="permitAll"/>
<intercept-url pattern="/logout.html" access="permitAll"/>
<intercept-url pattern="/403.html" access="permitAll"/>
<intercept-url pattern="/**" access="hasRole('ROLE_USER')"/>
<form-login login-page="/login.html" authentication-failure-url="/loginFailure.html"/>
<logout logout-success-url="/logout.html"/>
<access-denied-handler error-page="/403.html"/>
</http>
<authentication-manager>
<authentication-provider>
<password-encoder hash="bcrypt"/>
<jdbc-user-service data-source-ref="datasource"/>
</authentication-provider>
</authentication-manager>
<beans:bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<beans:property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<beans:property name="url" value="jdbc:mysql://localhost:3306/fitnesstracker"/>
<beans:property name="username" value="general"/>
<beans:property name="password" value="mindfire"/>
</beans:bean>
</beans:beans>
和servlet-context.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.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-3.2.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="com.fitnesstracker.controller"></context:component-scan>
<security:global-method-security pre-post-annotations="enabled"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<import resource="classpath:security-config.xml"/>
<import resource="classpath:jpaContext.xml"/>
</beans>