Announcement Announcement Module
Collapse
No announcement yet.
PropertiesPlaceholderConfigurer to avoid bean creation Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • PropertiesPlaceholderConfigurer to avoid bean creation

    Hello

    I have to deploy an application which uses a datasource. This datasource should be configurable outside of spring.

    I know about to use a PropertiesPlaceholderConfigurer to enable the settings in a property file.

    But what if the datasource can also come from jndi? Can I define two datasource-beans (one regular and one retrieved via JndiObjectFactoryBean) and use a PropertiesPlaceholderConfigurer to configure which of them will be used. But what if there is no jndi available? Can I disable the bean creation from a PropertiesPlaceholderConfigurer to avoid errors on context-startup?

    regards
    Mark

  • #2
    You may use PropertyPlaceHolderConfigurer in class attribute of bean tag.
    Code:
    <beans>
    <bean id="datasource" class="${datasource.class}" />
    </beans>

    Comment


    • #3
      Hello chelu,

      I think that this is not working, because the two classes need different properties to be set.

      Hmm, I will try to use an own FactoryBean.

      regards
      Mark

      Comment


      • #4
        you can avoid instantation erros on JndiObjectFactoryBean if you set lazy-init
        and use PropertiesConfigurePlaceHolder to reference the bean thats must
        be instantiated from properties file.

        I think that should work.

        Comment


        • #5
          Hello chelu,

          this looks very good. Thank you!

          regards,
          Mark

          Comment


          • #6
            hello,

            i try something similar, but always get CannotLoadBeanClassException: Cannot find class [${jdbc.datasource}] for bean with name 'dataSource'.

            Any idea?

            Comment


            • #7
              Test yourself:

              context:
              Code:
              <?xml version="1.0" encoding="UTF-8"?>
              <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
              	"http://www.springframework.org/dtd/spring-beans.dtd">
              	
              <beans>
              	<bean name="pphConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
              		<property name="locations">
              			<list>
              				<value>classpath:/test/PPHConfigurer.properties</value>
              			</list>
              		</property>
              	</bean>
              	
              	<bean id="theString" class="${theClass}"/>
              </beans>
              Test Class:
              Code:
              public class PPHConfigurerTest extends TestCase {
              	
              	public void testPPHConfigurer() throws Exception {
              		ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:/test/PPHConfigurerContext.xml");
              		assertTrue(ctx.getBean("theString") instanceof String);
              	}
              and property file:
              Code:
              theClass=java.lang.String

              Comment


              • #8
                Thanks for the answer, but at my enviroment it doesnt work

                Comment

                Working...
                X