Results 1 to 3 of 3

Thread: Registers Spring bean programmatically

  1. #1

    Default 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. #2
    Join Date
    Dec 2009
    Location
    India
    Posts
    108

    Default

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

  3. #3

    Default

    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •