I tried the following code with Spring 3.x which failed with BeanNotFoundException
and it should according to the answers of a question which I asked before - Can I inject same class using Spring?
@Service
public class UserService implements Service{
@Autowired
private Service self;
}
Since I was trying this with Java 6, I found the following code works fine:
@Service(value = "someService")
public class UserService implements Service{
@Resource(name = "someService")
private Service self;
}
but I don't understand how it resolves the cyclic dependency.
EDIT:
Here's the error message. The OP mentioned it in a comment on one of the answers:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.spring.service.Service] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
@Transactional
annotations to work properly on invocations of another method of the same class from within. Callingthis.myMethod()
would ignore the transaction, butself.myMethod()
should have the transaction created. See Section 5.1. Potential Pitfalls - Transactions and Proxies. – Snackoverflow@Transactional
and the class is@Autowired
as a dependency, then Spring actually injects a Proxy instance which wraps your class instance, and in the proxy implementation the method is also wrapped by a proxy method with transaction logic. If you usethis.myMethod()
directly, you are doing it from within your class instance code, referencing your class instance method directory, and not calling the injected proxy with the transaction logic. spring.io/blog/2012/05/23/… – Snackoverflow