留言板

Spring IOC in liferay project maven multi module

Stephane Franchamp,修改在9 年前。

Spring IOC in liferay project maven multi module

New Member 帖子: 19 加入日期: 14-8-7 最近的帖子
Hi,

I want to do a new project with multi-module maven project with Spring :

- The first Module is a java project how represent the Core with (DAO,Model,Services)
- The second Module is my LiferayPortlet

So any ideas how to inject the Spring Services in the Liferay Portlet ? Any exemple ?


Thanks
thumbnail
David H Nebinger,修改在9 年前。

RE: Spring IOC in liferay project maven multi module

Liferay Legend 帖子: 14919 加入日期: 06-9-2 最近的帖子
If you use the liferay service builder template, it already will generate a multi-module project.
Stephane Franchamp,修改在9 年前。

RE: Spring IOC in liferay project maven multi module

New Member 帖子: 19 加入日期: 14-8-7 最近的帖子
Yes of course i know it, but i want to set my own architecture with java Project as core emoticon

Thanks
thumbnail
David H Nebinger,修改在9 年前。

RE: Spring IOC in liferay project maven multi module

Liferay Legend 帖子: 14919 加入日期: 06-9-2 最近的帖子
Unfortunately I'm not really a maven expert. I do know that based upon the pom the maven build process will invoke the liferay maven plugins at the appropriate point in the build process. I'm sure it's possible to write your own pom to similarly get the liferay maven plugins invoked at the right points in the build process, but it's beyond what I've ever needed to accomplish... emoticon
Stephane Franchamp,修改在9 年前。

RE: Spring IOC in liferay project maven multi module

New Member 帖子: 19 加入日期: 14-8-7 最近的帖子
Ok Thanks, but have you any ideas how to invoke the Spring Service in the portlet ?

I'am using the <portlet-class>org.springframework.web.portlet.DispatcherPortlet</portlet-class> in my portlet.xml but i still have an error :

No matching bean of type [org.hibernate.SessionFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value=sessionFactory)}

of course the hibernate session is in the core project emoticon

Thanks
thumbnail
David H Nebinger,修改在9 年前。

RE: Spring IOC in liferay project maven multi module

Liferay Legend 帖子: 14919 加入日期: 06-9-2 最近的帖子
So when you do a service builder portlet, it creates some standard spring xml files, one of which (if I remember correctly) is like base or something, but it includes the hibernate session bean reference from Liferay.
thumbnail
Wouter Vernaillen,修改在9 年前。

RE: Spring IOC in liferay project maven multi module

Junior Member 帖子: 80 加入日期: 09-6-6 最近的帖子
Stephane Franchamp:

So any ideas how to inject the Spring Services in the Liferay Portlet ? Any exemple ?
Thanks


You can define your service project as a dependency in the pom.xml of your Liferay portlet project. The service project will then automatically be included as a jar in the portlet project.
This makes the DAO, model and service classes available to the Liferay portlet project, allowing you to include the packages in the component scanner of the spring application context.
Stephane Franchamp,修改在9 年前。

RE: Spring IOC in liferay project maven multi module

New Member 帖子: 19 加入日期: 14-8-7 最近的帖子
Hi Wouter Vernaillen thanks, but that what i do i add the core project as dependency to my spring portlet

This is my spring-sceleton-portlet.xml :

<context:component-scan base-package="com.test.demos" />
<context:component-scan base-package="com.test.demos.actions" />

<bean id="objectFactory"
class="com.test.demos.service.ContactService"></bean>
<bean id="object1" factory-bean="objectFactory" factory-method="createInstance" />

and in the portlet.xml :

<portlet-class>org.springframework.web.portlet.DispatcherPortlet</portlet-class>
<init-param>
<name>contextConfigLocation</name>
<value>/WEB-INF/spring-sceleton-portlet.xml</value>
</init-param>
<expiration-cache>0</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>view</portlet-mode>
</supports>

in the web.xml :

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/application-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>com.liferay.portal.kernel.servlet.SecurePluginContextListener</listener-class>
</listener>
<listener>
<listener-class>com.liferay.portal.kernel.servlet.PortletContextListener</listener-class>
</listener>

<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>ViewRendererServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.ViewRendererServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ViewRendererServlet</servlet-name>
<url-pattern>/WEB-INF/servlet/view</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<jsp-config>
<taglib>
<taglib-uri>http://java.sun.com/portlet_2_0</taglib-uri>
<taglib-location>/WEB-INF/tld/liferay-portlet.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>

also in the application-context.xml


<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/html/jsps/" />
<property name="suffix" value=".jsp" />
</bean>

in my calss Java :



@RequestMapping("VIEW")
public class DemosActions extends MVCPortlet {
private static Log _log = LogFactoryUtil.getLog(DemosActions.class);

@BeanReference(name = "object1")
private ContactService contactService;


and i get this error on deploying :

No matching bean of type [org.hibernate.SessionFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value=sessionFactory)}
thumbnail
Wouter Vernaillen,修改在9 年前。

RE: Spring IOC in liferay project maven multi module

Junior Member 帖子: 80 加入日期: 09-6-6 最近的帖子
I guess your DAO classes are autowiring hibernate sessionFactory and the sessionfactory cannot be found.

You will have to define the hibernate sessionFactory in the application-context.xml of your liferay portlet project.
Stephane Franchamp,修改在9 年前。

RE: Spring IOC in liferay project maven multi module

New Member 帖子: 19 加入日期: 14-8-7 最近的帖子
but i have the hibernate sessionFactory in the application-context.xml of the java core Project :

application-context.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: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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- Hibernate SessionFactory -->
<!-- Session Factory Declaration -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.test.demos.model" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.enable_lazy_load_no_trans">true</prop>
<!-- <prop key="hibernate.default_schema">demos</prop> -->
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
<property name="fetchSize" value="20" />
<property name="allowCreate" value="true" />
<property name="alwaysUseNewSession" value="false" />
</bean>

<!-- Enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="txManager"
proxy-target-class="true" />
<!-- Transaction Manager is defined -->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
thumbnail
Wouter Vernaillen,修改在9 年前。

RE: Spring IOC in liferay project maven multi module

Junior Member 帖子: 80 加入日期: 09-6-6 最近的帖子
The Spring ContextLoaderListener (defined in web.xml of your portlet project) loads the application-context of the portlet project, but the application-context of the service project is not loaded. That's why the sessionFactory is not available in the portlet.

Easiest way is to move the bean configurations from the application-context of the service project into the application-context of the portlet project.
Stephane Franchamp,修改在9 年前。

RE: Spring IOC in liferay project maven multi module

New Member 帖子: 19 加入日期: 14-8-7 最近的帖子
If i have 10 Portlet for example, should i move the application-context for every portlet ?
the application-context should be implemented only in the service project emoticon
thumbnail
Wouter Vernaillen,修改在9 年前。

RE: Spring IOC in liferay project maven multi module

Junior Member 帖子: 80 加入日期: 09-6-6 最近的帖子
You only have to copy at once in the general application-context of your portlet application.

Maybe you can try to import the service application context by putting something like this in the application context of your portlet application:
<import resource="classpath*:serviceApplicationContext.xml" />

I haven't try this yet and don't know if it works. I saw it mentioned here: http://stackoverflow.com/questions/18513380/spring-use-applicationcontext-from-jar-file-added-as-a-dependency