是上下文:注释-config来@Autowired替代? [英] Is context:annotation-config an alternative to @AutoWired?

查看:393
本文介绍了是上下文:注释-config来@Autowired替代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它是正确的,我可以把背景:注解配置在我的XML配置,它会自动注入bean类无需任何注释

因此​​,而不是使用这些注释类型:

 公共类邮差
{
    私人字符串名称;    @Autowired
    私人包裹包裹;    公共邮差(字符串名称)
    {
        this.name =名称;
    }    @Autowired
    公共无效setParcel(邮包包裹)
    {
        this.Parcel =包裹;
    }    @Autowired
    公共无效directionsToParcel(邮包包裹)
    {
        this.Parcel =包裹;
    }}

我只需要这样写:

 <豆...>
<豆的id =邮差级=邮差>
 <构造带参数的值=李四/>
< /豆>
<豆的id =包裹级=包裹/>
<背景:注解配置/>
< /豆>

然后我的邮差类看起来简单了很多,无需注释:

 公共类邮差
{
    私人字符串名称;    私人包裹包裹;    公共邮差(字符串名称)
    {
        this.name =名称;
    }
}


解决方案

默认情况下,Spring上下文会不注意 @Autowired 注释。为了处理他们的背景下需要有在上下文中注册的 AutowiredAnnotationBeanPostProcessor ​​豆。

<背景:注解配置/> 注册下列内容之一您(还有其他一些),所以你需要它(除非您注册 AutowiredAnnotationBeanPostProcessor ​​自己,这是完全有效)。

如果你不喜欢 @Autowired 在code,那么你就可以明确使用注入的XML属性<性> ,这只是移动杂波从一个地方到另一个地方。

如果您的上下文是的非常的简单,那么你可以使用自动装配隐,如所描述<一个href=\"http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-autowire\">here.从本质上讲,这告诉Spring通过属性名称或类型自动自动装配。这需要非常少的配置,但它很快就失去控制 - 这是自动的性质意味着它很难控制,并给你很少的灵活性。

@Autowired 真的是最好的选择,一般。

Is it correct that I can put context:annotation-config in my XML config and it will automatically inject the bean class without needing any annotations?

So instead of using these annotation types:

public class Mailman
{
    private String name;

    @Autowired
    private Parcel Parcel;

    public Mailman(String name)
    {
        this.name = name;
    }

    @Autowired
    public void setParcel(Parcel Parcel)
    {
        this.Parcel = Parcel;
    }

    @Autowired
    public void directionsToParcel(Parcel Parcel)
    {
        this.Parcel = Parcel;
    }

}

I would just need to write this:

<beans ... >
<bean id="mailMan" class="MailMan">
 <constructor-arg value="John Doe"/>
</bean>
<bean id="parcel" class="Parcel" />
<context:annotation-config />
</beans>

and then my MailMan class would look a lot simpler without the need for annotations:

public class Mailman
{
    private String name;

    private Parcel Parcel;

    public Mailman(String name)
    {
        this.name = name;
    }    
}

解决方案

By default, a Spring context will pay no attention to @Autowired annotations. In order to process them, the context needs to have a AutowiredAnnotationBeanPostProcessor bean registered in the context.

<context:annotation-config/> registers one of these for you (along with a few others), so you do need it (unless you register AutowiredAnnotationBeanPostProcessor yourself, which is perfectly valid).

If you don't like having @Autowired in your code, then you can explicitly inject properties in the XML using <property>, which just moves the clutter from one place to another.

If your context is extremely simple, then you can use implicit autowiring, as described here. Essentially, this tells Spring to autowire automatically by property name or type. This required very little configuration, but it very quickly gets out of control - it's automatic nature means it's hard to control, and gives you very little flexibility.

@Autowired really is the best option, in general.

这篇关于是上下文:注释-config来@Autowired替代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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