dawdler-local-service-core
Module introduction
The LocalService core module, used to mark an injected service as a local service.
1. Add the dependency to the pom
<groupId>club.dawdler</groupId>
<artifactId>dawdler-local-service-core</artifactId>
2. About @LocalService
@LocalService marks an injected service as a local service.
@Retention(value = RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD })
public @interface LocalService {
//The class name of the service; defaults to empty, in which case the fully qualified name of the class or interface where the annotation is placed is used (class first). Corresponds to serviceName in @Service
String serviceName() default "";
}
3. Usage example
3.1 Create a Controller
@Controller
@RequestMapping("/order")
public class OrderController {
@LocalService
private OrderService orderService;
@ResponseBody
@RequestMapping(value = "/list", method = RequestMethod.GET)
public PageResult<List<Order>> list(Integer pageOn, Order order) {
int row = 10;
return orderService.selectPageList(order, pageOn, row);
}
}
3.2 Create the service interface
@Service("order-service")
public interface OrderService {
BaseResult<Order> selectByPrimaryKey(Integer orderId);
}
3.3 Write the service implementation layer (using MyBatis as an example)
public class OrderServiceImpl implements OrderService {
@Repository
private OrderMapper orderMapper;
@Override
@DBTransaction(mode = MODE.readOnly)
public BaseResult<Order> selectByPrimaryKey(Integer orderId) {
Order order = orderMapper.selectByPrimaryKey(orderId);
BaseResult<Order> baseResult = new BaseResult<>(order);
return baseResult;
}
}
4. Circular reference restrictions
A non-single service (@Service(single = false)) creates a new instance, and re-injects its @LocalService fields, on every getService call;
therefore circular references between non-single services (including a service referencing itself directly) cannot be resolved. The container throws a DawdlerOperateException at creation time,
and the exception message contains the full reference chain, for example AService -> BService -> AService, instead of ending in a stack overflow.
To have services reference each other, change any one service in the cycle to single = true. Circular references between singleton services are guaranteed by the startup-time
ordering of "instantiate all services first, then inject fields", and work normally.