I have my hibernate entity manager factory declared in jpa.xml, hazecast configured in hazelcast.xml and I have domain model setup and I have corresponding componentContext.xml present in the domain model project where my entity manager factory class is implemented in it. But the problem I am
facing is that my bean in componentContext.xml is getting intialized in a loop (observed from hibernate.log)
My Web.xml has only com.sun.faces.config.ConfigureListener listenere configured.
My Stack
Spring - 4.3.8
Hibernate - 5.0.10-FINAL
hazelcast-hibernate5 - 1.3.0
JPA.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"
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">
<context:property-placeholder location="classpath:dataSources.properties" />
<!-- Default JPA configuration for OpenJPA and Hibernate -->
<!-- Hibernate configuration -->
<bean id="hibernateEntityManagerFactory" abstract="true" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.max_fetch_depth" value="3" />
<entry key="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider" />
<entry key="hibernate.enable_lazy_load_no_trans" value="true" />
<entry key="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform" />
<entry key="hibernate.current_session_context_class" value="jta" />
</map>
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="${dbDialect}" />
</bean>
</property>
</bean>
<alias name="hibernateEntityManagerFactory" alias="entityManagerFactory" />
</beans>
componentContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"
xmlns:jpa="http://www.springframework.org/schema/data/jpa">
<context:component-scan base-package="com.volvo.myfirstdomain.base />
<context:annotation-config />
<!-- JPA EntityManagerFactory -->
<!-- Based on choice of implementation between OpenJPA and Hibernate, parent attribute in bean should be set. For OpenJPA: parent = "openJpaEntityManagerFactory"
For Hibernate: parent = "hibernateEntityManagerFactory" Here, OpenJPA is chosen as you can see below. -->
<bean id="myDomainEntityManagerFactory" parent="entityManagerFactory">
<property name="persistenceUnitName" value="MYDomainPU" />
<property name="packagesToScan" value="com.volvo.myfirstdomain.entity" />
<property name="jpaProperties">
<props>
<prop key="hibernate.default_schema">DOMAIN_ONE</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect </prop>
<prop key="hibernate.cache.region.factory_class">com.hazelcast.hibernate.HazelcastCacheRegionFactory</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="javax.persistence.sharedCache.mode">ENABLE_SELECTIVE</prop>
</props>
</property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg ref="dataSource" />
</bean>
</beans>
I have tried debugging multiple times but I am not able to figure out why the initialzing is happening multiple times (as if in a loop). Any pointers greatly appreciated!
I finally figured out the reason! There was an additional #ComponentScan annotation specified in the application configuration class which was causing the loop to be triggered.
Related
Having spent days trying to get my head around AOP, I have no option than to lay out my code and hope the good guys on the internet will help me.
My dispatcher-servlet.xml:
<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?xml version=".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"
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/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
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.company.project"/>
<mvc:annotation-driven/>
<mvc:resources mapping="/resources/**" location="/WEB-INF/resources/, file:${catalina.home}/project/resources/" />
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<!--
The index controller.
-->
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
</beans>
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: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.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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<aop:aspectj-autoproxy proxy-target-class="true"/>
<context:component-scan base-package="com.company.project"/>
<!-- Transaction Management -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>/WEB-INF/jdbc.properties</value>
<value>/WEB-INF/c3p0.properties</value>
<value>/WEB-INF/application.properties</value>
</list>
</property>
</bean>
<!-- With c3p0-->
<bean id="dataSource" 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}" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.company.project.model"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<!-- <prop key="hibernate.current_session_context_class">thread</prop>-->
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.id.new_generator_mappings">false</prop>
<prop key="net.sf.ehcache.configurationResourceName">ehcache.xml</prop>
<prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
<prop key="hibernate.connection.driver_class">${jdbc.driverClassName}</prop>
<prop key="hibernate.connection.url">${jdbc.url}</prop>
<prop key="hibernate.connection.username">${jdbc.username}</prop>
<prop key="hibernate.connection.password">${jdbc.password}</prop>
<!-- <prop key="hibernate.connection.provider_class">org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider</prop>-->
<prop key="hibernate.c3p0.acquire_increment">1</prop>
<prop key="hibernate.c3p0.idle_test_period">300</prop>
<prop key="hibernate.c3p0.max_size">20</prop>
<prop key="hibernate.c3p0.max_statements">100</prop>
<prop key="hibernate.c3p0.min_size">3</prop>
<prop key="hibernate.c3p0.preferredTestQuery">SELECT 1;</prop>
<prop key="hibernate.c3p0.testConnectionOnCheckin">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="shiroFilter" class= "org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
<property name="loginUrl" value="/verify.jsp"/>
<property name="successUrl" value="/index"/>
<property name="filterChainDefinitions">
<value>
/verify.jsp = authc
/secure/** = authc
/logout.htm = logout
/api/test/** = anon
/api/** = jwtv
</value>
</property>
</bean>
<bean id="logout" class="org.apache.shiro.web.filter.authc.LogoutFilter">
<property name="redirectUrl" value="/login.jsp"/>
</bean>
<bean id="annon" class="org.apache.shiro.web.filter.authc.AnonymousFilter">
</bean>
<bean id="jwtg" class="com.company.project.model.api.ApiGuard">
</bean>
<bean id="jwtv" class="com.company.project.model.api.JWTVerifyingFilter">
</bean>
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!-- Single realm app. If you have multiple realms, use the 'realms' property instead. -->
<property name="realm" ref="jdbcRealm"/>
<property name="cacheManager" ref="cacheManager"/>
<!-- By default the servlet container sessions will be used. Uncomment this line
to use shiro's native sessions (see the JavaDoc for more): -->
<!-- <property name="sessionMode" value="native"/> -->
</bean>
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
<!-- Enable Shiro Annotations for Spring-configured beans. Only run after
the lifecycleBeanProcessor has run: -->
<bean id="annotationProxy"
class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
depends-on="lifecycleBeanPostProcessor" />
<bean
class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager" />
</bean>
<bean id="jdbcRealm" class="org.apache.shiro.realm.jdbc.JdbcRealm">
<property name="credentialsMatcher" ref = "sha256Matcher"/>
<property name="authenticationQuery" value = "SELECT password, salt FROM admin WHERE email = ?"/>
<property name="permissionsLookupEnabled" value = "true"/>
<property name="userRolesQuery" value = "SELECT role_name FROM admin_role WHERE email = ?"/>
<property name="permissionsQuery" value = "SELECT permission FROM roles_permission WHERE role_name = ?"/>
<property name="saltStyle" value = "COLUMN"/>
<property name="dataSource" ref = "dataSource"/>
</bean>
<bean id="sha256Matcher" class="org.apache.shiro.authc.credential.HashedCredentialsMatcher" >
<property name="hashAlgorithmName" value = "SHA-256"/>
</bean>
<bean id="cacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager" />
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10000000"/>
</bean>
</beans>
If I comment out the part:
<bean id="annotationProxy"
class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
depends-on="lifecycleBeanPostProcessor" />
<bean
class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager" />
</bean>
the project runs but the #RequiresPermissions annotation is useless. When I run the project with these config Error is:
[http-nio-8084-exec-100] WARN
org.springframework.web.context.support.XmlWebApplicationContext -
Exception encountered during context initialization - cancelling
refresh attempt:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'myController': Unsatisfied
dependency expressed through field 'myService'; nested exception
is org.springframework.beans.factory.BeanNotOfRequiredTypeException:
Bean named 'myService' is expected to be of type
[com.company.project.service.myService] but was actually of type
[com.sun.proxy.$Proxy530] [http-nio-8084-exec-100] ERROR
org.springframework.web.context.ContextLoader - Context initialization
failed
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'myController': Unsatisfied
dependency expressed through field 'myService'; nested exception
is org.springframework.beans.factory.BeanNotOfRequiredTypeException:
Bean named 'myService' is expected to be of type
[com.company.project.service.myService] but was actually of type
[com.sun.proxy.$Proxy530] at
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:569)
My controller classes are smth like:
#RestController
#RequestMapping(value = "/api/my")
public class MyApis {
#Autowired
MyService myService;
#RequestMapping(method = RequestMethod.GET, value = "/profile/save")
#RequiresPermissions("profile:save")
public void saveProfile(#RequestParam(value = "first-name") String firstName, #RequestParam(value = "last-name") String lastName,HttpServletRequest request) {
Profile profile = new Profile();
profile.setFirstName(firstName);
profile.setLastName(lastName);
myService.insert(profile);
}
}
Please help me figure out setting up Shiro annotations. Thank you.
Shiro has a Spring Boot example that uses Shiro's annotations on a controller. And a non-boot example too
I work with Spring MVC. I have a dynamic web project.
Project structure is
I have a correos.properties in /WEB-INF/classes.
correos.properties is
correosalida = xxx
I want to get a property from correos.properties
dispatcher is
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.jasypt.org/schema/encryption
http://www.jasypt.org/schema/encryption/jasypt-spring31-encryption-1.xsd">
<context:annotation-config />
<import resource="hibernate-context.xml" />
<context:component-scan base-package="eusurvey" />
<bean id="propertyPlaceholderConfigurer"
class="org.jasypt.spring31.properties.EncryptablePropertyPlaceholderConfigurer">
<constructor-arg ref="configurationEncryptor" />
<property name="location" value="/WEB-INF/spring.properties" />
</bean>
<bean id="configurationEncryptor" class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">
<property name="config" ref="environmentVariablesConfiguration" />
</bean>
<bean id="environmentVariablesConfiguration"
class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig">
<property name="algorithm" value="PBEWITHSHA256AND256BITAES-CBC-BC" />
<property name="passwordEnvName" value="CAS_PBE_PASSWORD" />
<property name="providerClassName"
value="org.bouncycastle.jce.provider.BouncyCastleProvider" />
<property name="providerName" value="BC" />
</bean>
<mvc:annotation-driven />
<mvc:view-controller path="encuesta/*"
view-name="encuesta/actualizarCorreoC.jsp" />
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="WEB-INF/classes/messages" />
</bean>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<mvc:resources mapping="/resources/**" location="/resources/" />
<context:property-placeholder location="WEB-INF/classes/correos.properties" order="1" ignore-unresolvable="true" />
</beans>
LeerProperties.java is
package eusurvey.auxiliar;
import org.springframework.beans.factory.annotation.Value;
public class LeerProperties {
#Value("${correos.correosalida}")
private String correosalida;
public String PropertyValue() {
return correosalida;
}
}
Value of correosalida is null.
How can I get a property from correos.properties?
Did you try to reach the file using classpath?
It should be like this:
<context:property-placeholder location="classpath:correos.properties" order="1" ignore-unresolvable="true" />
EDIT:
Try to load this way:
<util:properties id="correos" location="classpath:correos.properties"/>
And shouldn't be with a "#" instead of a "$"?
#Value("#{correos.correosalida}")
Please help me! I am modifiyng my spring mwc application. But I am not able to understand what does mean one tag in the webmvc-config.xml file . I mean why it is written common.index and not common/index ? In fact into the directory view/common there is the file index.jsp.
<?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<mvc:annotation-driven />
<!-- ================= MISC. ================= -->
<!-- Message properties -->
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>classpath:library</value>
</list>
</property>
</bean>
<!-- Declare the Interceptor -->
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
</mvc:interceptors>
<!-- Declare the Resolver -->
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />
<!-- Forwards requests to the "/" resource to the "home" view -->
<mvc:view-controller path="/" view-name="common.index" />
<mvc:view-controller path="/index.do" view-name="common.index" />
<mvc:view-controller path="/common/ok.do"
view-name="common.ok" />
<mvc:view-controller path="/common/welcome.do"
view-name="common.welcome" />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving
up static resources in the ${webappRoot}/resources/ directory -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<!-- ================= TILES ================= -->
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/spring/tiles-defs.xml</value>
</list>
</property>
</bean>
<bean id="tilesViewResolver"
class="org.springframework.web.servlet.view.tiles3.TilesViewResolver" />
<!-- ================= Upload Resolver (Roberto) ================= -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes (1 MB)-->
<property name="maxUploadSize" value="1048576" />
</bean>
<context:component-scan base-package="it.univaq.mwt.library.presentation" />
</beans>
From spring 3.0 documentation
This tag is a shorcut for defining a ParameterizableViewController
that immediately forwards to a view when invoked. Use it in static cases
when there is no Java Controller logic to execute before the view generates
the response
I am facing a problem to wire entity manager with the bean present in application context.
whenever i do some operation it gives NullPointerException.
this is my applicationContext.xml file
<?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"
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">
<context:component-scan base-package="com.ajit.retail"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://localhost/retail"/>
<property name="username" value="retail_user"/>
<property name="password" value="password"/>
</bean>
<bean id="entityManagerOne" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true"/>
<property name="databasePlatform" value="org.hibernate.dialect.PostgreSQL82Dialect"/>
</bean>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerOne"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
this is the java file in which i am creating the entity manager
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
public class AbstractRepository {
#PersistenceContext
EntityManager entityManager;
}
so whenever i use this entity manager it gives null pointer exception
please help!
Your entity manager bean is called enetityManagerOne, but the variable is called entityManager. Maybe renamme your bean in your XML file:
<bean id="entityManager" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
Another solution could be you forgot the following bean declarations:
For the support for transaction:
<tx:annotation-driven/>
The support for parsing JPA annotations:
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
my application context was not on the correct place (src/main/resources). Now I put that there and its working.
Use this code in your dispatcher-servlet.xml
<bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="persistenceXmlLocations">
<list>
<value>classpath*:META-INF/persistence.xml</value>
</list>
</property>
<property name="defaultDataSource" ref="dataSource"/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitManager" ref="persistenceUnitManager"/>
<property name="persistenceUnitName" value="etray"/>
</bean>
<jee:jndi-lookup id="dataSource" jndi-name="java:/prateek" />
<!-- we plan to use JTA transactions and declare them using annotations -->
<bean id="transactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager" />
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- to inject instances of EntityManager using #PersistenceContext annotation -->
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
it resolve my problem, hope it will help.
I'm using
<mvc:resources mapping="/resources/**" location="/resources/" />
To handle static content in my SpringMVC app. This works fine. I am able to upload images and retrieve them within the application using the "/resources" url .I want to use an jpeg for a background image in a jsp, so I though I would simply drop the jpeg into the resources folder in my workspace. However, there is no resource folder in my workspace folder anywhere. Where is spring storing these images? How do I get to this folder to add my background image?
as requested web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web- app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>SpringMVCTest</display-name>
<servlet>
<servlet-name>springMVCTest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVCTest</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
servlet.xml to clarify everything is configured properly.
<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<tx:annotation-driven />
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
<context:component-scan base-package="/"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:maxUploadSize="1000000" />
<bean id="personService" class="PersonServiceImpl" />
<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/test" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="/" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
The call to the save image method
saveImage(context.getRealPath("/resources/" + person.getId() + ".jpg"), image);
However, there is no resource folder in my workspace folder anywhere
It should be present in WebContent. If its not there then <mvc:resource> is not being used to serve static content.
How do I get to this folder to add my background image?
Create resources folder under WebContent and put your images into it. Access the images by /resources/foo.jpeg.