Spring Annotations
Sheesh, the trouble I'm apparently willing to go through to save myself a few lines of XML configuration! This story began with the promise that I'd never have to write any more <bean> entries in my beans.xml file... Spring Annotations would do it for me! Great! Even better, any @Entity objects would magically get loaded into Hibernate so I wouldn't have to add those stupid lines for every single class. Real progress!
Spring Annotations web site.
Spring Annotations is in a transitional phase, moving from java.net over to sourceforge. That causes problems, as much of the documentation has already been updated to point to sourceforge urls and classnames, but the actual source SVN repository is still on java.net (at least the one referred to in the docs). And as far as I can tell, they haven't been able to add their project to any public Maven2 repostitory, so anyone wanting to use Maven will need to check out the project, fix their pom file (it's missing a 'description' node) and publish to their own repository. To fix that for you, here's the xml so you can add it to your own Maven2-based project:
<repository>
<id>pelzer-util</id>
<url>http://util.pelzer.com/pelzer-util/repository/</url>
</repository>
<dependency>
<groupId>net.java.dev.spring-annotation</groupId>
<artifactId>spring-annotation-base</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>net.java.dev.spring-annotation</groupId>
<artifactId>spring-annotation-hibernate</artifactId>
<version>1.1</version>
</dependency>
Now that you've got Maven2 going, you're nearly there! In your beans.xml (or whatever you've named your primary Spring config xml, add the following:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:sa="https://spring-annotation.dev.java.net/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
https://spring-annotation.dev.java.net/context https://spring-annotation.dev.java.net/context.xsd"
default-autowire="byName">
<!-- Enable autoloading for Spring Annotations -->
<sa:annotation-autoload />
...
</beans>
To enable Hibernate @Entity auto-discovery, change your sessionFactory in beans.xml to this:
<bean id="sessionFactory"
class="net.java.dev.springannotation.hibernate.AutomaticAnnotationSessionFactoryBean">
Now, you're ALMOST done, and if you just run as a regular java class, you're actually totally done. But to run inside Tomcat, you need to do one more thing, and that's to create a beacon file, named 'to.properties'. This file threw me off for almost 2 whole days as I tried to debug why Spring Annotations worked great while I was in Eclipse, and passed all our Bamboo unit tests, but wouldn't work inside Tomcat! Create an empty (doesn't have to be empty, just doesn't need anything) file and get it packaged. Since we're using maven, it should be 'src/main/resources/to.properties'. This file allows SA to load up and find all your annotated classes.
Now you're ready to drop your 'bean' entries from the xml, as well as your hibernate schtuff. The simplest, since we turned on autowire, is to just annotate a bean:
@Bean( name="myFirstBean" )
Hope this helps you out. If you end up using my repository, drop a comment! Cheers!
I picked up my very own Nintendo Wii this weekend. I was in upstate NY for a funeral, but I still managed to get my butt over to Target at 7:15am to get in line. I was #41 of 42 folks who got systems, so I guess I'm lucky... Though now I find out that the Nintendo Store here in NYC has a surplus of them! But I will always get to say I stood in line... For 45 whole minutes! I'm a warrior.




