I have a data access module that provides implementations of repositories using Spring and JDBC.
Therefore, I define a Spring context as follows:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager" />
<bean id="annotationTransactionAspect" factory-method="aspectOf" class="org.springframework.transaction.aspectj.AnnotationTransactionAspect">
<property name="transactionManager" ref="transactionManager" />
</bean>
</beans>
I also expose the repositories implementations as services using Declarative Services as follows:
<?xml version="1.0" encoding="UTF-8"?>
<component name="cdr-repository" enabled="true" xmlns="http://www.osgi.org/xmlns/scr/v1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.osgi.org/xmlns/scr/v1.1.0 http://www.osgi.org/xmlns/scr/v1.1.0">
<!-- Property and Properties -->
<properties entry="OSGI-INF/myrepository-component.properties" />
<!-- Service (optional) -->
<service>
<provide interface="com.example.osgi.dataaccess.api.MyRepository" />
</service>
<!-- Zero or more references to services -->
<!-- Exactly one implementation -->
<implementation class="com.example.osgi.dataaccess.jdbc.impl.MyRepositoryImpl" />
</component>
So, my services are created outside the Spring environment and therefore they are not fully configured (e.g. datasource is not injected).
I'm looking for the right way to integrate Spring with Declarative Services.
Thanks, Mickael
Spring and Declarative services are not meant to be used together. So the approach you use will not work.Declarative services is a very simple framework that can only wire services into components and publish components as services. It has no capabilities regarding jpa. So I think DS will not be a good match if you want to use something like jpa with container managed persistence.
Like Holly mentioned blueprint supports that with the help of some other aries modules. I have created a tutorial that shows how to use this in a complete example. See: http://liquid-reality.de/pages/viewinfo.action?pageId=6586413
The blueprint approach is very different from how spring does jpa and container managed transactions. In spring you normally create the datasource in your context and inject it. In blueprint you work more like in standard jpa. There the datasource is referenced in persistence.xml and looked up using jndi. Aries jndi then bridges from jndi to OSGi services so another bundle can offer the datasource as an OSGi service.
The other option you have is to use spring dm to create jpa services the "spring way". But spring dm is not maintained and has a lot of problems in OSGi. So blueprint is currently the best way to go.
Related
My application has 3 possible profiles - dev, stage and prod. My spring boot application has a xml config with 2 profiles that seem to be not working.
<bean id="globalBean">
<ref bean="myBean">
</bean>
<beans profile="!stage">
<bean id="myBean">
<property name="name" value="notStage" />
</bean>
<!--There are more bean definitions here common for dev and prod and have reference to myBean defined above-->
</beans>
<beans profile="dev">
<bean id="myBean">
<property name="name" value="dev" />
</bean>
</beans>
<beans profile="stage">
<bean id="myBean">
<property name="name" value="stage" />
</bean>
</beans>
In dev mode, my application doesn't somehow pick the dev definition and defaults to the bean definition defined in profile="!stage".
As mentioned I need the profile="!stage" to hold common beans in dev and prod.
Specifying profile as dev matches both !stage and dev.
you can define multiple profiles in bean declaration like this
<beans profile="dev, prod">
You can defines bean declarations common to dev and prod here
I have working code that saves entity to DB with using EJB+JPA+Hibernate. Now I need to change EJB to Spring.
Below is my simplified manager class.
//#Stateless - changed to #Service
#Service
public class Manager {
//#EJB - changed to Autowired
#Autowired
private ClientDao clientDao;
public void addClient(Client client) {
clientDao.addClient(client);
}
}
Below is my DAO class.
//#Stateless - changed to #Repository
#Repository
public class JpaClientDao implements ClientDao {
#PersistenceContext(unitName="ClientsService")
private EntityManager em;
public void addClient(Client client) {
em.persist(client);
}
}
Below is persistence.xml.
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="ClientsService" transaction-type = "JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>myJtaDatabase</jta-data-source>
<class>com.entity.Client</class>
<properties>
<property name="hibernate.archive.autodetection" value="class"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="com" />
<context:annotation-config/>
</beans>
resoures.xml
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<Resource id="myJtaDatabase" type="DataSource">
JdbcDriver org.apache.derby.jdbc.ClientDriver
JdbcUrl jdbc:derby://localhost:1527/C:/ClientDB
UserName test
Password 123456
validationQuery = SELECT 1
JtaManaged true
</Resource>
</resources>
Questions.
1) When I used EJB, I had Container-Managed Transactions. Who should manage transactions with using Spring?
2) Do I need to necessarily use Spring Framework transaction management? Is there any alternatives?
I found some examples like this http://www.codingpedia.org/ama/java-persistence-example-with-spring-jpa2-and-hibernate/, and I cant undestand is code below spring specific or it suits for me.
<!-- ************ JPA configuration *********** -->
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath:config/persistence-demo.xml" />
<property name="dataSource" ref="restDemoDS" />
<property name="packagesToScan" value="org.codingpedia.demo.*" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
</bean>
</property>
</bean>
3) Do I need to edit Java code or should my steps be in xml configuration?
Any useful links are encouraged.
1) When I used EJB, I had Container-Managed Transactions. Who should
manage transactions with using Spring?
Answer : Spring provides container managed transaction support too (See JTA transaction manager , sample oracle article) and also application managed transaction (which means your application can declaratively/programatically manage the transactions with minimal effort by using Spring transaction API ) . See Spring transaction doc
2) Do I need to necessarily use Spring Framework transaction
management? Is there any alternatives?
Answer: If spring framework is not managing your transactions, then your container will need to manage them..you have alternatives of any Java EE JTA implementations like opensource JBossTS or enterprise JTA implementations Oracle-WebLogicJtaTransactionManager or IBM-WebSphereUowTransactionManager, you can find how to use them in the same point 1 spring documentation. You can even have your own transaction manager implemented.
But if you are already using Spring framework why not benefit from spring transaction management with plenty of configurations possible, (Spring + Hibernate transaction manager, Spring + JPAContainerManager, Spring+ JTAContainerManager etc..)
3) Do I need to edit Java code or should my steps be in xml
configuration?
Answer: Your transcation manager xml config seems fine to use a JpaTransactionManager, now you can initiate transactions in your service layer java code by annotating #Transactional which typically handles your service methods to participate in a transaction according to the configured transaction manager.
#Service
#org.springframework.transaction.annotation.Transactional
public class Manager {
I have a java web application. This application uses JPA for persistence. The persistence is managed by spring. This application can easily be deployed on Tomcat, just drop the war file in webapps. But deploying this application in JBoss became a nightmare to me for several days. I couldn't find an appropriate post on this site to solve the issue hence this post.
First and foremost, you need to configure a datasource (I used mysql database). My JBoss was installed under C:\jboss-eap-6.3\ and I used windows operating system.
Steps:
1. Create a directory structure com\mysql\main under C:\jboss-eap-6.3\modules. You will end up with C:\jboss-eap-6.3\modules\com\mysql\main directory structure.
2. Create an xml file module.xml in this main directory. Additionally drop mysql driver jar mysql-connector-java-5.1.23-bin.jar in the same directory. In the the end you will have module.xml and mysql-connector-java-5.1.23-bin.jar in C:\jboss-eap-6.3\modules\com\mysql\main.
3. Copy the following xml content to module.xml
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.0" name="com.mysql">
<resources>
<resource-root path="mysql-connector-java-5.1.23-bin.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
<module name="javax.transaction.api"/>
</dependencies>
</module>
4. Locate the file standalone.xml in C:\jboss-eap-6.3\standalone\configuration directory. Open this file with your favorite text editor. Locate the datasource subsystem i.e. this line <subsystem xmlns="urn:jboss:domain:datasources:1.2">. Add the xml fragment below under the drivers element.
<driver name="mysqlDriver" module="com.mysql">
<xa-datasource-class>com.mysql.jdbc.Driver</xa-datasource-class>
</driver>
Add the xml fragment below under datasources element:
<datasource jndi-name="java:jboss/datasources/MYSQLDATASOURCE" pool-name="MYSQLDATASOURCE" enabled="true" use-java-context="true">
<connection-url>jdbc:mysql://localhost:3306/databaseName</connection-url>
<driver>mysqlDriver</driver>
<security>
<user-name>root</user-name>
<password>databasepassword</password>
</security>
</datasource>
At this point you are done with datasource configuration.
Next is to configure your persistence.xml and applicationContext.xml.
persistence.xml
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="erPU">
<class>..</class> <!--Contains all your Entity classes-->
<properties>
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="false" />
<property name="jboss.as.jpa.providerModule" value="application" />
<property name="jboss.as.jpa.managed" value="false" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
</properties>
</persistence-unit>
</persistence>
<!--jboss.as.jpa.providerModule is the name of the persistence provider module (default is org.hibernate). Should be hibernate3-bundled if Hibernate 3 jars are in the application archive (adapterModule and adapterClass will automatically be set for hibernate3-bundled). Should be application, if a persistence provider is packaged with the application. -->
<!--jboss.as.jpa.managed can be set to false to disable container managed JPA access to the persistence unit. The default is true, which enables container managed JPA access to the persistence unit. This is typically set to false for Seam 2.x + Spring applications. -->
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
">
<context:annotation-config />
<context:component-scan base-package="..." /><!--Package base name to scan for annotations -->
<jee:jndi-lookup id="dataSource" jndi-name="java:jboss/datasources/MYSQLDATASOURCE" expected-type="javax.sql.DataSource"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="..." /><!--Packages to scan for entities-->
<property name="persistenceUnitName" value="erPU" />
<property name="persistenceXmlLocation" value="classpath*:META-INF/persistence.xml"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="false" />
</bean>
</property>
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.hbm2ddl.auto" value="update" />
<entry key="hibernate.format_sql" value="false" />
<entry key="hibernate.show_sql" value="false" />
<entry key="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
</map>
</property>
</bean>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
Note: Irrespective of the location of persistence.xml, you need to tell spring where the persistence.xml is located. The literature below was extracted from
Using Spring-managed persistence units
Spring applications running in JBoss AS7 may also create persistence units on their own, using the LocalContainerEntityManagerFactoryBean. This is what these applications need to consider:
Placement of the persistence unit definitions
When the application server encounters a deployment that has a file named META-INF/persistence.xml (or, for that matter, WEB-INF/classes/META-INF/persistence.xml), it will attempt to create a persistence unit based on what is provided in the file. In most cases, such definition files are not compliant with the Java EE requirements, mostly because required elements such as the datasource of the persistence unit are supposed to be provided by the Spring context definitions, which will fail the deployment of the persistence unit, and consequently of the entire deployment.
Spring applications can easily avoid this type of conflict, by using a feature of the LocalContainerEntityManagerFactoryBean which is designed for this purpose. Persistence unit definition files can exist in other locations than META-INF/persistence.xml and the location can be indicated through the persistenceXmlLocation property of the factory bean class.
Assuming that the persistence unit is in the META-INF/jpa-persistence.xml, the corresponding definition can be:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath*:META-INF/jpa-persistence.xml"/>
<!-- other definitions -->
</bean>
WEB-INF/jboss-deployment-structure.xml
Lastly, you need to create jboss-deployment-structure.xml under WEB-INF. The content of the file should be:
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.0">
<deployment>
<exclusions>
<module name="org.hibernate"/>
</exclusions>
</deployment>
</jboss-deployment-structure>
The reason for this is:
Since the LocalContainerEntityManagerFactoryBean and the corresponding HibernateJpaVendorAdapter are based on Hibernate 3, it is required to use that version with the application. Therefore, the Hibernate 3 jars must be included in the deployment. At the same time, due the presence of #PersistenceUnit or #PersistenceContext annotations on the application classes, the application server will automatically add the 'org.hibernate' module as a dependency.
This can be avoided by instructing the server to exclude the module from the deployment's list of dependencies.
Finally, the JPA libraries bundled with your application must be version 3.x.
I am trying to configure JMS connection caching and consumer concurrency with Spring to perform some load tests. The application context xml is below.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:cxf="http://cxf.apache.org/core"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<context:annotation-config />
<bean id="amqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://172.18.2.100:8080"/>
</bean>
<bean id="clientCachingConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<property name="targetConnectionFactory" ref="amqConnectionFactory"/>
<property name="sessionCacheSize" value="20"/>
<property name="cacheProducers" value="true"/>
</bean>
<bean id="clientContainerListener" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="clientCachingConnectionFactory" />
<property name="destinationName" value="test.load.outside.multispeak.ch.queue" />
</bean>
<jaxws:client
id="load-test-multispeak-ch-client"
xmlns:ns="http://www.multispeak.org/Version_4.1_Release"
serviceClass="org.multispeak.version_4_1_6.CH.CHServerSoap"
serviceName="ns:CH_Server"
endpointName="ns:CH_ServerSoap"
address="jms://"
wsdlLocation="classpath:CH_Server.wsdl">
<jaxws:features>
<bean class="org.apache.cxf.transport.jms.ConnectionFactoryFeature">
<constructor-arg index="0" ref="clientCachingConnectionFactory"/>
</bean>
</jaxws:features>
</jaxws:client>
<bean id="serverCachingConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<property name="targetConnectionFactory" ref="amqConnectionFactory"/>
<property name="sessionCacheSize" value="20"/>
<property name="cacheConsumers" value="true"/>
</bean>
<bean id="serverContainerListener" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="serverCachingConnectionFactory" />
<property name="destinationName" value="test.load.outside.multispeak.ch.queue" />
<property name="cacheLevel" value="3" />
<property name="concurrentConsumers" value="10" />
<property name="maxConcurrentConsumers" value="50" />
</bean>
<jaxws:endpoint
id="load-test-multispeak-ch-server"
xmlns:tns="http://www.multispeak.org/Version_4.1_Release"
implementor="pt.fraunhofer.outside.multispeak.ch.server.CHServerSoapImpl"
serviceName="tns:CH_Server"
endpointName="tns:CH_ServerSoap"
publish="false"
address="jms://"
wsdlLocation="classpath:CH_Server.wsdl">
<jaxws:features>
<bean class="org.apache.cxf.feature.LoggingFeature"/>
<bean class="org.apache.cxf.transport.jms.ConnectionFactoryFeature">
<constructor-arg index="0" ref="serverCachingConnectionFactory"/>
</bean>
</jaxws:features>
</jaxws:endpoint>
According to CXF documentation, from 3.0, a ConnectionFactoryFeature should be used instead of a JMSConfigFeature, which is deprecated (http://cxf.apache.org/docs/jms-transport.html). I was following the examples provided at http://cxf.apache.org/scalable-cxf-applications-using-jms-transport.html, but the documentation there seems to refer to CXF versions earlier than 3.0, because JMSConfiguration class in 3.0 does not have caching or concurrency capabilites (no setters/getters). So, I was trying to use Spring to achieve the same result, but without success. The very same documentation refers to Spring DefaultMessageListenerContainer as a container for caching and concurrency configuration, but I was not able to find examples of this with CXF integration, only for pure JMS. Also, DefaultMessageListenerContainer has a setter to register a JMS MessageListener. In case of CXF, I found that that listener is created and managed by CXF runtime and is not provided provided by the application.
Any advice?
Thanks!
Currently you can not set concurrent consumers for CXF 3. I did some performance tests with ActiveMQ and found the performance with CXF 3 is equal to CXF 2 or even better.
The reason is that we now use a MessageListener approach instead of the polling that spring did.
See my website for the performance tests.
We need to be able to tune concurrent and maximum consumers so we can handle our message load. The constraint is in speed of message processing by our applications; CXF is not the bottleneck. Are there plans to re-introduce these parameters in CXF? Can we configure CXF to use a configured DefaultMessageListenerContainer?
I have an xml in 3.0 like so:
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.internal.url}" />
<property name="username" value="${jdbc.internal.username}" />
<property name="password" value="${jdbc.internal.password}"/>
</bean>
I want to convert this to 3.1 while making use of the beans:profile However, when I try to change it to this:
<beans profile="dev">
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.internal.url}" />
<property name="username" value="${jdbc.internal.username}" />
<property name="password" value="${jdbc.internal.password}"/>
</bean>
</beans>
I get errors like:
Invalid content was found starting with element 'bean'. One of '{"http://www.springframework.org/schema/beans":beans}'
Question
How can I make use of the beans:profile so that this particular bean definition only gets called when the active profile is dev
Update
My beans definition is:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd">
You must put all nested <beans> declarations at the very end of the configuration file. This is how XML schema is defined and you have to obey this.
See also
Spring Framework 3.1 M1 released:
spring-beans-3.1.xsd has been updated to allow this nesting, but constrained to allow such elements only as the last ones in the file.
This is by design.
From the SpringSource blog:
spring-beans-3.1.xsd has been updated to allow this nesting, but constrained to allow such elements only as the last ones in the file. This should help provide flexibility without incurring clutter in the XML files. While this enhancement was developed in service of bean definition profiles, nested elements are useful in general. Imagine you have a subset of beans in a given file that should be marked lazy-init="true". Rather than marking each bean, you could instead declare a nested element, and all beans within will inherit that default. Beans defined elsewhere in the file will maintain the normal default of lazy-init="false". This applies for all the default-* attributes of the element, such as default-lazy-init, default-init-method, default-destroy-method, and so on.
I had the same problem : I have never suceed to nest <beans> inside an other <beans>, even if the spring-beans.xsd was correct.
My (partial) solution was to create an other applicationContext.xml starting with
<beans profile="dev, qualif"
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>