Configuring DAOs the standard way can become cumbersome if your application requires a lot of DAOs. With help of Spring 2.5 namespaces there is a more sophisticated way to configure them.
Example 2.11. DAO configuration with Spring namespaces
<?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:hades="http://schemas.synyx.org/hades" (1) xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://schemas.synyx.org/hades (2) http://schemas.synyx.org/hades/hades.xsd"> <import resource="infrastructure.xml" /> (3) <hades:dao-config base-package="org.synyx.hades.dao"> (4) <hades:dao id="userDao" /> (5) <hades:dao id="roleDao" /> </hades:dao-config> </beans>
(1)(2) | Import the dao namespace to be available in the xml configuraton document. |
(3) | Import infrastructure configuration. |
(4) | Declare base packages to construct domain classes and DAO interfaces. |
(5) | Declare a single DAO instance. Each |
This configuration replaces simple configuration mechanism entirely so we encourage to use this kind of configuration over the simple one.
The <dao-config /> element allows two further
attributes to be defined. With dao-name-postfix you can
customize the bean id for the DAO registered. This defaults to
Dao.
You can simplify configuration even more by adding no
<dao /> elements to <dao-config
/> at all.
Example 2.12. Automatic DAO configuration
<?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:hades="http://schemas.synyx.org/hades" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://schemas.synyx.org/hades http://schemas.synyx.org/hades/hades.xsd"> <import resource="infrastructure.xml" /> <hades:dao-config base-package="org.synyx.hades.dao" /> </beans>
This will trigger auto detection mechanism of DAOs that extend
GenericDao or
ExtendedGenericDao. For each candidate
found it will set up an DAO instances supposing you've placed the DAO
interfaces in the configured package and named them as described
above.
In case you need to wire an
EntityManagerFactory to a DAO
instance or all DAO instances explicitly use
entity-manager-factory-ref attribute to point to the
desired Spring bean.
By default Hades will pick up every interface extending
GenericDao located underneath the
configured base package and create a bean instance for it. However,
you might want to gain finer grained control over which interfaces
bean instances get created for. Hades supports the use of
<include-filter /> and <exclude-filter
/> elements inside <dao-config />. The
samantics are exactly equivalent to the elements in Spring's context
namespace. For details see Spring reference documentation on these
elements.
E.g. to exclude certain interfaces from instantiation as Hades DAO, you could use the following configuration:
Example 2.13. Using exclude-filter element
<hades:dao-config base-package="org.synyx.hades.dao"> <exclude-filter type="regex" expression=".*SomeDao" /> </hades:dao-config>
This would exclude all interface ending on
SomeDao from being
instantiated.