Results 1 to 5 of 5

Thread: How to override a bean definition at config time

  1. #1

    Default How to override a bean definition at config time

    Hi,

    I have a large number of beans defined in a spring config file A.xml, and I import them in file B.xml.

    One bean in A.xml can no longer be instantiated in the new environment that I am using it in. e.g. it requires a JNDI lookup for a data source, and my current test application is a standalone one, which doesn't have a JNDI directory since it isn't running in a server.

    So I override the bean definition by providing another bean in B.xml with same id as that in A.xml. This doesn't seem to work, and I have the following error:

    -----------------
    Error creating bean with name 'gen.db.datasource' defined in class path resource [com/def/abc/gen/db-spring-config.xml]: Invocation of init method failed; nested exception is javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
    Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
    at javax.naming.spi.NamingManager.getInitialContext(N amingManager.java:645)
    at javax.naming.InitialContext.getDefaultInitCtx(Init ialContext.java:247)
    at javax.naming.InitialContext.getURLOrDefaultInitCtx (InitialContext.java:284)
    at javax.naming.InitialContext.lookup(InitialContext. java:351)
    at org.springframework.jndi.JndiTemplate$1.doInContex t(JndiTemplate.java:124)
    at org.springframework.jndi.JndiTemplate.execute(Jndi Template.java:86)
    at org.springframework.jndi.JndiTemplate.lookup(JndiT emplate.java:122)
    at org.springframework.jndi.JndiTemplate.lookup(JndiT emplate.java:147)
    at org.springframework.jndi.JndiLocatorSupport.lookup (JndiLocatorSupport.java:90)
    at org.springframework.jndi.JndiObjectLocator.lookup( JndiObjectLocator.java:101)
    at org.springframework.jndi.JndiObjectFactoryBean.loo kupWithFallback(JndiObjectFactoryBean.java:164)
    at org.springframework.jndi.JndiObjectFactoryBean.aft erPropertiesSet(JndiObjectFactoryBean.java:151)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.invokeInitMethods(Abstr actAutowireCapableBeanFactory.java:1062)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.initializeBean(Abstract AutowireCapableBeanFactory.java:1029)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:420)
    at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 45)
    at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:141)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:242)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:156)
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.preInstantiateSingletons(Defaul tListableBeanFactory.java:287)
    at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:348)
    at org.springframework.context.support.ClassPathXmlAp plicationContext.(ClassPathXmlApplicationContext.j ava:92)
    at org.springframework.context.support.ClassPathXmlAp plicationContext.(ClassPathXmlApplicationContext.j ava:77)
    at org.springframework.context.support.ClassPathXmlAp plicationContext.(ClassPathXmlApplicationContext.j ava:68)

    ------------------

    How do I override a bean definition in A.xml while still using all the other beans in A.xml ?

    I will appreciate any help from the spring user community.

    Thanks,
    Sunil Jigyasu
    Last edited by Sunil Jigyasu; Jul 20th, 2007 at 03:10 PM.

  2. #2
    Join Date
    Sep 2004
    Posts
    1,086

    Default

    It's possible to do the overriding, but I wouldn't recommend it really, as it's dependent on the ordering of the files at the load time.

    I'd suggest separating the bean that's context dependent in a new file and load different versions of that file depending on the context.

  3. #3
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    Quote Originally Posted by dejanp View Post
    I'd suggest separating the bean that's context dependent in a new file and load different versions of that file depending on the context.
    Personally, I'd go for the same approach. I've found nothing but trouble in the past when people rely on the overriding order and then forget about this in different deployments.
    Last edited by karldmoore; Aug 27th, 2007 at 04:09 PM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  4. #4

    Default

    Thanks for your replies.

    I bypassed the problem by separating the bean definitions alongwith their environments in separate spring config files. i.e. the spring config having JNDI lookup element isn't loaded in the (non-container) environment.

    Sunil Jigyasu

  5. #5
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    Quote Originally Posted by Sunil Jigyasu View Post
    I bypassed the problem by separating the bean definitions alongwith their environments in separate spring config files. i.e. the spring config having JNDI lookup element isn't loaded in the (non-container) environment.
    As I said previously, IMHO this is a better solution anyway and less prone to error.
    Last edited by karldmoore; Aug 27th, 2007 at 04:09 PM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

Posting Permissions

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