Not injected bean in class that extends abstract class in Spring Boot - java

I have trouble with initializing bean and injecting JPA repository into one particular bean. No idea why it doesn't work...
There is a interface defining key service:
public interface KeyService {
Store getKeyStore();
Store getTrustStore();
}
and abstract class that implements this interface:
public abstract class DefaultKeyService implements KeyService {
abstract KeyRecord loadKeyStore();
abstract KeyRecord loadTrustStore();
/* rest omitted... */
}
and base class that extends abstract class:
#Service
public class DatabaseKeyService extends DefaultKeyService {
#Autowired
private KeyRecordRepository keyRecordRepository;
#Override
protected KeyRecord loadKeyStore() {
return extract(keyRecordRepository.findKeyStore());
}
#Override
protected KeyRecord loadTrustStore() {
return extract(keyRecordRepository.findTrustStore());
}
/* rest omitted... */
}
And bean initialization:
#Bean
public KeyService keyService() {
return new DatabaseKeyService();
}
This is a KeyRecordRepository repository:
public interface KeyRecordRepository extends Repository<KeyRecord, Long> {
KeyRecord save(KeyRecord keyRecord);
#Query("SELECT t FROM KeyRecord t WHERE key_type = 'KEY_STORE' AND is_active = TRUE")
Iterable<KeyRecord> findKeyStore();
#Query("SELECT t FROM KeyRecord t WHERE key_type = 'TRUST_STORE' AND is_active = TRUE")
Iterable<KeyRecord> findTrustStore();
KeyRecord findById(long id);
}
Question: is there some reason why keyRecordRepository in DatabaseKeyService class is still null? Really I have no idea why only this this field is not injected. Other beans and repositories works perfectly fine.
Couldn't be a problem because parent class is an abstract class?

DatabaseKeyService must be annotated with #Component to be a Spring managed bean.

Your problem is related with having 2 beans for class DatabaseKeyService. One from configuration class - #Bean annotation and second from #Service annotation.
Probably when you remove
#Bean
public KeyService keyService() {
return new DatabaseKeyService();
}
injecting with #Service will be work.
If you want use #Bean you must add KeyRecordRepository. I prefer using constructor injection so firstly create it in DatabaseKeyService
public DatabaseKeyService(KeyRecordRepository keyRecordRepository) {
this.keyRecordRepository = keyRecordRepository;
}
Then in your configuration file
//other
#Autowired
private KeyRecordRepository keyRecordRepository;
#Bean
public KeyService keyService() {
return new DatabaseKeyService(keyRecordRepository);
}

Related

Is there any way to know in Spring what bean to use while being from the parent class?

I have this specific problem in which I can't use a #Qualifier cause I need the bean in the parent class. My idea is to remove the baseComponent propertie and make an abstract method in BaseController like getComponent() and return the desired bean for BaseComponent ... but perhaps there is a cleaner way to do this through configuration.
#RestController
public abstract class BaseController {
#Autowired
private BaseComponent baseComponent;
#GetMapping("/something")
public void doSomething() {
baseComponent.printSomething();
}
}
#RestController
#RequestMapping(value = "/foo")
public class FooController extends BaseController {
}
#RestController
#RequestMapping(value = "/bar")
public class BarController extends BaseController {
}
public interface BaseComponent {
void printSomething();
}
#Component
public class FooComponent implements BaseComponent {
#Override
public void printSomething() {
System.out.println("foo!");
}
}
#Component
public class BarComponent implements BaseComponent{
#Override
public void printSomething() {
System.out.println("bar!");
}
}
This is one of the reasons I don't like autowiring directly to a private field. I would do this by injecting BaseComponent through the constructor of BaseController:
public abstract class BaseController {
private final BaseComponent baseComponent;
protected BaseController(BaseComponent baseComponent){
this.baseComponent = baseComponent;
}
#GetMapping("/something")
public ResponseEntity<String> getSomething(){
return new ResponseEntity<String>(baseComponent.getSomething(), HttpStatus.OK);
}
}
#RestController
#RequestMapping("/foo")
public class FooController extends BaseController{
#Autowired
public FooController(#Qualifier("fooComponent") BaseComponent baseComponent) {
super(baseComponent);
}
}
#RestController
#RequestMapping("/bar")
public class BarController extends BaseController{
#Autowired
public BarController(#Qualifier("barComponent") BaseComponent baseComponent){
super(baseComponent);
}
}
#Component
public class BarComponent implements BaseComponent {
#Override
public String getSomething() {
return "bar";
}
}
#Component
public class FooComponent implements BaseComponent {
#Override
public String getSomething() {
return "foo";
}
}
Requests to /something/bar will return bar and requests to something/foo will return foo.
Note that the abstract BaseComponent is not actually declared as any kind of Spring component nor does it have any dependencies automatically injected. Instead, the subclasses are the components and the dependencies are wired into their constructors and passed through super to BaseComponent. The subclass constructors provide a place for the #Qualifier annotation to specify which BaseComponent you want.
In theory, I don't like declaring two classes that are identical other than the annotations. In practice, though, I have found that sometimes it is simplest just to declare classes to hold the Spring annotations. That's better than the old days of XML configuration.
You can do it in the following way
import org.springframework.beans.factory.BeanFactory;
#RestController
public abstract class BaseController {
#Autowired
private BeanFactory beanFactory;
#GetMapping("/something")
public void doSomething() {
BaseComponent baseComponent = beanFactory.getBean("Foo", // or "Bar"
BaseComponent.class);
baseComponent.printSomething();
}
}
#Component("Foo")
public class FooComponent implements BaseComponent {
...
}
#Component("Bar")
public class BarComponent implements BaseComponent{
...
}
If I right understand what you want.
You can use generics if you don't want to hardcode the bean name:
#RestController
public abstract class BaseController<T extends BaseComponent> {
#Autowired
private T baseComponent;
...
}
#RestController
public class FooController extends BaseController<FooComponent> {
...
}

