Sunday, January 26, 2020

Connect Service Builder to External Database in Liferay DXP


Today we will discuss how to connect service builder to external database using spring beans. We already know how to write Service Builder in Liferay DXP 7.2. So lets start this step by step :-



Step 1:- Create Service Builder
Here i create a small entity employee that contain only two column:-

service.xml


Here two things are important:-
1)dpenedency-injector="spring"  ie we are using spring and not ds
2)data-source="secondDatabase" ie we create another datasource.

When you build the service you see some errors for spring dependency.

For this add the dependency in build.gradle 

 compileOnly group: "com.liferay", name: "com.liferay.portal.spring.extender.api", version: "4.0.0"

 And refresh gradle project.

Here a spring folder is automatically created inside META-INF






Step 2:- Specify the datasource
Now open your portal-ext.properties and enter the database properties:-

portal-ext.properties


Explanation
Here we add our database properties .The .ext is our new database which we are using for creating connection.


Step 3:- Create Spring Bean
Create a ext-spring.xml file inside the sprint folder and paste this content:-

ext-spring.xml


Explanation:-
1)Here we use DataSourceFactoryBean where value is jdbc.ext. which we already create in out portal-ext.properties(Step 2).
2)alias="secondDatabase" this secondDatabase we already mention in our service.xml

RUN SERVICE BUILDER

Also restart server because we mention new database properties in portal-ext.properties.

Now you can use another database tables .

Final Project Structure





Related Post:-

No comments:

Post a Comment

Total Pageviews

Number Of Unique Visitor

Free counters!