Abstract classes cannot be instantiated in java. But spring says something like bean creation with abstract="true"
. If a state of an abstract class is initialised only by its child class instance(i guess i am right), then if i need to use that attribute inside the method which is defined in the abstract class then... is there a possibility for it? I have a set of code as follows:
class abstract A {
private Something somethingObj;
// getters and setters are present.
public void logSomething() {
try{
//some code which throws exception
}
catch(Exception e){
somethingObj.logIt(e);// I have some logic inlogIt method.
}
}
}
Abstract beans in Spring are somewhat different from abstract classes. In fact, abstract bean in Spring doesn't even have to be mapped to any class. Take this as an example:
And classes:
Note that
FooDao
andBarDao
do not have any parent (abstract or not) base class in common. Parent abstract bean definition is used only to group common properties, so you avoid repetition in XML.On the other hand introducing abstract
Dao
class that bothFooDao
andBarDao
inherit from would be a good idea:But still
dao
bean doesn't have to define a class. Treat abstract beans as a way to reduce duplication in XML when several concrete beans have same dependencies.Actually, abstract parent bean is not necessary to define class attribute, you may just need a common property for sharing.
According to this tutorial