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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:06:16+00:00 2026-05-11T21:06:16+00:00

Does anyone know if there any way that I can programmatically create a bean

  • 0

Does anyone know if there any way that I can programmatically create a bean context?

I want to be able to do something like:

ConfigurableApplicationContext c = new ConfigurableApplicationContext();
BeanDefinition bd = new BeanDefinition();
bd.setId("id");
bd.setClassName("classname");
bd.setProperty("propertyName", propertyValue");
...etc...

or better still be able to inject a ready made bean into the application context:

c.addBean("beanId", beanObject);

Or if I’m using annotations:

c.setAnnotationAware(true);
c.setAnnotationScanBasePackage("packagename");

or

c.addAnnotatedSpringClass("classnamethatisannotated");

The rationale for this is that I want to be able override bean definitions for the purpose of testing – In my test I create this new application context, configured with code in the test (not in xml) and then make this test application context have as a parent the SUT application context.

I haven’t found any code in the spring libraries that can do this. Has anyone built something like this? Would it be possible to build something like this? I know the former approach is doable, I’m not 100% sure the latter approaches will work without conditions.

  • 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-11T21:06:16+00:00Added an answer on May 11, 2026 at 9:06 pm

    Try either:

    • Spring JavaConfig
    • Grails BeanBuilder
    • new AtUnit

    JavaConfig code sample

    @Configuration
    public class AppConfig {
        @Bean
        public TransferService transferService() {
            return new TransferServiceImpl();
        }
    }
    

    BeanBuilder code sample

    def bb = new grails.spring.BeanBuilder()
    
    bb.beans { 
      dataSource(BasicDataSource) { 
        driverClassName = "org.hsqldb.jdbcDriver" 
        url = "jdbc:hsqldb:mem:grailsDB" 
        username = "sa" 
        password = "" 
      } 
    
      sessionFactory(ConfigurableLocalSessionFactoryBean) { 
        dataSource = dataSource
        hibernateProperties = [ "hibernate.hbm2ddl.auto":"create-drop", "hibernate.show_sql":true ] 
      }   
    }
    

    AtUnit code sample

    Unit test

    @RunWith(AtUnit.class)
    @Container(Container.Option.SPRING)
    @MockFramework(MockFramework.Option.EASYMOCK)
    public class ExampleSpringEasyMockTest {
    
        @Bean @Unit UserManagerImpl manager;
        @Bean("fred") User fred;
        @Bean("userDao") @Mock UserDao dao;
        @Bean("log") @Stub Logger log;
    
        @Test
        public void testGetUser() {
            expect(dao.load(1)).andReturn(fred);
            replay(dao);
            assertSame(fred, manager.getUser(1));
            verify(dao);
        }
    
    
    }
    

    Context file ( private for the test )

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
    
      <bean id="userManager" class="atunit.example.subjects.UserManagerImpl">
          <constructor-arg ref="log"/>
          <property name="userDao" ref="userDao"/>
      </bean>
    
      <bean id="fred" class="atunit.example.subjects.User">
          <property name="id" value="500"/>
          <property name="username" value="fred"/>
      </bean>
    
    </beans>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 145k
  • Answers 146k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I think a closer equivalent than Rails' Observer is the… May 12, 2026 at 8:55 am
  • Editorial Team
    Editorial Team added an answer Couple of improvements (I'm sure that's not all but should… May 12, 2026 at 8:55 am
  • Editorial Team
    Editorial Team added an answer Could you inherit from the basic text field control (TextBox?)… May 12, 2026 at 8:55 am

Related Questions

I am having some trouble with macros in my excel workbook. I have a
I have a problem with reflection. I need to find the type that instantiates
I am trying to refer to a column name to order a query in
I'm building a web form in which administrators on my site can add XML

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.