Reputation: 33
@Component
public abstract class AbstractProcessTask implements Task {
@Resource
protected WorkOrderEventService workOrderEventService;
@Resource
protected NodeService nodeService;
@Resource
protected ConfigReader configReader;
protected void updateStatus(WorkOrderEvent workOrderEvent, String status, String description) {
workOrderEvent.setStatus(status);
workOrderEvent.setComments(description);
workOrderEventService.saveWorkOrderEvent(workOrderEvent);
}
}
I write a abstract class for use,But I don't know how to use. At Old spring version,We can write abstract="true" in xml. for example:
<bean id="BaseEventAction" class="com.sinosig.evaluation.fcff.web.event.BasicEventAction"
abstract="true" parent="BaseAction">
<property name="rowFactory" ref="FcffCacheAdapter" />
<property name="caculate" ref="CaculateService" />
<property name="diffusion" ref="DeffusionService" />
</bean>
What should I do?
Upvotes: 3
Views: 19153
Reputation: 1176
The abstract class should not be annotated by either @Service or @Component annotation as:
So, for example:
abstract class Vehicle() {
private final Helper helper;
protected(Helper helper) {
this.helper = helper;
}
}
@Component
public class Car extends Vehicle() {
public Car(Helper helper) {
super(helper);
}
}
Upvotes: 1
Reputation: 4071
Using @Component
over an abstract class would not help Spring to create a bean from that (As of course, you know, you can not instantiate an object from an abstract class). Use @Component
annotation over the concrete classes.
@Component
public class MyProcessTask extends AbstractProcessTask {
...
}
And the rest are okay. If spring finds the concrete classes in the scan path, the associated beans will be created automatically.
When you mention the attribute abstract=true
in a bean declaration, you are just abstracting the bean. Abstract beans in Spring are somewhat different from abstract classes. In fact, the abstract bean in Spring doesn't even have to be mapped to any class.
See this nice answer for more about What is meant by abstract=“true” in spring?
Upvotes: 4
Reputation: 506
You can simply extend the abstract class with another class, and use @Component
in the subclass. You may also need to implement any methods in the superclass.
@Component
public class AbstractChild extends AbstractProcessTask {
}
Upvotes: 3