Skip to content

Instantly share code, notes, and snippets.

@martinlau
Created November 30, 2012 00:24
Show Gist options
  • Save martinlau/4172872 to your computer and use it in GitHub Desktop.
Save martinlau/4172872 to your computer and use it in GitHub Desktop.
A simple service using spring's @transactional and dependency injection.
package au.com.permeance.service;
public interface FakeService {
void doStuff() throws Exception;
}
package au.com.permeance.service.impl;
import au.com.permeance.service.FakeService;
import com.liferay.portal.service.UserLocalService;
import com.liferay.portal.util.Portal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class FakeServiceImpl implements FakeService {
private final Portal portal;
private final UserLocalService userLocalService;
@Autowired
public FakeServiceImpl(Portal portal,
UserLocalService userLocalService) {
this.portal = portal;
this.userLocalService = userLocalService;
}
@Override
@Transactional(rollbackFor = Exception.class)
public void doStuff() throws Exception {
userLocalService.getDefaultUser(portal.getDefaultCompanyId());
throw new RuntimeException("Don't really delete that user");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment