开发者

How to enable @Required?

How to enable the @Required annotation in Java (Spring 3.1) ? Not in a xml, but in through Java. Also under which annotation I put this enabling? Under @Feature (in @FutureConfiguration or @Bean (in @Configuration) ?

Edit:

    @Feature
    public MvcAnnotationDriven annotationDriven(ConversionService conversionService) {
        return new MvcAnnotationDriven().conversionService(conversionService)
                .argumentR开发者_如何学Goesolvers(new CustomArgumentResolver());
    }

Does this enables all annotations?


@anubhava's answer works, but he's referenced the Spring 2.0 manual, which is 5 years old.

In XML config, Spring 3.x has a more elegant approach: <context:annotation-config/>. This also enabled a whole other bunch of features that you'll probably want, whereas RequiredAnnotationBeanPostProcessor only enables a few.

See Spring 3.x manual.

If you're using @Bean-style config, then annotations like @Required should already be enabled, since that's how @Bean works. However, it's possible that this is a bug - Spring 3.1 is still in early beta, and big chunks of it are likely to be broken.

Unless you really know what you're doing, I strongly recommend sticking to 3.0.x.


From the Spring manual:

There is one last little (small, tiny) piece of Spring configuration that is required to actually 'switch on' this behavior. Simply annotating the 'setter' properties of your classes is not enough to get this behavior. You need to enable a component that is aware of the @Required annotation and that can process it appropriately.

This component is the RequiredAnnotationBeanPostProcessor class. This is a special BeanPostProcessor implementation that is @Required-aware and actually provides the 'blow up if this required property has not been set' logic. It is very easy to configure; simply drop the following bean definition into your Spring XML configuration.

<bean class=
"org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>

Please check: http://static.springsource.org/spring/docs/2.0.x/reference/metadata.html


Use AnnotationConfigApplicationContext if you don't want to use XML:

Standalone application context, accepting annotated classes as input - in particular @Configuration-annotated classes, but also plain @Components and JSR-330 compliant classes using javax.inject annotations. Allows for registering classes one by one (register(java.lang.Class...)) as well as for classpath scanning (scan(java.lang.String...)).

In case of multiple Configuration classes, Bean methods defined in later classes will override those defined in earlier classes. This can be leveraged to deliberately override certain bean definitions via an extra Configuration class.

Sample Code:

ConfigurableApplicationContext applicationContext =
new AnnotationConfigApplicationContext(
    "com.mycompany.package1",
    "com.mycompany.package2",
    "com.mycompany.package3"
    // etc.
);
applicationContext.refresh();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