Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring, abstract class and annotations

Tags:

java

spring

I've got a pretty simple abstract class

public abstract class AbstractServiceActions {      @Autowired     protected DatabaseModel dbModel;      protected User user;     protected boolean complete;     protected String serviceResult;      public AbstractServiceActions(User user) {         this.user = user;         this.serviceResult = "";     }      public abstract String doAction();     } 

Now you can see, I'm trying to autowire the DatabaseModel. But in my extended class I only recieve null for the dbModel.

@Component public class CreateDatabaseAction extends AbstractServiceActions { .... } 

Question: Am I trying something impossible here?

like image 378
onigunn Avatar asked May 27 '10 14:05

onigunn


People also ask

Can we use @autowired in abstract class?

We can't use @Autowired on a constructor of an abstract class. Spring doesn't evaluate the @Autowired annotation on a constructor of an abstract class. The subclass should provide the necessary arguments to the super constructor.

Can we create Bean of abstract class?

You don't. You only declare the beans which have a concrete subclass of that abstract class.

Can we use @component for interface?

Annotating an interface with @Component is common for Spring classes, particularly for some Spring stereotype annotations : package org. springframework.

What is use of @service annotation?

In an application, the business logic resides within the service layer so we use the @Service Annotation to indicate that a class belongs to that layer. It is also a specialization of @Component Annotation like the @Repository Annotation.


1 Answers

Your setup seems fine. The reason perhaps lies elsewhere. Maybe you are instantiating the class with new CreateDatabaseAction(), rather than letting spring do this.

like image 117
Bozho Avatar answered Sep 30 '22 11:09

Bozho