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 8678537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:44:47+00:00 2026-06-12T20:44:47+00:00

I have a code: @Configuration public class BeanSample { @Bean(destroyMethod = stop) public SomeBean

  • 0

I have a code:

@Configuration
public class BeanSample {

    @Bean(destroyMethod = "stop")
    public SomeBean someBean() throws Exception {
        return new SomeBean("somebean name1");
    }


    class SomeBean {

        String name;

        public SomeBean(String name) {
            this.name = name;
        }

        public void stop() {
            System.out.println("stop");
        }
    }

    public static void main(String[] args) throws Exception {

        BeanSample beanSample = new BeanSample();
        SomeBean someBean1 = beanSample.someBean();

        ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
                new String[] {"appContext.xml"});

        SomeBean someBean2 = (SomeBean) appContext.getBean("someBean");

        if (someBean1 == someBean2) System.out.println("OK");

    }
}

I’m expecting, once I start app, the BeanSample.getSomeBean() then SomeBean is started to be available by ‘someBean’.

Bu now I have an error: No bean named ‘someBean’ is defined

Actually, I dot not understand which app-context I should use to pick my beans up?

About @Configuration:

Any reasons, why I should use @Configuration annotation here? (with this one, my IDE highlights my classes as it were Spring-related then, so it should make sense )

— OK: after I got an answer my code looks like this:

 public static void main(String[] args) throws Exception {

        AnnotationConfigApplicationContext appContext = new AnnotationConfigApplicationContext(BeanSample.class);

        SomeBean someBean2 = (SomeBean) appContext.getBean("someBean");

        if (someBean2 != null) System.out.println("OK");

    }
  • 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-12T20:44:49+00:00Added an answer on June 12, 2026 at 8:44 pm

    First, if you use the Java config, you have to instantiate your context like this:

    new AnnotationConfigApplicationContext(BeanSample.class)
    

    Second, the @Configuration annotation doesn’t make a bean out of the class that is annotated. Only the @Bean methods are used to create beans.

    If you want to have a BeanSample bean too, you have to create another @Bean method that creates one. But then again, why would you want that? I think the a @Configuration class should only be used as the configuration container and not for anything else.

    Third, the default bean names for @Bean do not follow the conventions of property getters. The method names are used directly, meaning in your example, the bean would be named getSomeBean and not someBean. Change the method to this:

    @Bean(destroyMethod = "stop")
    public SomeBean someBean() throws Exception {
        return new SomeBean("somebean name1");
    }
    

    Finally, the @Configuration class should not be instantiated. Its methods only serve the purpose of creating the beans. Spring will then handle their lifecycle, inject properties and so on. In contrast, if you instantiate the class and call the methods directly, the returned objects will be just normal objects that don’t have anything to do with Spring.

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

Sidebar

Related Questions

I have this code: public class Configuration{ public Control container; public Configuration() { container
I have the following code block: - using System.ComponentModel.DataAnnotations; public class NewsItem { [RegularExpression(System.Configuration.ConfigurationSettings.AppSettings[UrlRegEx],
I have the following code public class Something { [Inject] public Configuration config {get;set;}
I have the following code: using System.Configuration; namespace test { public partial class MyService
So I have this code inside my lib/ folder: class GlobalConfig::SetHelper def self.yes_no_input(configuration) value
I have the following Powershell code: Add-Type -Assembly System.Configuration [ExeConfigurationFileMap] $configMap = New-Object ExeConfigurationFileMap
I have the following code var section = new CustomConfigurationSection(); section.SectionInformation.Type = System.Configuration.NameValueFileSectionHandler; section.SectionInformation.SetRawXml(sectionXml);
I have the following code: modelBuilder.Entity<User>().HasMany(x => x.Items).WithRequired(); The model looks like public class
I have the following code: public abstract class BaseController : Controller { public IUserService
I have code like using FluentValidation; public class FreeformValidator : AbstractValidator<Freeform> { public FreeformValidator()

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.