-2
@Service
public class UserService implements Service{
   @Autowired
   private Service self;
}

Does the code above works fine in Spring new versions (5.*)? ( I could check by myself but I wanna know 100%, but myself I may screw it up somehow ) Also I know workarounds:

@Service(value = "someService")
public class UserService implements Service{
   @Resource(name = "someService")
   private Service self;
}

OR

 @Autowired
private ApplicationContext applicationContext;

So I'm not just asking for nothing, I need to know 100% I need an advice from professionals, I don't believe to myself experiments, because I'm not so much experienced in Spring ( e.g. there are much nebulous configs there for me) . Hope this clarifies why I'm asking rather than experiment.

J.J. Beam
  • 2,612
  • 2
  • 26
  • 55
  • 1
    Seems like trying it would be faster and more authoritative than asking here. – duffymo Nov 01 '19 at 10:53
  • yes, but trying is not enough, I wanna be 100% sure as written above. Technically a successful experiment is not a 100% proof – J.J. Beam Nov 01 '19 at 12:07
  • It's a logic: unsuccessful experiment proves, but successful doesn't. Suppose you successfully one ( or even multiple) time throw a coin and won a dime. It doesn't mean every time will be such result. – J.J. Beam Nov 01 '19 at 13:43
  • Why is this a desirable feature? Seems like an incredibly long-winded way of writing `this` – Michael Nov 01 '19 at 14:28
  • In Spring Framework "this" will not work, due to proxying mechanism – J.J. Beam Nov 01 '19 at 14:30
  • This would be useful if you have aspects of particular method and want to call a method from this class – J.J. Beam Nov 01 '19 at 14:31
  • For example call transactional method of the same class from another transactional method – J.J. Beam Nov 01 '19 at 14:41

1 Answers1

0

Ok I found the answer: With Spring 4 it's possible to Self autowired

@Service
@Transactional
public class UserServiceImpl implements UserService{
    @Autowired
    private  UserRepository repository;

    @Autowired
    private UserService userService;

    @Override
    public void update(int id){
       repository.findOne(id).setName("ddd");
    }

    @Override
    public void save(Users user) {
        repository.save(user);
        userService.update(1);
    }
}
J.J. Beam
  • 2,612
  • 2
  • 26
  • 55