gpt4 book ai didi

java - spring 和 hibernate 的 web.xml 设置

转载 作者:行者123 更新时间:2023-12-02 13:40:45 25 4
gpt4 key购买 nike

/WEB-INF/appconfig-data.xml

<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/javabegin" />
<property name="username" value="root" />
<property name="password" value="pass" />
</bean>

<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">



<property name="driverClass" value="${jdbc.driverClassName}" />

<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />

<property name="minPoolSize" value="3" />
<property name="maxPoolSize" value="20" />
<property name="maxIdleTime" value="30000" />
</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="packagesToScan" value="com.hihoall" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>

<bean id="myTransactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<tx:annotation-driven transaction-manager="myTransactionManager" />

/WEB-INF/appconfig-root.xml

<import resource="spring-mvc-servlet.xml"/>

<import resource="appconfig-data.xml"/>

<import resource="application-security.xml"/>

<context:component-scan base-package="com.hihoall.*"/>

<context:property-placeholder location="classpath:database.properties"/>

/WEB-INF/application-security.xml

<http auto-config="true">

<remember-me data-source-ref="dataSource" />


<access-denied-handler error-page="/accessDenied" />

<intercept-url pattern="/user" access="ROLE_USER" />
<intercept-url pattern="/admin" access="ROLE_ADMIN" />

<form-login login-page='/login' default-target-url="/user"
authentication-failure-url="/login?error=true" username-parameter="user_login"
password-parameter="password_login" />

<logout logout-success-url="/login" />

</http>

<beans:bean id="jdbcGroupsImpl" class="org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl">
<beans:property name="enableGroups" value="true" />
<beans:property name="enableAuthorities" value="false" />
<beans:property name="dataSource" ref="dataSource" />
</beans:bean>

<authentication-manager>
<authentication-provider user-service-ref="jdbcGroupsImpl">

</authentication-provider>
</authentication-manager>

/WEB-INF/spring-mvc-servlet.xml

<mvc:annotation-driven/>

<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>

<mvc:resources location="/resources/" mapping="/resources/**"></mvc:resources>

<mvc:resources mapping="/favicon.ico" location="/resources/images/favicon.ico" />

<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<ref bean="localeChangeInterceptor" />
</mvc:interceptor>
</mvc:interceptors>

<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/locales/messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>


<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="ru" />
</bean>

<context:component-scan base-package="com.hihoall"/>

/WEB-INF/web.xml

    <display-name>spring-mvc</display-name>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/appconfig-root.xml
</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<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>

<filter>
<filter-name>encoding-filter</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding-filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

一切都很好,但如果我稍微改变一下

web.xml 中添加了笔画“/WEB-INF/spring-mvc-servlet.xml”

<display-name>spring-mvc</display-name>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/appconfig-root.xml
</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-mvc-servlet.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<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>

<filter>
<filter-name>encoding-filter</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding-filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

并且笔画“import resource="spring-mvc-servlet.xml”已从/webapp/WEB-INF/appconfig-root.xml中删除

    <import resource="appconfig-data.xml"/>

<import resource="application-security.xml"/>

<context:component-scan base-package="com.hihoall.*"/>

<context:property-placeholder location="classpath:database.properties"/>
</beans>

我收到错误

HTTP Status 500 - org.apache.jasper.JasperException: org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread

我应该怎样做才能使第二个变体起作用?

最佳答案

您在 mvc 配置文件和数据配置文件中进行了组件扫描:

内部/WEB-INF/spring-mvc-servlet.xml:

<context:component-scan base-package="com.hihoall"/>

内部/WEB-INF/appconfig-root.xml

<context:component-scan base-package="com.hihoall.*"/>

由于它们扫描相同的包,因此 bean 在这里加载了 2 次,一次由 mvc 配置加载,另一次由 root 配置加载。因此它们存储在 Web 应用程序上下文中,另一个存储在根应用程序上下文中。

现在事务管理 bean 已在根配置文件中定义(因为您正在根配置文件中导入 /WEB-INF/appconfig-data.xml)。

这会导致您的应用程序出现问题, Controller 类可能会从不了解事务管理的 Web 应用程序上下文中获取 Bean,因此当您执行 DML 操作时,它会因该错误而失败。

关于java - spring 和 hibernate 的 web.xml 设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42749716/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com