How provide a different bean implementation to #Autowire field based on annotation?

I have a config class that provides two implemenations of the same base bean interface. I want these to be set on the autowired fields conditionally based on an annotation on the field.
public class MyController
{
#Autowired
private MyBeanInterface base;
#Autowired
#MyAnnotation
private MyBeanInterface special;
}
This is a pesudo-code of the config class:
#Configuration
public class ConfigClass
{
#Bean
#Primary
public MyBeanInterface getNormalBeanInterface()
{
return new MyBeanInterfaceImpl();
}
#Bean
//This doesn't work
#ConditionalOnClass(MyAnnotation.class)
public MyBeanInterface getSpecialBeanInterface()
{
return new MyBeanInterfaceForMyAnnotation();
}
}
How can I make the annotated field be populated by the second bean?
Use Qualifier annotation. Example:
Controller:
Add Qualifier annotation at the injected fields with bean id as parameter:
public class MyController
{
#Autowired
#Qualifier("normalBean")
private MyBeanInterface base;
#Autowired
#Qualifier("specialBean")
private MyBeanInterface special;
}
ConfigClass
Specify bean id:
#Configuration
public class ConfigClass
{
#Bean(name="normalBean")
#Primary
public MyBeanInterface getNormalBeanInterface()
{
return new MyBeanInterfaceImpl();
}
#Bean(name="specialBean")
public MyBeanInterface getSpecialBeanInterface()
{
return new MyBeanInterfaceForMyAnnotation();
}
}

Generifying Service layer classes

I'm trying to follow code reusing best practices.
I have generic DAO interface with some common methods:
public interface DaoInterface<T> {
T findById(int id);
//...more methods...
}
and its implementation class:
public class GenericDao<T> implements DaoInterface<T> {
#SuppressWarnings("unchecked")
private final Class<T> persistentClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
#Autowired
protected SessionFactory sessionFactory;
#Override
#SuppressWarnings("unchecked")
public T findById(int id) {
Session session = sessionFactory.getCurrentSession();
return (T) session.get(persistentClass, id);
}
//...more methods...
}
Then my every concrete implementation class extends GenericDao and implements its interface.
I also have Service layer in my application. Some Services' methods completely delegate their work to DAO classes. So in the each concrete Service implementation I autowire appropriate DAO class and call its methods.
So now it looks:
public interface CustomerService {
Customer findById(int id);
}
and implementation:
#Service
#Transactional(readOnly = true, rollbackFor = Exception.class)
public class CustomerServiceImpl implements CustomerService {
#Autowired
private CustomerDao customerDao;
#Override
public Customer findById(int id) {
return customerDao.findById(id);
}
}
My question is - how to generify Service classes in the same way as DAO? So that my concrete class will look:
public class CustomerServiceImpl extends GenericService<Customer> implements CustomerService {
.....
}
The problem is that I cannot autowire DAO class in Generic Service:
#Autowired
private GenericDao<T> dao;
so that I could call dao's methods.
Should I do it in the constructor?
And one more question - where is the right place to annotate methods with #Transactional - in generic class or in every implementation class?
You have to create an instance of a generic Dao and put in the service layer some decision:
#Repository
public class GenericDao implements DaoInterface<T> {
//The type must be aquired at runtime,otherwise it may not be thread safe
#Autowired
protected SessionFactory sessionFactory;
#Override
#SuppressWarnings("unchecked")
public T findById(int id,Class<?> persistenceClass) {
Session session = sessionFactory.getCurrentSession();
return (T) session.get(persistenceClass, id);
}
//...more methods...
}
Also if you need a good generic repository layer take a look for Spring Data Jpa
This will make one and only one instance of the GenericDao.
Next you have 2 choice:
Create a singleton services for all your needs
Create a class service for every entity
abstract class GenericService<T> {
#Autowired
protected GenericDao dao;
#SuppressWarnings("unchecked")
protected final Class<T> persistentClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
#Override
public T findById(int id) {
return dao.findById(id,persistenceClass);
}
}
Now every one of your service must extends the GenericService with a supplied persistence type and the job is done.

