Announcement Announcement Module
Collapse
No announcement yet.
Registers Spring bean programmatically Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Registers Spring bean programmatically

    Hi,
    I am using Spring3.1 on standalone env.

    I am trying to take a simple Pojo and register it as Spring bean programmatically.

    I read about using DefaultListableBeanFactory or GenericBeanDefinition. but still couldn't find my way.

    also I want to avoid memory leaks so I better be careful with those objects.

    So let's say I have this pojo:

    Code:
    PropertiesDTO propertiesDTO = new PropertiesDTO();
    		propertiesDTO .setMyProperty("1");
    
    		MyObject myObject = new MyObject ();
    		myObject.setPropertiesDTO(oropertiesDTO);
    and I want to add myObject to the spring context.

    * Ive put ApplicationContext instance in a Singleton class so I can get instance of it anywhere. maybe that could serve my purpose some how.

    Thanks for your help,
    ray.

  • #2
    I think this talks about exactly the same thing that you're trying to achieve programmatically.

    Comment


    • #3
      Yes.. I looked at this post and it didnt work.

      I tried to register the bean in two ways and I dont get how I suppose to connect my pojo instance into it:


      This is the bean:

      Code:
      public class SessionMDB
      {
      
      	@PostConstruct
      	public void init() throws Exception
      	{
      		System.out.println("I am alive");
      	}
      }

      and this is how I am trying to create new instance of it and register it as a spring bean:

      First way:

      Code:
      SessionMDB sessionMDB = new SessionMDB();
      ClassPathXmlApplicationContext context = ApplicationContextSingleton.getApplicationContext();
      
      		GenericBeanDefinition beanDef = new GenericBeanDefinition();
      		beanDef.setBeanClass(SessionMDB.class);
      		context.getBeanFactory().registerSingleton("SessionMDB", beanDef);

      second way:
      Code:
      SessionMDB sessionMDB = new SessionMDB();
      ClassPathXmlApplicationContext context = ApplicationContextSingleton.getApplicationContext();
      	DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) context.getBeanFactory();
      		beanFactory.registerBeanDefinition("SessionMDB", BeanDefinitionBuilder.rootBeanDefinition(SessionMDB.class.getName()).getBeanDefinition());
      This is my ApplicationContextSingleton class:

      Code:
      public class ApplicationContextSingleton
      {
      	static ClassPathXmlApplicationContext context;
      
      	private ApplicationContextSingleton()
      	{
      	}
      
      	public static ClassPathXmlApplicationContext getApplicationContext()
      	{
      		if (context == null)
      		{
      			context = new ClassPathXmlApplicationContext(Constants.APPLICATION_CONTEXT_XML);
      
      		}
      		return context;
      	}
      
      }

      any idea?

      thanks,
      ray.

      Comment

      Working...
      X