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

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

问题描述

我正在尝试使用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!框架,虽然其他autowired 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()中的artistDao。

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

知道我做错了吗?

Martin

推荐答案

您需要启用加载时编织(或其他类型的编织)才能使用 @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.

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

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