Archive for Uncategorized

Spring Transaction Management using Aspects

For the sake of doing dependency injection into some Hibernate loaded domain classes (see previous post), I was forced to change an application to use AspectJ for it’s aspect weaving. When I did this, it broke my existing usage of the Spring @Transaction annotation.

Here’s what I had to do to fix it.

1. Define my transaction as annotation driven in the application context file.

<tx:annotation-driven transaction-manager="transactionManager" mode="aspectj"/>

2. Manually set the transactionManager on the AnnotationTransactionAspect class. Supposedly, this is a bug in Spring pre 2.5.x, but I was still encountering it even in 2.5.5.

<bean class="org.springframework.transaction.aspectj.AnnotationTransactionAspect" factory-method="aspectOf" dependency-check="none">
<property name="transactionManager" ref="transactionManager"/>
</bean>

Comments

Domain Model Dependency Injection

I recently had the need to add some rich features to an existing domain model. The application is built with Spring 2.5.x and Hibernate. When I started on this endeavor, I had no idea how many issues I would encounter. So, here’s what I ultimately had to do to get DI in my Hibernate loaded domain classes.

1. Use the @Configurable annotation on your domain class that you want to do DI with.
2. Use the @Autowired annotation on the member variable that needs DI.

@Configurable
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="material_type", discriminatorType=DiscriminatorType.STRING)
@org.hibernate.annotations.ForceDiscriminator
@Table(
name = "materials",
uniqueConstraints = {@UniqueConstraint(columnNames={"material_id"})}
)
public abstract class MaterialContent {
@Autowired
@Transient
protected ProtectedFileUrlDao protectedFileUrlDao;
}

3. Define the Autowired member variable as a bean in your application context file (there’s of course other ways to do this).

<bean id="protectedFileURLDao" class="com.sample.impl.ProtectedFileURLDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

4. Setup the standard application context settings to allow for Annotation DI. By default, spring will use compile-time weaving (fine by me).

<context:component-scan base-package="com.sample"/>
<context:spring-configured/>
<context:annotation-config/>

5. Setup your maven pom to compile the code with the AspecJ weaver.

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.0</version>
<configuration>
<source>1.5</source>
<verbose>true</verbose>
<complianceLevel>1.5</complianceLevel>
<showWeaveInfo>true</showWeaveInfo>
<aspectLibraries>
<aspectLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.6.1</version>
</dependency>
</dependencies>
</plugin>

Here’s some of the references that I used while setting this up:

Chris Richardson - enterprise POJOs
New Improvements in Domain Object Dependency Injection Feature

Comments

Getting Typo 5 running on Dreamhost

I decided to attempt to install Typo 5.0.2 on my Dreamhost account in an effort to learn something about Rails 2. Didn’t go so well, but here’s what I had to do to get it working:

  1. I had previously setup my Dreamhost account to leverage my own gems, ruby, etc. The best info for this is from Nate Clark.
  2. Install the typo gem. This was time consuming, because Dreamhost kept killing my *gem install* processes. In the end, I had to download each of the required gems independently and install them one by one. I just placed them in my home directory and then ran gem install gem_name.
  3. Install typo to a specific path by running typo install $HOME/blog.tjmoretto.com/. Typo has the actual instructions if you need.
  4. Configure the web directory of my subdomain to point to the $HOME/blog.tjmoretto.com/public directory of the typo install.
  5. Create a mysql database using the Dreamhost admin tool. I ended up using mysql, because when I first tried to write a blog entry with the default sqlite setup, I was getting an Invalid state error. Could not find any useful info on the typo mailing list for fixing this.
  6. Update the database.yml file to use the mysql database (default is sqlite).
  7. Create the database tables by running rake db:migrate RAILS_ENV=production from the root of the typo install.
  8. Update the dispatch.fcgi file to use my own gems by replacing the existing shebang line at the top of the file with #!/home/<user>/.packages/bin/ruby
  9. Test out the dispatch.fcgi process by running it from the command line. This can be done by going to the $HOME/blog.tjmoretto.com/public directory and running ./dispatch.fcgi. You should see the html output to the console if it is working correctly.
  10. Done!

Comments (3)