Method in interface implementing class not visible in service class

I have a method defined in an implementing #Repository class that is an extension of an abstract base class and implements an interface. However, a method defined in the #Repository class is not visible in the #Service class, and I am not sure why.
I have a #Service class defined as
#Service
#Transactional
public class CategoryService {
#Autowired
private IJpaRepository categoryRepository;
public CategoryService(){ }
/* service methods */
public List<Category> findTopLevelCategories(){
//findTopLevelCategories is not visible here
return categoryRepository.findTopLevelCategories();
}
}
where IJpaRepository is an interface defined by
public interface IJpaRepository<T> {
T findOne(int id);
List<T> findAll();
T create(T entity);
T update(T entity);
void delete(T entity);
void deleteById(int id);
}
and the #Repository is defined as an extension of an abstract class implementing the interface above:
#Repository
public class CategoryRepository extends AbstractJpaRepository<Category> implements IJpaRepository<Category> {
public List<Category> findTopLevelCategories(){
Query queryCategoryTopLevel = getEntityManager().createNamedQuery("findTopLevelCategories");
return queryCategoryTopLevel.getResultList();
}
/* Other overriding methods */
}
My best guess is that it is due to the #Autowired binding to the IJpaRepository, rather than the actual implementing CategoryRepository class. Without making any changes to IJpaRepository interface, how can I make the new method defined in CategoryRepository visible to the #Service class?

Dependency not found when trying to inject a generic class

I have developed a GenericDAO interface with two generic types, the entity and the type of the primary key:
public interface GenericDAO<E, PK extends Serializable> {
PK save(E newInstance);
void update(E transientObject);
//typical dao methods
}
Then I have provided an implementation for them in hibernate 4:
#Transactional
#Component
#Repository
public abstract class GenericDAOHibernate4<E, PK extends Serializable> implements GenericDAO<E, PK> {
public PK save(E newInstance) {
return (PK) factory.getCurrentSession().save(newInstance);
}
public E findById(PK id) {
return (E) factory.getCurrentSession().get(getEntityClass(), id);
}
//method implementations
}
Then I just have to create concrete classes extending this abstract class:
#Component
#Transactional
#Repository
#Qualifier("userDAO")
public class UserDAO extends GenericDAOHibernate4<User, Long> {
#Autowired
public UserDAO(SessionFactory factory) {
super(factory);
}
#Override
protected Class<User> getEntityClass() {
return User.class;
}
}
Then I inject the concrete DAOs when needed this way:
public class UserService extends GenericService<User> {
#Autowired
public UserService(#Qualifier("userDAO") GenericDAO<User, Long> dao) {
super(dao);
}
But, if I need to add another method to the concrete dao, and therefore inject the concrete class, spring cannot find the dependency. This fails at startup:
public class UserService extends GenericService<User> {
#Autowired
public UserService(#Qualifier("userDAO") UserDAO dao) {
super(dao);
}
With this error:
Could not instantiate bean class [ddol.rtdb.services.UserService]: No default constructor found; nested exception is java.lang.NoSuchMethodException: ddol.rtdb.services.UserService.()
How should I inject it?
If a class implements an interface, a bean of that class can only be autowired using the interface type and not the concrete class type. Since UserDao implements the GenericDAO<User, Long> interface, it is being autowired correctly when you autowire using the interface. Spring is not able to find the dependency when you try to autowire using the concrete class, then it looks for a no-arg constructor for UserService and fails giving the particular error on not finding any.
In general, it not a good idea to inject dependencies using concrete classes because it tightly couples your modules. Right way to do this is to have an interface for each of your DAO classes and have their implementation extending the GenericDAOHibernate4.
public interface GenericDAO<E, PK extends Serializable> {
PK save(E newInstance);
void update(E transientObject);
//typical dao methods
}
public interface UserDAO extends GenericDAO<User, Long> {
List<User> findUsersByFirstname(String firstName);
}
#Component
#Transactional
#Repository
#Qualifier("userDAO")
public class UserDAOImpl extends GenericDAOHibernate4<User, Long>
implements UserDAO {
#Autowired
public UserDAO(SessionFactory factory) {
super(factory);
}
#Override
protected Class<User> getEntityClass() {
return User.class;
}
List<User> findUsersByFirstname(String firstName) {
//provide implementation here
}
}
public class UserService extends GenericService<User> {
#Autowired
public UserService(#Qualifier("userDAO") UserDAO dao) {
super(dao);
}
}
#Autowired = wire by Type
#Resource = wire by bean name

Categories