Abstract classes cannot be instantiated

Source: Internet
Author: User

1. Abstract classes cannot be instantiated

Recently in the construction of the SSH framework, I sealed layer of the DAO "Basedao.java" layer, there is a method can no longer basedao concrete implementation, in order to prevent the next person, inherit the basic class of people, do not ignore overloading the class, I will write it as an abstract class, so, The Basedao class must also be an abstract class

@Repositorypublic  abstract class basedao<t, PK extends serializable> extends Hibernatedao {/**     *  Get the combination of entity conditions     * @return *  /public abstract    criterion[] Criterionsarray (T entity);

  

However, I have written a basic service class "Baseservice", in order to reduce the application layer, repeat the application layer write add and delete to change the function, I simply in baseserice to implement it:

@Service @transactionalpublic abstract class baseservice<t, PK extends serializable> {    protected Logger Logger = Loggerfactory.getlogger (This.getclass ());    @Autowired    Private basedao<t, serializable> Basedao;    public void Create (T entity) {        basedao.create (entity);    }    public void Delete (T entity) {        basedao.delete (entity);    }    public void update (T entity) {        basedao.update (entity);    }    Public list<t> List (T entity) {        assert.assertnotnull (entity);        return Basedao.list (entity);    }    Public T get (String ID) {       return (T) basedao.get (ID);    }    ...}

Here's the problem, I'm in the JUnit test framework, the application layer "Userserivice extends Baseservice": test results

Nested exception is Org.springframework.beans.factory.NoSuchBeanDefinitionException:No qualifying bean of type [ Com.cqs.common.base.dao.BaseDao] found for dependency:expected at least 1 bean which qualifies as Autowire candidate For this dependency. Dependency annotations: {@org. springframework.beans.factory.annotation.Autowired (Required=true)}

Automatic assembly error. Online said because each layer did not write annotations (that is, the Action layer Add @action,service layer add @service, @Transactional and the DAO layer to add @repository), I have checked all the classes according to this method, found all wrote " PS encountered before, there is a service layer of the class did not write @service annotations, indeed thrown the above exception ".

In order to narrow down the visit, I have written a Basedao test in JUnit, just do one thing, auto assemble Basedao

public class Hibernatedaotest extends basetestconfig{    @Autowired    private Basedao Basedao;    @Test public    void Testgetcurrentsession () {        System.out.println ("= =");}    }

  

@RunWith (Springjunit4classrunner.class) @ContextConfiguration ({"Classpath:applicationContext.xml"}) @ Transactionalpublic class Basetestconfig extends Abstractjunit4springcontexttests {    protected static Logger Logger = Loggerfactory.getlogger (Basetestconfig.class);}

To run the result, throw an exception:

Caused By:org.springframework.beans.factory. Nouniquebeandefinitionexception:no Qualifying Bean of type [Com.cqs.common.base.dao.BaseDao] is defined:expected Single matching beans but found 2:imageuploaddao,userdao

Well, this shows that two classes of inherited Basedao are instantiated, and Basedao cannot be instantiated. So the reason is that in baseservice instantiation of the Basedao, but Basedao is abstract class, and abstract class is not instantiated!

The following is the modification of the Basedao into the ordinary class on the line

@Repositorypublic  class basedao<t, PK extends serializable> extends Hibernatedao {/**     *  get the combined condition of the entity     * @return *  /public criterion[] Criterionsarray (T entity) {      return null;        }; ....}

However, the problem is that the structure is not as good as the previous declaration of the abstract class [inheriting the Basedao class, without prompting, overloading the Criterionsarray method], but, in the latter way, you can avoid writing a lot of repetitive code.

Abstract classes cannot be instantiated

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.