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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:56:02+00:00 2026-06-10T22:56:02+00:00

I need to be able to add new beans to a spring web context

  • 0

I need to be able to add new beans to a spring web context (in any scope) for classes that might and might not be defined in the classpath at compilation time.

For example, I could create some of this classes dynamically and then register a singleton or session bean for this class.

I read a little about the BeanFactoryPostProcessor, but not sure if it will work on a web context, and if I understand correctly it will only work before actually loading the bean instances and not after that, or am I wrong?

I haven’t been able to find information on how to do this in a AnnotationConfigWebApplicationContext, and at least in my test all the beans I create dinamically are not injected into other instances, and even if they were I only see the method to register Singleton beans but not for other scopes:

ctx.getBeanFactory().registerSingleton("dummy", dummy);

P.D.

I found this question : Dynamic creation of beans in Spring but when I try to add any bean, bean definition or bean definition builder to the web context like this the bean does not get loaded:

AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.setServletContext(servletContext);
ctx.refresh();
//
log.trace("Registering first test bean");
Test test = new Test();
test.setDummy("x1");
ctx.getBeanFactory().registerSingleton("myTestBean1", test);        

log.trace("Registering second test bean");
BeanDefinition testDef = new RootBeanDefinition(Test.class);        
testDef.setScope(BeanDefinition.SCOPE_SINGLETON);        
ctx.getBeanFactory().registerSingleton("myTestBean2", testDef);

Test dummy1 = (Test)ctx.getBean("myTestBean1");
Object dummy2 = ctx.getBean("myTestBean2");

log.trace("beans: 1: {}; 2: {}", dummy1, dummy2); //<--this works, but...
ctx.refresh();

ctx.register(MyConfig.class); //configuring other beans

…When I do this from another bean configured inside MyConfig.class or annotated with @Component (It might also even be another dynamic component):

@Autowire Test myTestBean1;
//Or this:
Object a = this.ctx.getBean("myTestBean1"); //Or myTestBean2

It throws a NoSuchBeanDefinitionException

Please help me!! I been looking around a lot and havent found any useful info, or maybe I have but I cannot test all what I find in the web in such short time and the spring documentation only seems to be very good for normal cases and xml configurations, not for this crazy things I’m trying to do and not for programatically configuration.

  • 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-10T22:56:04+00:00Added an answer on June 10, 2026 at 10:56 pm

    What you’re after can’t be accomplished, sorry. Autowiring happens when a bean is initially created. In your case, you expect to wire a bean before you have added it to the factory, which of course won’t work – Spring doesn’t do time-travel.

    What I think you are after is the factory pattern. In Spring, you can defer instantiation of a bean to a factory you create by implementing the FactoryBean. Read more here:

    • http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-extension-factorybean
    • http://blog.springsource.org/2011/08/09/whats-a-factorybean/
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have many PowerPoint presentations that I need to be able to add to
I need to be able to add a new line to the end of
Basically what I need is being able to add new functionality to my application,
Users need to be able to add a specific type of column to an
I need to be able to add rows to a table dynamically with jQuery
I need some help to be able to add element to a JList and
I am trying to associate existing records while still being able to add new
I'm stuck - I need to have a Wicket Panel be able to add
Hopefully this is simple enough. I need to be able to add elements to
In my Backbone.js app, I need to be able to insert new items between

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.