Spring 自动装配使用 @Configurable [英] Spring autowiring using @Configurable

查看:32
本文介绍了Spring 自动装配使用 @Configurable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Spring @Configurable@Autowire 将 DAO 注入到域对象中,这样它们就不需要直接了解持久层.

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.

我正在尝试遵循 http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/aop.html#aop-atconfigurable,但是我的代码好像没有效果.

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.

基本上,我有:

@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 中,我有:

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>

类路径扫描和初始化由 Play 的 spring 模块执行!框架,虽然其他自动装配的 bean 工作,所以我很确定这不是根本原因.我使用的是 Spring 3.0.5.

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.

在其他代码中(实际上,在使用 Spring 注入控制器的 bean 方法中),我正在这样做:

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();

这给了我一个 NullPointerException 尝试访问 Artist.save() 中的艺术家.

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

知道我做错了什么吗?

马丁

推荐答案

您需要启用加载时编织(或其他类型的编织)才能使用 @Configurable.确保正确启用它,如 7.8.4 在 Spring 框架中使用 AspectJ 进行加载时编织.

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.

这篇关于Spring 自动装配使用 @Configurable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