Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is meant by abstract="true" in spring?

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.          }      } } 
like image 944
Arun Avatar asked Feb 22 '12 15:02

Arun


People also ask

What is spring abstract bean?

The parent bean cannot be instantiated on its own because it is incomplete, and it is also explicitly marked as abstract. When a definition is abstract like this, it is usable only as a pure template bean definition that serves as a parent definition for child definitions.

Can we create Bean of abstract class in spring?

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

Can we inject abstract class in spring?

Third, as Spring doesn't support constructor injection in an abstract class, we should generally let the concrete subclasses provide the constructor arguments. This means that we need to rely on constructor injection in concrete subclasses.

What is the purpose of abstract class?

An abstract class cannot be instantiated. The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share.


1 Answers

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:

<bean id="dao" abstract="true">     <property name="dataSource" ref="dataSource"/>     <property name="someHelper" ref="someHelper"/> </bean>  <bean id="fooDao" class="FooDao" parent="dao">     <property name="fooHelper" ref="fooHelper"/> </bean> <bean id="barDao" class="BarDao" parent="dao">     <property name="barHelper" ref="barHelper"/> </bean> 

And classes:

public class FooDao {     private DataSource dataSource;     private SomeHelper someHelper;     private FooHelper fooHelper;      //setters }  public class BarDao {     private DataSource dataSource;     private SomeHelper someHelper;     private BarHelper barHelper;      //setters } 

Note that FooDao and BarDao 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 both FooDao and BarDao inherit from would be a good idea:

public abstract Dao {     protected DataSource dataSource;     protected SomeHelper someHelper;      //setters }  public class FooDao extends Dao {     private FooHelper fooHelper;      //setters }  public class BarDao extends Dao {     private BarHelper barHelper;      //setters } 

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.

like image 86
Tomasz Nurkiewicz Avatar answered Sep 20 '22 07:09

Tomasz Nurkiewicz