question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Property placeholder in bean class name results exception but bean is instantiated afterwards

See original GitHub issue

Affects: 4.3.30, 5.2.13, 5.3.4

I tried all three, same result.

I have a Spring Web Application. The root context is loaded with:

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/spring/*.xml</param-value>
	</context-param>

and files are:

/webapp/src/main/webapp/WEB-INF/spring
/webapp/src/main/webapp/WEB-INF/spring/jaxrs
/webapp/src/main/webapp/WEB-INF/spring/jaxws
/webapp/src/main/webapp/WEB-INF/spring/config-properties.xml
/webapp/src/main/webapp/WEB-INF/spring/root-context.xml
/webapp/src/main/webapp/WEB-INF/spring/x2tc-proxy-components.xml

Note JAX-RS and JAX-WS have their subcontexts. They do not matter now here.

config-properties.xml is merely:

	<beans:bean id="config" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
		<beans:property name="locations">
			<beans:array>
				<beans:value>classpath:x2tc-proxy.properties</beans:value>
				<beans:value>classpath:x2tc-proxy-override.properties</beans:value>
				<beans:value>file:${catalina.base}/conf/x2tc-proxy/tc-system.properties</beans:value>
				<beans:value>file:${catalina.base}/conf/x2tc-proxy/credentials.properties</beans:value>
			</beans:array>
		</beans:property>
		<beans:property name="ignoreResourceNotFound" value="true" />
	</beans:bean>

x2tc-proxy.properties contains:

...
teamcenter.auth.credentialsFactory.plain=....PlainCredentialsFactoryBean
teamcenter.auth.credentialsFactory.offlineSso=....OfflineSsoCredentialsFactoryBean
teamcenter.auth.credentialsFactory.onlineSso=...OnlineSsoCredentialsFactoryBean

All of these factory beans have everything autowired and return an object of interface type Credentials. Now credentials.properties contains specific config for a factory type, depending on a deployment. The properties file contains:

teamcenter.auth.credentialsFactory=onlineSso
# followed by all properties the `OnlineSsoCredentialsFactoryBean` requires.

x2tc-proxy-components.xml contains:

	<beans:bean id="credentialsFactory"
		class="${teamcenter.auth.credentialsFactory.${teamcenter.auth.credentialsFactory}}" />

I’d like to configure the concrete factory bean class name in the properties file w/o fiddling the beans definition. At startup I see the following:

22:30:53.037 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Ignoring bean class loading failure for bean 'credentialsFactory'
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [${teamcenter.auth.credentialsFactory.${teamcenter.auth.credentialsFactory}}] for bean with name 'credentialsFactory' defined in ServletContext resource [/WEB-INF/spring/x2tc-proxy-components.xml]; nested exception is java.lang.ClassNotFoundException: ${teamcenter.auth.credentialsFactory.${teamcenter.auth.credentialsFactory}}
	at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1397)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:638)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:607)
	at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1496)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:425)

but as it seems the factory bean is still wired and the objects from the factory are perfectly.

It is not clear whether this is intentional or bug. StackOverflow contains a bunch of questions where people try to provide a class name from a property.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
sbrannencommented, Feb 28, 2021

Yes, you have concluded correctly.

In general, if Spring logs something at DEBUG or TRACE level and your app still works fine, then the log messages are only there for informational purposes.

My tip: filter logging at INFO or WARN and only revert to DEBUG or TRACE if you run into issues.

0reactions
michael-ocommented, Feb 28, 2021

Thanks a million. I can safely put this in production now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Invalid bean definition: Could not resolve placeholder
It seems Spring container is trying to instantiate TestController bean before the PropertyPlaceholderConfigurer so property is not getting ...
Read more >
Chapter 3. Beans, BeanFactory and the ApplicationContext
When a bean property is a Java Class type, Spring allows you to specify the value for that property as a string value...
Read more >
Spring Core Certification 3 Mock Exam - Java & Moi
Select one or many correct answers about spring bean life cycle. 1. The method annoted with @PostConstruct is called after bean instantiation and...
Read more >
Property placeholders - Apache Camel
Camel has extensive support for property placeholders, which can be used almost anywhere in your Camel routes, endpoints, DSL, and route configuration, bean...
Read more >
Management & Monitoring - Micronaut Documentation
A new @InjectScope annotation has been added which destroys any beans with no defined scope and injected into a method or constructor annotated...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found