I have a Spring MVC application, and I was pretty confident I understand all the various configuration I have set up, however, in a recent exercise I have been switching over to use pure annotation based config (apart from the security configuration) and I have seen a change in the application's behavior which has made me think twice on a few points.
The main behavior change, is that it appears that my old app was running an OpenSessionInView type pattern - as several of my domain objects were lazy loading elements, but those elements could then still be accessed outside of the transactional/service layer classes (e.g. in my controllers) - having switched to annotations, this now fails as you would expect with lazy loading errors as the session is not open in my controller layer.
Below is the config I have been using - can any one explain which part of this is responsible for the OpenSessionInView type behavior my app was displaying previously?
App Context config file:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xmlns:cloud="http://schema.cloudfoundry.org/spring" xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.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.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://schema.cloudfoundry.org/spring
http://schema.cloudfoundry.org/spring/cloudfoundry-spring-0.7.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
<context:property-placeholder location="classpath*:META-INF/spring/*.properties" />
<context:spring-configured />
<context:annotation-config />
<mvc:annotation-driven />
<aop:aspectj-autoproxy proxy-target-class="true" />
<context:component-scan base-package="com.tmm.enterprise.adapter"></context:component-scan>
<context:component-scan base-package="com.tmm.enterprise.service"></context:component-scan>
<context:component-scan base-package="com.tmm.enterprise.helper"></context:component-scan>
<context:component-scan base-package="com.tmm.enterprise.util"></context:component-scan>
<context:component-scan base-package="com.tmm.enterprise.repository"></context:component-scan>
<context:component-scan base-package="com.tmm.enterprise.core.dao"></context:component-scan>
<context:component-scan base-package="com.tmm.enterprise.security.dao"></context:component-scan>
<!--Http client -->
<bean id="httpClient" class="org.apache.commons.httpclient.HttpClient">
<property name="state" ref="httpState" />
</bean>
<!--Http state -->
<bean id="httpState" factory-bean="httpStateFactory" factory-method="buildState"/>
<!--Http client -->
<bean id="httpClientFactory"
class="org.springframework.http.client.CommonsClientHttpRequestFactory">
<constructor-arg ref="httpClient" />
</bean>
<!--RestTemplate -->
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<constructor-arg ref="httpClientFactory" />
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
lazy-init="default" autowire="default">
<property name="driverClass" value="${database.driverClassName}" />
<property name="jdbcUrl" value="${database.url}" />
<property name="user" value="${database.username}" />
<property name="password" value="${database.password}" />
</bean>
<bean class="org.springframework.orm.jpa.JpaTransactionManager"
id="transactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="applicationController" class="com.tmm.enterprise.service.ApplicationService"></bean>
<bean id="socialConfig"
class="com.tmm.enterprise.configuration.SocialConfiguration"></bean>
<bean id="cachingConfig"
class="com.tmm.enterprise.configuration.CachingConfiguration"></bean>
<tx:annotation-driven mode="aspectj"
transaction-manager="transactionManager" proxy-target-class="true" />
<bean
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
id="entityManagerFactory">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="persistenceUnit" />
</bean>
<bean id="geocoder" class="com.google.code.geocoder.Geocoder" />
<bean id="validatorFactory" class="javax.validation.Validation"
factory-method="buildDefaultValidatorFactory" />
<bean id="validator" factory-bean="validatorFactory"
factory-method="getValidator" />
</beans>
MVC Config:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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: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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- The controllers are autodetected POJOs labeled with the #Controller annotation. -->
<context:component-scan base-package="com.tmm.enterprise.controller" use-default-filters="false">
<context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
<!-- Turns on support for mapping requests to Spring MVC #Controller methods
Also registers default Formatters and Validators for use across all #Controllers -->
<mvc:annotation-driven/>
<mvc:resources mapping="/resouces/**" location="/styles, /images, /javascript, /libs" />
<!-- a higher value meaning greater in terms of sorting. -->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" p:order="3">
<property name="alwaysUseFullPath" value="true" />
</bean>
<!-- register "global" interceptor beans to apply to all registered HandlerMappings -->
<mvc:interceptors>
<bean class="org.springframework.web.servlet.theme.ThemeChangeInterceptor"/>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" p:paramName="lang"/>
</mvc:interceptors>
<!-- selects a static view for rendering without the need for an explicit controller -->
<mvc:view-controller path="/login"/>
<!--mvc:view-controller path="/index"/-->
<mvc:view-controller path="/uncaughtException"/>
<mvc:view-controller path="/resourceNotFound"/>
<mvc:view-controller path="/dataAccessFailure"/>
<mvc:view-controller path="/jobs" view-name="jobsPage"/>
<mvc:view-controller path="/applications" view-name="jobsPage"/>
<mvc:view-controller path="/users" view-name="usersPage"/>
<mvc:view-controller path="/companies" view-name="companiesPage"/>
<mvc:view-controller path="/tos" view-name="tos"/>
<mvc:view-controller path="/about" view-name="about"/>
<mvc:view-controller path="/privacypolicy" view-name="privacyPolicy"/>
<mvc:view-controller path="/sitemap.xml" view-name="sitemap"/>
<!-- Resolves logical view names returned by Controllers to Tiles; a view
name to resolve is treated as the name of a tiles definition -->
<bean class="org.springframework.js.ajax.AjaxUrlBasedViewResolver" id="tilesViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>
</bean>
<!-- Configures the Tiles layout system -->
<bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer" id="tilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/layouts/layouts.xml</value>
<!-- Scan views directory for Tiles configurations -->
<value>/WEB-INF/views/**/views.xml</value>
</list>
</property>
</bean>
<!-- Resolves localized messages*.properties and application.properties files in the application to allow for internationalization.
The messages*.properties files translate Roo generated messages which are part of the admin interface, the application.properties
resource bundle localizes all application specific messages such as entity names and menu items. -->
<bean class="org.springframework.context.support.ReloadableResourceBundleMessageSource" id="messageSource" p:basenames="WEB-INF/i18n/application" p:fallbackToSystemLocale="false"/>
<!-- store preferred language configuration in a cookie -->
<bean class="org.springframework.web.servlet.i18n.CookieLocaleResolver" id="localeResolver" p:cookieName="locale"/>
<!-- resolves localized <theme_name>.properties files in the classpath to allow for theme support -->
<bean class="org.springframework.ui.context.support.ResourceBundleThemeSource" id="themeSource"/>
<!-- store preferred theme configuration in a cookie -->
<bean class="org.springframework.web.servlet.theme.CookieThemeResolver" id="themeResolver" p:cookieName="theme" p:defaultThemeName="standard"/>
<!-- This bean resolves specific types of exceptions to corresponding logical - view names for error views.
The default behaviour of DispatcherServlet - is to propagate all exceptions to the servlet container:
this will happen - here with all other types of exceptions. -->
<bean class="com.tmm.enterprise.controller.exception.NerdExceptionHandler" p:defaultErrorView="uncaughtException">
<property name="exceptionMappings">
<props>
<prop key=".DataAccessException">dataAccessFailure</prop>
<prop key=".NoSuchRequestHandlingMethodException">resourceNotFound</prop>
<prop key=".TypeMismatchException">resourceNotFound</prop>
<prop key=".MissingServletRequestParameterException">resourceNotFound</prop>
</props>
</property>
</bean>
<!-- Controllers -->
<bean id="connectController" class="com.tmm.enterprise.controller.ConnectController"></bean>
<!-- allows for integration of file upload functionality -->
<bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver" id="multipartResolver"/>
<bean id="ajaxViewResolver"
class="com.tmm.enterprise.views.AjaxViewResolver">
<property name="ajaxView">
<bean class="com.tmm.enterprise.views.AjaxView" />
</property>
<property name="ajaxPrefix" value="ajax_"></property>
</bean>
<bean id="editableViewResolver"
class="com.tmm.enterprise.views.EditableViewResolver">
<property name="editableView">
<bean class="com.tmm.enterprise.views.EditableView" />
</property>
<property name="editablePrefix" value="editable_"></property>
</bean>
</beans>
The OpenSessionInView(Hibernate)/OpenEntityManagerInView(JPA) behaviour is often part of a filter defined in web.xml, but can also be done using an interceptor, as described in this answer. You can use the OpenEntityManagerInViewInterceptor for JPA.
<mvc:interceptors>
<bean class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
</mvc:interceptors>
Related
I am trying to get the value of "value" attribute of second bean's property with name "jdbcUrl". I tried multiple xpath combinations, but the only one that works for me is (//#value)[4] which is not optimal at all because if someone changes order of properties, I will get the wrong value. Is there more safer way to do it using xPath?
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- Add support for component scanning -->
<context:component-scan base-package="xxx" />
<!-- Add support for conversion, formatting and validation support -->
<mvc:annotation-driven/>
<!-- Define Spring MVC view resolver -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- Step 1: Define Database DataSource / connection pool -->
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="org.postgresql.Driver" />
<property name="jdbcUrl" value="afsafa" />
<property name="user" value="postgres" />
<property name="password" value="admin" />
<!-- these are connection pool properties for C3P0 -->
<property name="initialPoolSize" value="5"/>
<property name="minPoolSize" value="5" />
<property name="maxPoolSize" value="20" />
<property name="maxIdleTime" value="30000" />
</bean>
<!-- Step 2: Setup Hibernate session factory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="packagesToScan" value="ngsi" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- Step 3: Setup Hibernate transaction manager -->
<bean id="myTransactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- Step 4: Enable configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="myTransactionManager" />
<!-- Add support for reading web resources: css, images, js, etc ... -->
<mvc:resources location="resources" mapping="/resources/**"/>
</beans>
get the value of "value" attribute of second bean's property with name "jdbcUrl"
That translates directly to
/*/*:bean[2]/*:property[#name="jdbcUrl"]/#value
That's XPath 2.0 syntax; if you're still using XPath 1.0 for some reason, then you can't use *:local; instead, either bind a prefix to the namespace http://www.springframework.org/schema/beans, or use *[local-name()="local"]
I am trying to import a standard spring project P1 into a web project P2 and I am implementing the web aspect of P1 in P2.
I have imported all resources of P1 in P2 as P1.jar
Have explicitly imported the application context file as well using <import-resource> which happens successfully.
But the JpaRepositories does not get autowired in P2. It doesn't seem to be in the context of P2.
Can anyone suggest what I might be missing here.
UPDATE: 25Nov2016
P1-ApplicationContext.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:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:repository="http://www.springframework.org/schema/data/repository"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.8.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.11.xsd">
<tx:annotation-driven proxy-target-class="true"/>
<context:component-scan base-package="com.home.p1.blog" />
<jpa:repositories base-package="com.home.p1.blog.repo" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver" />
<property name="url"
value="jdbc:derby://localhost:1527/MyDerby" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="WorkUp" />
<property name="dataSource" ref="dataSource"></property>
<property name="packagesToScan" value="com.home.p1.blog"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.DerbyDialect" />
</bean>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.DerbyDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.use_sql_comments">false</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="authorDAO" class="com.home.p1.blog.repo.AuthorDAO">
</bean>
<bean id="authorService" class="com.home.p1.blog.service.AuthorServiceImpl"></bean>
<bean id="blogService" class="com.home.p1.blog.service.BlogServiceImpl"></bean>
</beans>
P2-ApplicationContext.xml (its actually named: rest-servlet.xml to hold the RestController scan package)
<?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:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:repository="http://www.springframework.org/schema/data/repository"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.8.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.11.xsd">
<import resource="classpath:com.home.p1.blog.src/src/main/resources/P1-ApplicationContext.xml"/> <!-- this loads find -->
<context:component-scan base-package="com.home.p2.blog.controller" /> <!-- this will load the RestController -->
<!-- <context:component-scan base-package="com.home.**" /> -->
<!-- THIS GIVES SOME WIERD ERROR <jpa:repositories base-package="com.home.*" /> -->
<mvc:annotation-driven/>
<!-- <bean id="blogService" class="com.oracle.blog.service.BlogServiceImpl"></bean> -->
</beans>
UPDATE: 28Nov2016
Upon including <jpa:repositories base-package> configuration in P2's application context, I no longer get the wierd error which I was getting earlier.
Also, enabling a successful import of xml config in Spring bean support, everything else is falling appropriately into place. Only the JpaRepositories referred in P1 are not reflecting in P2's context.
I dont think what you are trying to do is possible. You need to enable repository scan in your p2 and mention the packages in that scan.
If your p1.jar is in the classpath of p2 then you only have enable repository scan in your p2,
<jpa:repositories base-package="com.home.p1.blog.repo" />
RESOLVED: Please note that to import the JpaRepositories from P1.jar in P2, we don't have to mention <jpa:repositories> tag in P2's config xml.
Just ensure that we are appropriately importing the P1's config.xml.
Also Eclipse users need to add Xml / Java config in Spring Beans Support tab as well as enable support for import element.
I searched for how to configure spring with tomcat I found that I have to put the following loader into context
<Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"/>
And I have to put spring-instrument-tomcat-x.x.jar into the tomcat lib folder
applicationContext.xml
<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
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-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<import resource="database-mysql.xml" />
<context:load-time-weaver aspectj-weaving="off" />
<!-- enables interpretation of the #Resource annotation to apply dependency injection through annotations -->
<context:component-scan base-package="com.shs.dao"></context:component-scan>
<context:component-scan base-package="com.shs.service"></context:component-scan>
<context:component-scan base-package="com.shs.web"></context:component-scan>
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
<!-- enables interpretation of the #PersistenceUnit/#PersistenceContext annotations providing convenient
access to EntityManagerFactory/EntityManager -->
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<bean id="contextApplicationContextProvider" class="com.shs.utils.spring.AppContext"></bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="Default_Persistence_Unit"/>
<property name="dataSource" ref="dataSource"/>
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="false"/>
<property name="database" value="MYSQL"/>
</bean>
</property>
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver"/>
</property>
</bean>
<bean id="jpaTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="jpaTransactionManager" proxy-target-class="true"/>
</beans>
But I know there is another methodology to configure spring without using context loader or putting the jar into tomcat lib folder
Any ideas how to do that
Thanks in advance
I just started with spring framework and i have a problem with my spring beans configurations.
To load my static resources such as css, js, etc. i had to put the following line:
<mvc:resources mapping=”/resources/**” location=”/resources/” /> in my spring bean configuration:
My problem is that if i put this line, for some reason all url's of my application are not mapped when i deploy my application. When i comment this line all the url's application are working just fine, but of course my static resources are not mapped.
I need some tips on how should i tackle this problem. Thank you !.
Also this is all my spring-config.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<context:component-scan base-package="com.application" />
<!-- Mapping the static resources -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- Unimportant for my problem -->
<!-- Bean used for login from userDao(repository) -->
<bean id="customUserDetailsService" class="com.application.service.CustomUserDetailsService"></bean>
<!-- Setup the userDao(repository) -->
<bean id="userDao" class="com.application.model.UserDAO" />
<!-- Declared datasource bean -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/licenta" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
</beans>
You need to add <mvc:annotation-driven /> to your spring-config.xml
Check out this similar problem
Any ideas, what could cause this error?
org.springframework.beans.factory.parsing.BeanDefinitionParsingException:
Configuration problem: Unable to locate Spring NamespaceHandler for
XML schema namespace [http://www.springframework.org/schema/data/jpa]
Offending resource: ServletContext resource
[/WEB-INF/spring/appServlet/servlet-context.xml]
Here is my 'servle-context.xml' (there are some problems with the indents, but the file is too huge...):
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
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/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by #Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.epam.mvc3.model" />
<context:component-scan base-package="com.epam.mvc3.controller" />
<!-- JPA -->
<beans:bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<beans:property name="location">
<beans:value>resources/database.properties</beans:value>
</beans:property>
</beans:bean>
<beans:bean id="myDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<beans:property name="driverClassName">
<beans:value>${jdbc.driverClassName}</beans:value>
</beans:property>
<beans:property name="url">
<beans:value>${jdbc.url}</beans:value>
</beans:property>
<beans:property name="username">
<beans:value>${jdbc.username}</beans:value>
</beans:property>
<beans:property name="password">
<beans:value>${jdbc.password}</beans:value>
</beans:property>
</beans:bean>
<beans:bean id="myEmf"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<beans:property name="dataSource" ref="myDataSource" />
<beans:property name="persistenceUnitName" value="application" />
<beans:property name="persistenceXmlLocation"
value="classpath*:META-INF/persistence.xml" />
<beans:property name="jpaVendorAdapter" ref="hibernateVendor" />
<beans:property name="loadTimeWeaver">
<beans:bean
class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"></beans:bean>
</beans:property>
</beans:bean>
<beans:bean id="hibernateVendor"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
</beans:bean>
<beans:bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<beans:property name="entityManagerFactory" ref="myEmf"></beans:property>
</beans:bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- Spring Data configuration -->
<jpa:repositories base-package="com.epam.mvc3.repository"/>
</beans:beans>
As you can see, i specified the path to the jpa-schema. And I don't know what is the problem
Try to replace
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
by
http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
But if you still use Version 1.0-M1 then try to update first. DATAJPA-21
Make sure you have spring-data-jpa in your dependencies
If you use maven:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.7.0.RELEASE</version>
</dependency>
I fixed this by resolving the missing the spring-tx dependency in our project.
org.springframework:spring-tx
The issue is because the spring-jpa schema cannot be located . You might be having some old jar which does not contain the schema info .
I know this seems silly, but if you are using IDEA and Maven you should check to make sure you have Use plugin registry checked so that the server also has access to your jar files.
I would post a screen shot but I'm new on here and don't have enough reputation points. It does work though and I've verified with the support team at IntelliJ
After facing this same problem over a week, I found a solution. If you are using Spring MVC + JPA + Maven, use this dispatcher-servlet.xml
<?xml version="1.0" encoding="windows-1252"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- Use #Component annotations for bean definitions -->
<context:component-scan base-package="PACKAGE WHERE YOU CAN FIND YOUR SOURCE CODE"/>
<!-- Use #Controller annotations for MVC controller definitions -->
<mvc:annotation-driven />
<!-- Add JPA support -->
<bean id="emf" class=
"org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="loadTimeWeaver">
<bean class=
"org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
</property>
</bean>
<!-- Add Transaction support -->
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="emf"/>
</bean>
<!-- Use #Transaction annotations for managing transactions -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- View resolver -->
<bean class=
<!-- HERE YOU REPLACE PROPERTY VALUE TO MATCH YOUR VIEW FOLDER (JSP PAGES) -->
"org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/web/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
too late, but this solved my problem:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.9.4.RELEASE</version>
</dependency>