私は、Spring を使用し、DAO をドメイン オブジェクトに注入して、永続層の直接的な知識を必要としないようにするというアイデアで遊んでい@Configurable
ます@Autowire
。
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();
これにより、Artist.save() で artistDao にアクセスしようとすると、NullPointerException が発生します。
私が間違っていることは何か分かりますか?
マーティン