I have a desktop app in Java that connects to a MySql database through Hibernate.
I have the following files for configuration:
- pom.xml, where I put some stuff and database profiles
- [app_name]_context.xml, where I put the main configuration, including the beans and the datasource
- [app_name]_hibernate.cfg.xml, where I put the mappings to the tables
The app is almost finished, everything was working 100%, but then I decided to change the configuration to create DB profiles (before that the pom.xml didn’t include any profile).
So I created the profiles in pom.xml and referenced them from context.xml. Everything was working. Then I wanted the SQL to be printed to the terminal for debugging, and for some reason, it wasn’t being printed. So I changed the database configurations until I got it right. Everything was working. Then I changed it to the way it was before the sql-print changes, the exact configuration it was working before, but now I’m getting this error for a bean that was always there, and was never changed.
Here’s some code to understand it better:
The datasource in context.xml: (the ${} strings are filtered and replaced with the real values stored in pom.xml)
<bean id="desenvolvimentoDS" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>org.hibernate.dialect.MySQLDialect</value>
</property>
<property name="url">
<value>${db.mysql.url}</value>
</property>
<property name="username">
<value>${db.mysql.username}</value>
</property>
<property name="password">
<value>${db.mysql.password}</value>
</property>
</bean>
The current profile being used, in pom.xml:
<profile>
<id>HOMOLOGACAO</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<profile.group>HOMOLOGACAO</profile.group>
<maven.test.skip>true</maven.test.skip>
<db.show_sql>true</db.show_sql>
<db.format_sql>false</db.format_sql>
<db.mysql.url>[filtered]</db.mysql.url>
<db.mysql.username>[filtered]</db.mysql.username>
<db.mysql.password>[filtered]</db.mysql.password>
</properties>
</profile>
The resource part of pom.xml, that tells Spring to filter those context.xml strings to the profiles values:
<resources>
<resource>
<directory>src/main/resources/configuracao</directory>
<filtering>true</filtering>
</resource>
</resources>
And finally, the bean that is giving me the error:
<bean name="servidorUDP"
class="br.uff.pgci.sgca.gateway.server.ServidorUDP"
scope="singleton"/>
Resuming, I created the database profiles to organize the configurations, changed some database configurations back and forth, and suddenly I’m getting “NoSuchBeanDefinitionException no bean named ServidorUDP” error.
Could this be caused by misplaced linebreaks, or spaces, or wrong open/close xml tags? I’m pretty sure this isn’t the case, but we never know…
Where exactly you placed your [app_name]_context.xml? Does the filters you introduced in the pom is skipping the bean definition xml?
Check this maven pom reference http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html, it might give you some clues.