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

  • Home
  • SEARCH
  • 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 6581861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:12:27+00:00 2026-05-25T16:12:27+00:00

I have a question regarding Spring inheritance: Let’s say I have a class (bean)

  • 0

I have a question regarding Spring inheritance:
Let’s say I have a class (bean) BraveKnight that implements the interface Knight. After I declare this bean in the xml file with the id->knight, I’m trying to get a reference of the bean:

Knight knight = (Knight) context.getBean("knight"); 

This is the way it works. But why isn’t possible to get the bean reference from the bean’s name itself: BraveKnight (and why doesn’t work with no interface at all)?

BraveKnight knight = (BraveKnight) context.getBean("knight"); 
  • 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-05-25T16:12:27+00:00Added an answer on May 25, 2026 at 4:12 pm

    First of all please use:

    public <T> T getBean(String name, Class<T> requiredType)
    

    instead of:

    public Object getBean(String name)
    

    You will avoid this nasty class cast.

    Secondly your question about why it is not working with no interface is a little bit unobvious. I guess you have some kind of problem getting BraveKnight when it is not implementing any interface at all. If you want do that you have to force Spring to use CGLIB instead of dynamic proxy mechanism. If you have a problem with ClassCastException though when casting your bean to BraveKnight it means that Spring used dynamic proxy and implemented a $Proxy0 class which is only implementing Knigh interface and does not extend BraveKnigh directly. Whatever your problem is please try to get to know CGLIB and JDK dynamic proxy mechanism better so that you will not be surprised next time what Spring will do in the future with your objects.

    If that answer does not correspond to your problem please try to attach some more info, stack trace or explanation.

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

Sidebar

Related Questions

I have a question regarding dependency injection. say i want to create a class
I have a question regarding static function in php. let's assume that I have
I have a question regarding string modification. Let's assume that we have the following
I have a question regarding inheritance in Java. If I have this base class
I have question regarding the SQLAlchemy. How can I add into my mapped class
Greetings all... I have a simple, yet annoying question regarding Spring Web MVC. I
I have a question regarding interfaces. I understood that you cannot create instances of
I'm just learning about generics and have a question regarding method return values. Say,
I have a question regarding inheritance, so I will describe the scenario below: I
I have a question regarding the append method in the StringBuilder class. I was

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.