DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Thomas has posted 33 posts at DZone. View Full User Profile

Using AspectJ To Dependency Inject Domain Objects With Spring

  • submit to reddit
        Spring configuration: 

<context:spring-configured />
<context:component-scan base-package="some.package.domain" />
<context:load-time-weaver aspectj-weaving="on" />

A base entity that use Hibernate: 
package come.package.domain;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;

public abstract class Entity<T, ID extends Serializable> {
	private SessionFactory sessionFactory;
	public T getById(ID id) {
		return (T) getSession().get(getClass(), id);
	public ID save() {
		return (ID) getSession().save(this);

	protected Session getSession() {
		return sessionFactory.getCurrentSession();

JVM argument : 

More info: