Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring autowiring using @Configurable

I'm playing with the idea of using Spring @Configurable and @Autowire to inject DAOs into domain objects so that they do not need direct knowledge of the persistence layer.

I'm trying to follow http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/aop.html#aop-atconfigurable, but my code seems to have no effect.

Basically, I have:

@Configurable public class Artist {      @Autowired     private ArtistDAO artistDao;      public void setArtistDao(ArtistDAO artistDao) {         this.artistDao = artistDao;     }      public void save() {         artistDao.save(this);     }  } 

And:

public interface ArtistDAO {      public void save(Artist artist);  } 

and

@Component public class ArtistDAOImpl implements ArtistDAO {      @Override     public void save(Artist artist) {         System.out.println("saving");     }  } 

In application-context.xml, I have:

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springsource.org/dtd/spring-beans-2.0.dtd"> <beans>      <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />     <bean class="org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect" factory-method="aspectOf"/>  </beans> 

Class path scanning and initialisation is performed by the spring module for Play! framework, although other autowired beans work, so I'm pretty sure this is not the root cause. I'm using Spring 3.0.5.

In other code (inside a method in bean that's injected into my controller using Spring, in fact), I'm doing this:

Artist artist = new Artist(); artist.save(); 

This gives me a NullPointerException trying to access the artistDao in Artist.save().

Any idea what I'm doing wrong?

Martin

like image 609
optilude Avatar asked Jan 16 '11 01:01

optilude


People also ask

Can we Autowire @configuration class?

In addition to being able to reference any particular bean definition as seen above, one @Configuration class may reference the instance of any other @Configuration class using @Autowired . This works because the @Configuration classes themselves are instantiated and managed as individual Spring beans.

What is @configurable in Spring?

Spring @Configuration annotation is part of the spring core framework. Spring Configuration annotation indicates that the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application.

Can I use @autowired in interface?

If you try to use @Autowired on an interface, the Spring framework would throw an exception as it won't be able to decide which implementation class to use.

What is @autowired annotation in Spring?

The @Autowired annotation provides more fine-grained control over where and how autowiring should be accomplished. The @Autowired annotation can be used to autowire bean on the setter method just like @Required annotation, constructor, a property or methods with arbitrary names and/or multiple arguments.


1 Answers

You need to enable load-time weaving (or other kinds of weaving) in order to use @Configurable. Make sure you enabled it correctly, as described in 7.8.4 Load-time weaving with AspectJ in the Spring Framework.

like image 140
axtavt Avatar answered Oct 08 '22 18:10

axtavt