6

I'm using Spring Boot to write server application.

Most of the time, I write all business logic inside services, where I use @Autowired to access repositories and other services.

However, sometimes I want to access certain service or property from @Entity class, which cannot use @Autowired.

For instance, I have an entity that should be able to serialize itself to JSON. In JSON, it should have imageUrl field, which contains of image name (stored in database and as a property in the @Entity class) and base url, which is only available in application.properties. This means that I have to use @Value annotation inside @Entity class, but it doesn't work that way.

So I create a service which looks like this:

@Service
public class FilesService {

    private static FilesService instance;

    @PostConstruct
    public void init() {
        FilesService.instance = this;
    }

    public static FilesService getInstance() {
        return instance;
    }

    @Value("${files.path}")
    String filesPath;
    @Value("${files.url}")
    String filesUrl;

    public String saveFile(MultipartFile file) throws IOException {
        if (file == null || file.isEmpty()) {
            return null;
        }
        String filename = UUID.randomUUID().toString();
        file.transferTo(new File(filesPath + filename));
        return filename;
    }

    public String getFileUrl(String filename) {
        if (filename == null || filename.length() == 0) {
            return null;
        }
        return filesUrl + filename;
    }

}

And then inside the @Entity class I write the following code:

@JsonProperty
public String getImageUrl() {
    return FilesService.getInstance().getFileUrl(imageName);
}

This works, but it doesn't look right. Moreover, I concern whether this can lead to some side effects if used with less trivial @Service classes or @Repository classes.

What is the correct way to use @Repository and @Service classes from @Entity classes or any other non-@Component classes (classes not managed by Spring)?

2
  • Why don't you store the url along in that entity too? When the client/controller creates a new entity the file path can be passed to your entity. I know there's no set rule' but it 'feels' wrong to have that hidden dependency in your entity.
    – H.Rabiee
    Commented Jul 26, 2016 at 15:25
  • 1
    Well, ntot service. Write custom marshaller Commented Jul 27, 2016 at 6:47

1 Answer 1

3

Well, I'd say there's no correct way of using repositories and services from entities since every fiber of my being screams of wrongness but that being said , you can refer to this link for suggestions on how to do it.

In your case, I think it should allow you to populate @Value fields within your entities, which would actually be preferable to autowiring the service.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.