Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Skulled Filed
How to use custom Spring service in Liferay
June 27, 2013 2:42 PM
Answer

Skulled Filed

Rank: New Member

Posts: 13

Join Date: March 22, 2011

Recent Posts

Hello,

I don't want to use Liferay Service builder, I need to use Spring Classic implementation Service and JPA (Hibernate) for persistence.

But I have some problem with beans injection. I think Liferay is in conflict with my spring beans autowired injection.

It is possible to implement in Liferay 6.1.1 our custom service ?

If I declare only one Portlet all is work. But if I declare a second portlet my spring injection service doesn't work.

Controller portlet1 :


 1   @Controller
 2    @RequestMapping( "VIEW" )
 3    public class Portlet1Controller
 4    {
 5        @Autowired
 6        private ClientService clientService;
 7               
 8        public void setClientService( ClientService clientService )
 9        {
10            this.clientService = clientService;
11        }
12   
13        @RequestMapping
14        public String doView( final Model model, final RenderRequest request, final RenderResponse response, final PortletSession session ) throws Exception
15        {
16            clientService.saveClient(new ClientEntity("henry");
17            return "view1";
18            
19        }


ServiceImpl.java

 1       @Service
 2        public class ClientServiceImpl implements ClientService
 3        {
 4            @Autowired
 5            private ClientDao clientDao;
 6       
 7            @Override
 8            @Transactional
 9            public void saveClient( ClientEntity client ) throws DataAccessException
10            {
11                clientDao.save(client);
12            }
13    }


DaoImpl.java
 1    @Repository
 2    public class ClientDaoImpl implements ClientDao {
 3
 4        @PersistenceContext
 5        private EntityManager em;
 6   
 7        @Override
 8        public void save(ClientEntity client) {
 9            if (client.getId() == null) {
10                this.em.persist(client);            
11            }
12            else {
13                this.em.merge(client);   
14            }
15        }

It's work in a java classic web application but not in Liferay.

I see that I can declare in properties "spring.configs=" but I don't understant this properties.


Thanks for help.

My Error log:
 121:33:29,345 ERROR [DispatcherPortlet:276] Context initialization failed
 2org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'clientServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private fr.kitech.cash.dao.ClientDao fr.kitech.cash.service.impl.ClientServiceImpl.clientDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'clientDaoImpl': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined
 3    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
 4    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1116)
 5    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
 6    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
 7    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
 8    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
 9    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
10    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
11    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626)
12    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
13    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
14    at org.springframework.web.portlet.FrameworkPortlet.createPortletApplicationContext(FrameworkPortlet.java:357)
15    at org.springframework.web.portlet.FrameworkPortlet.initPortletApplicationContext(FrameworkPortlet.java:294)
16    at org.springframework.web.portlet.FrameworkPortlet.initPortletBean(FrameworkPortlet.java:268)
17    at org.springframework.web.portlet.GenericPortletBean.init(GenericPortletBean.java:124)
18    at javax.portlet.GenericPortlet.init(GenericPortlet.java:107)
19    at com.liferay.portlet.InvokerPortletImpl.init(InvokerPortletImpl.java:256)
20    at com.liferay.portlet.PortletInstanceFactoryImpl.init(PortletInstanceFactoryImpl.java:221)
21    at com.liferay.portlet.PortletInstanceFactoryImpl.create(PortletInstanceFactoryImpl.java:140)
22    at com.liferay.portlet.PortletInstanceFactoryUtil.create(PortletInstanceFactoryUtil.java:41)
23    at com.liferay.portlet.PortletBagFactory.create(PortletBagFactory.java:310)
24    at com.liferay.portal.deploy.hot.PortletHotDeployListener.initPortlet(PortletHotDeployListener.java:546)
25    at com.liferay.portal.deploy.hot.PortletHotDeployListener.doInvokeDeploy(PortletHotDeployListener.java:321)
26    at com.liferay.portal.deploy.hot.PortletHotDeployListener.invokeDeploy(PortletHotDeployListener.java:120)
27    at com.liferay.portal.deploy.hot.HotDeployImpl.doFireDeployEvent(HotDeployImpl.java:188)
28    at com.liferay.portal.deploy.hot.HotDeployImpl.fireDeployEvent(HotDeployImpl.java:96)
29    at com.liferay.portal.kernel.deploy.hot.HotDeployUtil.fireDeployEvent(HotDeployUtil.java:27)
30    at com.liferay.portal.kernel.servlet.PluginContextListener.fireDeployEvent(PluginContextListener.java:151)
31    at com.liferay.portal.kernel.servlet.PluginContextListener.doPortalInit(PluginContextListener.java:141)
32    at com.liferay.portal.kernel.util.BasePortalLifecycle.portalInit(BasePortalLifecycle.java:42)
33    at com.liferay.portal.kernel.util.PortalLifecycleUtil.register(PortalLifecycleUtil.java:64)
34    at com.liferay.portal.kernel.util.PortalLifecycleUtil.register(PortalLifecycleUtil.java:56)
35    at com.liferay.portal.kernel.util.BasePortalLifecycle.registerPortalLifecycle(BasePortalLifecycle.java:52)
36    at com.liferay.portal.kernel.servlet.PluginContextListener.contextInitialized(PluginContextListener.java:103)
37    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4779)
38    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5273)
39    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
40    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:895)
41    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:871)
42    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:615)
43    at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1099)
44    at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1621)
45    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
46    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
47    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
48    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
49    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
50    at java.lang.Thread.run(Thread.java:662)
51Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private fr.kitech.cash.dao.ClientDao fr.kitech.cash.service.impl.ClientServiceImpl.clientDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'clientDaoImpl': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined
52    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514)
53    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
54    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
55    ... 47 more
David H Nebinger
RE: How to use custom Spring service in Liferay
June 28, 2013 6:33 AM
Answer

David H Nebinger

Rank: Liferay Legend

Posts: 7227

Join Date: September 1, 2006

Recent Posts

No qualifying bean of type (javax.persistence.EntityManagerFactory) is defined


When doing just spring, you need to be aware of the spring messages. Looks like your exception is indicating you do not have an entity manager factory defined in your spring context.