Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8159173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:49:37+00:00 2026-06-06T17:49:37+00:00

I’m trying to realize the same as in Spring error when trying to manage

  • 0

I’m trying to realize the same as in Spring error when trying to manage several classes that share a common base class?

But I’m still getting this Exception:

Error creating bean with name 'com.example.model.CategoryTest': Injection of
autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not autowire
field: private com.example.model.CategoryService
com.example.model.CategoryTest.service; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean
of type [com.example.model.CategoryService] 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)}

Here are my classes in hope someone can help me understanding this autowiring stuff…

public abstract class BaseDAO<E>
{
    public abstract void delete( int id );
    public abstract void save( E entity );
    public abstract List<E> list();
}

public abstract class BaseService<E, D extends BaseDAO<E>>
{
    private final D dao;

    protected BaseService( D dao )
    {
        this.dao = dao;
    }

    @Transactional
    public void delete( int id )
    {
        dao.delete( id );
    }

    @Transactional
    public void save( E entity )
    {
        dao.save( entity );
    }

    @Transactional
    public List<E> list()
    {
        return dao.list();
    }
}

@Repository
public class CategoryDAO extends BaseDAO<Category>
{
    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public void delete( int id )
    {
        Category category = ( Category ) sessionFactory.getCurrentSession().load( Category.class, id );

        if ( category != null )
        {
            sessionFactory.getCurrentSession().delete( category );
        }
    }

    @Override
    public void save( Category category )
    {
        sessionFactory.getCurrentSession().save( category );
    }

    @Override
    public List<Category> list()
    {
        return sessionFactory.getCurrentSession().createQuery( "from Category" ).list();
    }
}

@Service
public class CategoryService extends BaseService<Category, CategoryDAO>
{
    @Autowired
    public CategoryService( CategoryDAO dao )
    {
        super( dao );
    }
}

UPDATE

Servlet context does contain this line: <context:component-scan base-package="com.example" />
Test context (I’m using maven) does contain this line: <context:annotation-config />

Replacing <context:annotation-config /> with <context:component-scan base-package="com.example" /> results in this Exception:

org.springframework.beans.factory.BeanCreationException: Could not autowire field:
private com.example.model.CategoryService
com.example.controller.ExampleController.categoryService; 
nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean
with name 'categoryService' defined in file
[/home/danny/example/target/classes/com/example/model/CategoryService.class]:
Initialization of bean failed; nested exception is
org.springframework.aop.framework.AopConfigException: Could not generate CGLIB
subclass of class [class com.example.model.CategoryService]: Common causes of
this problem include using a final class or a non-visible class; nested exception
is java.lang.IllegalArgumentException: Superclass has no null constructors but no
arguments were given

UPDATE2

I’m still getting this exception, here’s my new code (only changed classes):

public abstract class BaseService<E, D extends BaseDAO<E>>
{
    private D dao;

    /*protected BaseService( D dao )
    {
        this.dao = dao;
    }*/
    protected BaseService(){}

    protected void setDAO( D dao )
    {
        this.dao = dao;
    }

    @Transactional
    public void delete( int id )
    {
        dao.delete( id );
    }

    @Transactional
    public void save( E entity )
    {
        dao.save( entity );
    }

    @Transactional
    public List<E> list()
    {
        return dao.list();
    }
}

@Service
public class CategoryService extends BaseService<Category, CategoryDAO>
{
    @Autowired
    public CategoryService( CategoryDAO dao )
    {
        setDAO( dao );
    }
}

UPDATE3

The solution:

public abstract class BaseService<E, D extends BaseDAO<E>>
{
    protected D dao;

    public BaseService()
    {
    }

    protected D getDao()
    {
        return dao;
    }

    @Autowired
    protected void setDAO( D dao )
    {
        this.dao = dao;
    }

    // ...
}

@Service
public class CategoryService extends BaseService<Category, CategoryDAO>
{
    public CategoryService()
    {
        setDAO( dao );
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T17:49:38+00:00Added an answer on June 6, 2026 at 5:49 pm

    It doesn’t look like an instance of CategoryService is available for Spring to inject in the dependency into the test. You may be missing the component-scan in your services package – <context:component-scan base-package="..">

    Update:
    Based on your update, and this post – Error creating bean with name 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0' defined in ServletContext resource , it looks like you will have to change your BaseService, to have a setter for dao rather than set using a constructor. CGLIB with Spring AOP may not work well with a non default constructor

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.