如何在Spring MVC中使用延迟加载 [英] How to use lazy loading in Spring MVC
问题描述
如何在Spring MVC中使用延迟加载?此刻我正在使用,但这使得我的应用程序运行速度变慢。
这是我的域名的一部分:
pre $ @ $ c $ @ManyToMany(fetch = FetchType.EAGER)
@JoinTable (name =NEWS_TAG,joinColumns = @JoinColumn(name =NEWS_ID),inverseJoinColumns = @JoinColumn(name =TAG_ID))
private List< Tags> tags = new ArrayList< Tags>();
公共列表<标签> getTags(){
返回this.tags;
}
和dao:
public List< News> getSomeNews(long b,long hm){
List< News> news =(List< News>)sessionFactory
.getCurrentSession()
.createQuery(
from News WHERE title!='About'ORDER BY publish_time)
.setMaxResults (int)hm).setFirstResult((int)b).list();
回报消息;
}
Servlet-context:
< context:annotation-config />
< context:component-scan base-package =net.babobka.blog/>
< import resource =../../ db / db-config.xml/>
< bean id =urlForwardController
class =org.springframework.web.servlet.mvc.UrlFilenameViewController/>
< bean id =tilesConfigurer
class =org.springframework.web.servlet.view.tiles2.TilesConfigurer>
< property name =definitions>
< list>
<值> /WEB-INF/tiles.xml< /值>
< / list>
< / property>
< / bean>
< bean id =viewResolver
class =org.springframework.web.servlet.view.UrlBasedViewResolver>
< property name =viewClass
value =org.springframework.web.servlet.view.tiles2.TilesView/>
< / bean>
< / beans>
Db-config:
< bean id =propertyConfigurer
class =org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
p:location =/ WEB-INF / db / jdbc .properties/>
destroy-method =closep:driverClassName =$ {jdbc.driverClassName }
p:url =$ {jdbc.databaseurl}p:username =$ {jdbc.username}p:password =$ {jdbc.password}/>
< bean id =sessionFactory
class =org.springframework.orm.hibernate3.LocalSessionFactoryBean>
< property name =dataSourceref =dataSource/>
< property name =configLocation>
<值> /WEB-INF/db/hibernate.cfg.xml< /值>
< / property>
< property name =configurationClass>
< value> org.hibernate.cfg.AnnotationConfiguration< / value>
< / property>
< property name =hibernateProperties>
<道具>
< prop key =hibernate.dialect> $ {jdbc.dialect}< / prop>
< prop key =hibernate.show_sql> $ {jdbc.show_sql}< / prop>
< /道具>
< / property>
< / bean>
< bean id =transactionManager
class =org.springframework.orm.hibernate3.HibernateTransactionManager>
< property name =sessionFactoryref =sessionFactory/>
< / bean>
< tx:annotation-driven />
< / beans>
Web.xml:
<?xml version =1.0encoding =UTF-8?>
< web-app version =2.5xmlns =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>
<! - 所有Servlet共享的Root Spring容器的定义
和Filters - >
< context-param>
< param-name> contextConfigLocation< / param-name>
< param-value>
/WEB-INF/spring/root-context.xml
/WEB-INF/spring/application-security.xml
< /参数值>
< / context-param>
<! - 创建所有Servlet和过滤器共享的Spring容器 - >
< listener>
< listener-class> org.springframework.web.context.ContextLoaderListener< / listener-class>
< / listener>
<! - 处理应用程序请求 - >
< servlet>
< servlet-name> appServlet< / servlet-name>
< servlet-class> org.springframework.web.servlet.DispatcherServlet< / servlet-class>
< init-param>
< param-name> contextConfigLocation< / param-name>
< param-value> /WEB-INF/spring/appServlet/servlet-context.xml< / param-value>
< / init-param>
1< / load-on-startup>
< / servlet>
< servlet-mapping>
< servlet-name> appServlet< / servlet-name>
< url-pattern> /< / url-pattern>
< / servlet-mapping>
< servlet-mapping>
< servlet-name> appServlet< / servlet-name>
< url-pattern> * .gif< / 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> hibernateFilterChain< / filter-name>
< filter-class> org.springframework.orm.hibernate3.support.OpenSessionInViewFilter< / filter-class>
< / filter>
< filter-mapping>
< filter-name> hibernateFilterChain< / filter-name>
< url-pattern> / *< / url-pattern>
< / filter-mapping>
< / web-app>
我需要做些什么来解决我的问题?您可以使用 OpenSessionInViewFilter
来防止休眠会话关闭。
web.xml
: < filter>
< filter-name> hibernateFilterChain< / filter-name>
< filter-class> org.springframework.orm.hibernate4.support.OpenSessionInViewFilter< / filter-class>
< / filter>
< filter-mapping>
< filter-name> hibernateFilterChain< / filter-name>
< url-pattern> / *< / url-pattern>
< / filter-mapping>
@see阅读更多信息: OpenSessionInViewFilter
code> fetch = FetchType.EAGER 。 @ManyToMany
默认为LAZY。
How to use lazy loading in Spring MVC? I'm using eager at this moment, but this makes my app works slowler. This is part of my domain:
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "NEWS_TAG", joinColumns = @JoinColumn(name = "NEWS_ID"), inverseJoinColumns = @JoinColumn(name = "TAG_ID"))
private List<Tags> tags = new ArrayList<Tags>();
public List<Tags> getTags() {
return this.tags;
}
And dao:
public List<News> getSomeNews(long b, long hm) {
List<News> news = (List<News>) sessionFactory
.getCurrentSession()
.createQuery(
"from News WHERE title!='About' ORDER BY publish_time")
.setMaxResults((int) hm).setFirstResult((int) b).list();
return news;
}
Servlet-context:
<context:annotation-config />
<context:component-scan base-package="net.babobka.blog" />
<import resource="../../db/db-config.xml" />
<bean id="urlForwardController"
class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles.xml</value>
</list>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.tiles2.TilesView" />
</bean>
</beans>
Db-config:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/db/jdbc.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>/WEB-INF/db/hibernate.cfg.xml</value>
</property>
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${jdbc.dialect}</prop>
<prop key="hibernate.show_sql">${jdbc.show_sql}</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven />
</beans>
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">
<!-- The definition of the Root Spring Container shared by all Servlets
and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/root-context.xml
/WEB-INF/spring/application-security.xml
</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>*.gif</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>hibernateFilterChain</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hibernateFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
What I have to do to solve my problem?
You can use OpenSessionInViewFilter
to prevent hibernate session get closed. Add this to web.xml
:
<filter>
<filter-name>hibernateFilterChain</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hibernateFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
@see read more here: OpenSessionInViewFilter
And remove fetch = FetchType.EAGER
. @ManyToMany
is LAZY by default.
这篇关于如何在Spring MVC中使用延迟加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!