I would like to try embedded database for testing my DAO objects in Spring application.
In application context I have this tag:
<jdbc:embedded-database id="dataSourceEmbedded" type="HSQL">
<jdbc:script location="classpath:/embeddeddb/schema.sql" />
<jdbc:script location="classpath:/embeddeddb/data.sql" />
</jdbc:embedded-database>
my JUnit test class needs to work with this bean:
import org.apache.log4j.Logger;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration("classpath:/applicationContext.xml")
public class PartnerDAOTest {
#Autowired
#Qualifier("dataSourceEmbedded")
public EmbeddedDatabase dataSourceEmbedded;
#Test
public void testSavePartner() {
}
}
everything works ("dataSourceEmbedded" bean is created) but when I try to autowire them in PartnerDAOTest class Spring throws this exception:
testSavePartner(sandbox.PartnerDAOTest):
Error creating bean with name
'sandbox.PartnerDAOTest': Injection of
autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not
autowire field: public
org.springframework.jdbc.datasource.embedded.EmbeddedDatabase
sandbox.PartnerDAOTest.dataSourceEmbedded;
nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type
[org.springframework.jdbc.datasource.embedded.EmbeddedDatabase] found
for dependency: expected at least 1 bean which qualifies as autowire
candidate for this dependency. Dependency annotations:
{#org.springframework.beans.factory.annotation.Autowired(required=true),
#org.springframework.beans.factory.annotation.Qualifier(value=dataSourceEmbedded)}
What is wrong with my code?
As #M.Deinum noticed in his comment:
There is no bean of the type EmbeddedDatabase there is a DataSource.
The embedded-database eventually creates a DataSource not an
EmbeddedDatabase.
Related
I am trying to upgrade my project to Spring 5.0.6 and Spring Data 2.0.6 and we are using WebSphere managed JPA 2.1
Getting below exception while creating the Spring context:
[5/29/18 6:30:41:929 CDT] 00000052 SystemOut O 06:30:41.876 [Default : 5] ERROR org.springframework.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userService': Unsatisfied dependency expressed through field 'userRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.mmm.arc.dao.UserRepository' available: expected at least 1 bean which qualifies as autowire candidate.
...
service class:
#Transactional(propagation = Propagation.REQUIRED )
public class UserServiceImpl implements UserService, UserDetailsService {
#PersistenceContext
private EntityManager em;
#Autowired
UserRepository userRepository;
}
repository class:
public interface UserRepository extends JpaRepository<User, String>, JpaSpecificationExecutor< User >,UserRepositoryCustom{
}
public class UserRepositoryImpl implements UserRepositoryCustom{
#PersistenceContext
EntityManager em;
-----------methods
}
in schema i have not mentioned any spring version
<jpa:repositories base-package="repo class package" /><bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
<property name="persistenceUnitName" value="persistance unit name"/>
<property name="dataSource" ref="dataSource"/>
</bean>
we have separate persistence.xml file to map all entities which are completely annotation based
Your bean UserRepositoryImpl doesn't implement interface UserRepository, which is the type that UserServiceImpl is using. Thus, I think there actually is no UserRepository instance created anywhere.
Perhaps you intended for UserRepositoryImpl to implement UserRespository instead of UserRepositoryCustom?
I couldn't create Spring Solr Repository.
Below is my bean configuration
<context:annotation-config />
<solr:solr-server id="solrServer" url="http://soms-env.staples.com:8983/solr" />
<bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate"
scope="singleton">
<constructor-arg ref="solrServer" />
</bean>
<bean id="searchRepository"
class="com.solr.repository.SolrCurrencyRepository">
<property name="solrOperations" ref="solrTemplate" />
</bean>
and the code used in my controller is
#Autowired
private SolrCurrencyRepository searchRepository;
Getting below error all the time.
SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mainController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.solr.repository.SolrCurrencyRepository Controller.MainController.searchRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.solr.repository.SolrCurrencyRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
Am I missing anything to instantiate Repository.
I need to create a simple REST-service with some fields I want to export from applicatonContext.xml file, but I've got
Stack-Trace
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 's3RestController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String com.emc.sk.data_mgmt_central.server.S3RestController.path; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true), #org.springframework.beans.factory.annotation.Qualifier(value=path)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1204)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:725)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:109)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:952)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:941)
at com.emc.sk.data_mgmt_central.main.Main.main(Main.java:39)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String com.emc.sk.data_mgmt_central.server.S3RestController.path; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true), #org.springframework.beans.factory.annotation.Qualifier(value=path)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:555)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 16 common frames omitted
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true), #org.springframework.beans.factory.annotation.Qualifier(value=path)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1261)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1009)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:904)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:527)
... 18 common frames omitted
I run application with
Main class
#ComponentScan("com.emc.sk.data_mgmt_central.server")
#EnableAutoConfiguration
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}
And here is my
RestController
#RestController
#ContextConfiguration("/FileStorageContext.xml")
public class S3RestController {
#Autowired
private String path;
#RequestMapping("/getBucket")
public Bucket getBucket(#RequestParam(value="name", defaultValue="world") String name) {
return new Bucket(name);
}
}
My applicationContext is called FileStorageContext and stored in src/main/recourses (I've got a maven project)
FileStorageContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name = "path" class="java.lang.String">
<constructor-arg type="java.lang.String" value="#{ systemProperties['java.io.tmpdir'] }bucketDir" />
</bean>
#Autowired works properly in my tests, but here this error occurs. I am new in Spring, maybe this construction is not correct at all. Thanks.
Add #PropertySource("classpath:/your/properties/path/applicatio.properties")
to your controller and add #Value tag to path instead of #Autowired.
#Value("${java.io.tmpdir}")
private String path;
I had to put
#ImportResource("classpath:/FileStorageContext.xml") before my Main class, the problem was that this file wasn't seen.
This line causes your exception:
#Autowired
private String path;
Autowiring works only on Spring managed beans and String should not be considered as one (IMO).
You may use #Value annotation if you want to get the value of path from your context. Or if you want to use as is, you can try #Qualifier("path") annotation along with #Autowired.
I´m trying to configure a Bean to be used in a Spring MVC Controller app using the #Bean annotation.
As far as I know, the #Bean annotation is equivalent to the tag in XML configuration. What I am doing is the following:
The class with the configuration
#Configuration
public class ContextConfig {
#Bean
public MyBean myBean() {
return new MyBean();
}
}
But when trying to autowire myBean in an MVC Controller it fails.
The bean injection
#Controller
public class HomeController {
#Autowired
private MyBean myBean;
#RequestMapping({"/", "/home"})
public ModelAndView home (ModelAndView model) {
model.setViewName("home");
return model;
}
}
The error is:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'homeController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.validation.ValidatorFactory com.proeza.sgs.controller.HomeController.factory; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.validation.ValidatorFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.validation.ValidatorFactory com.proeza.sgs.controller.HomeController.factory; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.validation.ValidatorFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
... 22 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.validation.ValidatorFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1100)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:960)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:855)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
... 24 more
Final comments
There are other beans configured via annotation (#Bean), that are being injected without problem.
The MyBean class is not implementing nor extending anything.
The configuration location is declared in the web.xml
Conf:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.myapp.config</param-value>
</context-param>
The ContextConfig class is in that package
Any idea why is this happening?
The exception is not related to the MyBean class. The exception says:
No qualifying bean of type [javax.validation.ValidatorFactory] found for dependency
Spring failed to inject javax.validation.ValidatorFactory to the HomeController class. Check that configuration for ValidatorFactory class.
Thanks for the response Ilya.
You´re right, the stack I pasted was not the correct one. The cause was that to simplify, I gave the example with MyBean and I forgot to rename it in the stack.
The problem has been solved. The real name of MyBean is MessageResolver and what was happening was that I already had another method called messageResolver inside the ContextConfig class, used to create a bean for Thymeleaf. The difference is that mine does not receive parameters and the other receives a MessageSource, BUT...Reading the Spring docmentation I found this:
Spring Doc Quote:
2.2.6. Customizing bean naming
By default, JavaConfig uses a #Bean method's name as the name of the
resulting bean. This functionality can be overridden, however, using
the BeanNamingStrategy extension point.
Spring #Bean doc
So, the problem is that the bean was not being created because it was being overrided its definition.
The error disappears just renaming the messageResolver method.
Thanks again. Regards!
I'd like to make use of spring-data-jpa and CrudRepository<T, ID extends Serializable>. But I cannot get it autowired. (all other services in the same package are wired correctly):
#Service
public class UserService {
#Resource
private UserRepo repo;
//this does neither work
//#Autowired
//private CrudRepository<User, Long> repo;
}
public interface UserRepo extends CrudRepository<User, Long> {
}
#Entity
public class User {
#Id
private Long id;
}
Result:
Caused by: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'userService': Injection of resource
dependencies failed; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type [UserRepo] found for dependency: expected at
least 1 bean which qualifies as autowire candidate for this
dependency. Dependency annotations:
{#javax.annotation.Resource(shareable=true, mappedName=, description=,
name=, type=class java.lang.Object, lookup=,
authenticationType=CONTAINER)}
What might be wrong here?
From the docs I think it should work without writing an implementation:
In a typical Java application, you’d expect to write a class that
implements CustomerRepository. But that’s what makes Spring Data JPA
so powerful: You don’t have to write an implementation of the
repository interface. Spring Data JPA creates an implementation on the
fly when you run the application.
Updated SpringConfig:
#Configuration
#EnableTransactionManagement
#EnableJpaRepositories("my.package")
public class AppConfig {
#Bean
public LocalContainerEntityManagerFactoryBean emf() throws ClassNotFoundException, PropertyVetoException {
LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
emf.setDataSource(dataSource());
emf.setPackagesToScan("my.package");
emf.setJpaVendorAdapter(jpaAdapter());
emf.setJpaProperties(jpaProterties());
return emf;
}
}
Result: emf is missing, which is strange as I already have working DAO serices where I can autowire EntityManager and EMF without any problem.
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#1a6e658': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:336)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:632)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:442)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1094)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:989)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:276)
... 50 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:641)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1159)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:282)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
... 58 more
You should verify the following:
1) Annotate the UserRepo with #Repository.
2) Your spring beans xml file should have in it:
<jpa:repositories base-package="your.repository.package"></jpa:repositories>
3) I'd recommend injecting this type of bean with #Autowired instead of #Resource
UPDATE
it seems you did my first 3 steps and you're one step ahead now. Now, remember that in Java Config methods names DO matter. Either change emf() to entityManagerFactory() (which is more "standard"), or set entity-manager-factory-ref to emf. Example:
#Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws ClassNotFoundException, PropertyVetoException {
LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
emf.setDataSource(dataSource());
emf.setPackagesToScan("my.package");
emf.setJpaVendorAdapter(jpaAdapter());
emf.setJpaProperties(jpaProterties());
return emf;
}
Doesn't UserRepo needs to be annotated with :
#Repository
It must be an issue in your applicationContext configuration. Here is the one I used to made it work:
<jpa:repositories
base-package="package.containing.your.repositories"
factory-class="org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean"
transaction-manager-ref="transactionManager"
entity-manager-factory-ref="entityManagerFactory" />
<bean
id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManagerFactory" />
<bean
id="hibernateJpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
<bean
id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="dataSource"
p:persistenceUnitName="PU"
p:jpaVendorAdapter-ref="hibernateJpaVendorAdapter"
p:jpaProperties="classpath:hibernate.properties" />
<util:property-path id="sessionFactory" path="entityManagerFactory.sessionFactory" />
Configuring spring data JPA using XML and annotations
//This is the two way to configure spring data JPA
#EnableJpaRepositories (basePackages= "Repository Bean is located in package", entityManagerFactoryRef= "Here is your configuration of EntityManagerFactoryBean")
<jpa:repositories base-package= "Repository Bean where package"
Entity-manager-factory-ref= "Here is your configured EntityManagerFactoryBean" ></jpa:repositories>
// Sample code:
#Configuration
#EnableTransactionManagement
#EnableJpaRepositories(basePackages="com.zxg.springdata",
entityManagerFactoryRef="factoryBean")
public class SpringDataConfig {
#Bean
public LocalContainerEntityManagerFactoryBean factoryBean (
ComboPooledDataSource dataSource, JpaVendorAdapter jpaVendorAdapter
){
LocalContainerEntityManagerFactoryBean factoryBean = new
LocalContainerEntityManagerFactoryBean ();
FactoryBean.setDataSource (dataSource);
FactoryBean.setJpaVendorAdapter (jpaVendorAdapter);
FactoryBean.setPackagesToScan ("com.zxg.springdata");
Return factoryBean;
}
}