【发布时间】:2011-06-09 20:19:18
【问题描述】:
我正在考虑使用 Spring @Configurable 和 @Autowire 将 DAO 注入到域对象中,这样它们就不需要直接了解持久层。
我正在尝试关注http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/aop.html#aop-atconfigurable,但我的代码似乎没有效果。
基本上,我有:
@Configurable
public class Artist {
@Autowired
private ArtistDAO artistDao;
public void setArtistDao(ArtistDAO artistDao) {
this.artistDao = artistDao;
}
public void save() {
artistDao.save(this);
}
}
还有:
public interface ArtistDAO {
public void save(Artist artist);
}
和
@Component
public class ArtistDAOImpl implements ArtistDAO {
@Override
public void save(Artist artist) {
System.out.println("saving");
}
}
在 application-context.xml 中,我有:
<?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>
类路径扫描和初始化由 Play 的 spring 模块执行!框架,尽管其他自动装配的 bean 可以工作,所以我很确定这不是根本原因。我正在使用 Spring 3.0.5。
在其他代码中(实际上,在使用 Spring 注入到我的控制器中的 bean 方法中),我正在这样做:
Artist artist = new Artist();
artist.save();
这给了我一个 NullPointerException 尝试访问 Artist.save() 中的 artistDao。
知道我做错了什么吗?
马丁
【问题讨论】:
-
你能在你的
@Configurable标记类Artist中使用@Value注解吗?
标签: java spring aspectj spring-aop