Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 表不是使用JPA创建的_Spring_Spring Mvc_Jpa_Spring Security_Jpa 2.0 - Fatal编程技术网

Spring 表不是使用JPA创建的

Spring 表不是使用JPA创建的,spring,spring-mvc,jpa,spring-security,jpa-2.0,Spring,Spring Mvc,Jpa,Spring Security,Jpa 2.0,我正在开发spring应用程序。我也在使用JPA和spring安全性。在我运行服务器后不会创建表 这是我的web.xml配置。请指导我的配置。security-config.xml应该由ContextLoader listener或DispatcherServlet加载 web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/j

我正在开发spring应用程序。我也在使用JPA和spring安全性。在我运行服务器后不会创建表

这是我的web.xml配置。请指导我的配置。security-config.xml应该由ContextLoader listener或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>

org.springframework.web.context.ContextLoaderListener
上下文配置位置
类路径:/jpaContext.xml
上下文配置位置
/WEB-INF/config/security-config.xml
org.springframework.security.web.session.HttpSessionEventPublisher
适合性跟踪测试
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
/WEB-INF/config/servlet-context.xml,/WEB-INF/config/jpaContext.xml
适合性跟踪测试
*.html
springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy
springSecurityFilterChain
/*
Web应用程序创建的原型
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>
<?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>

security-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: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>
<?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>


为什么您认为应该创建它?web.xml与此无关。请发布您的JPA配置。谢谢。是的,我已经编辑了我的帖子。请建议!!“在我运行服务器后不会创建该表。”哪个表不会创建?对于dispatcherServlet,您只需要servlet-context.xml contextConfigLocation。对于DispatcherServlet,启动时加载标签可能有用。不要使用2个contextConfigLocation上下文参数:将其全部放在一个参数值标记中(每个值用新行分隔)。