Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
RE: Best way to make an util service
Hi there!I'm starting to work in a mail service for my software. This need to be used in various modules.
Which is the best aproach to build this?I use IntelliJ, and I don't see the 'DS service' recommended here (https://liferay.dev/forums/-/message_boards/message/77592272).Thanks in advance!
Which is the best aproach to build this?I use IntelliJ, and I don't see the 'DS service' recommended here (https://liferay.dev/forums/-/message_boards/message/77592272).Thanks in advance!
Juan Pereyra:
I use IntelliJ, and I don't see the 'DS service' recommended here.
There's almost no magic in DS: You'll have a standard Java interface, and implement it in a class annotated with @Component. Then you can use it in other classes through @Reference.
I'd recommend to go through OSGi Basics, or another OSGi tutorial. Once you get the basics in, the amount of OSGi that you'll get in touch with day by day is surprisingly low and easy to understand.
A second way, if you just need want to reuse code, is to write normal classes and export them in the bnd file. We use that often for data transfer objects, since we need them in various modules.
You can, of course, add all kinds of methods to such classes, to create a simple library.To make them available to other modules you need to export them in the bnd.bnd file:
Note: I recommend to create an extra package for dto classes. Don't add it to an existing package since it will also export stuff by default. And you will overwrite the automatically generated export directive with yours, which could cause problems. It can be fixed, but it requires some understanding what happens.
Note 2: If you use @Component/@Reference the bnd stuff is created automatically for you.
// Note: Uses Lombok to create getters, setters
@NoArgsConstructor
@AllArgsConstructor
public class CheckDownloadDTO {
@Getter @Setter
private String downloadURL;
@Getter @Setter
private boolean downloadPossible;
...
}
You can, of course, add all kinds of methods to such classes, to create a simple library.To make them available to other modules you need to export them in the bnd.bnd file:
Export-Package: my.package.with.dto, my.otherpackage.with.something
Note: I recommend to create an extra package for dto classes. Don't add it to an existing package since it will also export stuff by default. And you will overwrite the automatically generated export directive with yours, which could cause problems. It can be fixed, but it requires some understanding what happens.
Note 2: If you use @Component/@Reference the bnd stuff is created automatically for you.
Hi guy!.
Thanks you both!
I'm going to check that course.
The 'Simply library method' looks good! I think I'm going to give it a chance!
Thanks again!
Thanks you both!
I'm going to check that course.
The 'Simply library method' looks good! I think I'm going to give it a chance!
Thanks again!
Copyright © 2025 Liferay, Inc
• Privacy Policy
Powered by Liferay™