1

我不想使用 Liferay Service builder,我需要使用 Spring Classic 实现 Service 和 JPA (Hibernate) 进行持久化。

但是我对豆类注入有一些问题。我认为 Liferay 与我的 spring beans autowired 注射相冲突。

是否可以在 Liferay 6.1.1 中实现我们的自定义服务?

如果我只声明一个 Portlet,那么一切正常。但是,如果我声明第二个 portlet,我的 spring 注入服务将不起作用。

控制器 portlet1:

@Controller
@RequestMapping( "VIEW" )
public class Portlet1Controller
{
    @Autowired
    private ClientService clientService;

    public void setClientService( ClientService clientService )
    {
        this.clientService = clientService;
    }

    @RequestMapping
    public String doView( final Model model, final RenderRequest request, final RenderResponse response, final PortletSession session ) throws Exception
    {
        clientService.saveClient(new ClientEntity("henry");
        return "view1";

    }

ServiceImpl.java

    @Service
    public class ClientServiceImpl implements ClientService
    {
        @Autowired
        private ClientDao clientDao;

        @Override
        @Transactional
        public void saveClient( ClientEntity client ) throws DataAccessException
        {
            clientDao.save(client);
        }
}

DaoImpl.java

@Repository
public class ClientDaoImpl implements ClientDao {

@PersistenceContext
private EntityManager em;

@Override
public void save(ClientEntity client) {
    if (client.getId() == null) {
        this.em.persist(client);            
    }
    else {
        this.em.merge(client);    
    }
}

它适用于 Java 经典 Web 应用程序,但不适用于 Liferay。

我看到我可以在属性中声明,spring.configs=但我不明白这个属性。

错误日志:

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
4

0 回答 0