When trying to override SimpleJpaRepository
, adding other beans via @Autowired
does not work. How can beans be injected in this case? Here is an implementation:
public class BaseDAO<T, ID extends Serializable>
extends SimpleJpaRepository<T, ID>
implements IDAO<T, ID> {
@Autowired
private SomeBean someBean; // NULL!
}
Instances of
BaseDAO
are not Spring-managed beans in themselves and therefore autowiring through@Autowired
will not work out-of-the-box. Dependencies therefore need to be injected intoBaseDAO
instances.This will be required to autowire the dependencies for the custom repository implementation at the time of repository creation.
In order to let Spring know that it needs to inject something inside your DAO you need to annotate it with @Component.
You can read more about this here: http://docs.spring.io/autorepo/docs/spring-boot/current/reference/html/using-boot-spring-beans-and-dependency-injection.html.