Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring @Configuration and <context:component-scan />

Tags:

java

spring

I have a scenario configuring Spring Security on embedded Jetty which seems to be somewhat solved if I make use of JavaConfig to configure the Jetty server.

As a result, it's looking like JavaConfig rather than XML might be the better option for large chunks of the project. However, there are some niceties in the XML namespaces, like <context:component-scan /> which aren't readily available in a @Configuration setting.

I have discovered that ApplicationContextAware is honored for @Configuration classes, so the following is possible

@Configuration
public class FooConfig implements ApplicationContextAware {
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        ((AnnotationConfigApplicationContext) applicationContext).scan("org.example");
    }
}

The alternative, which is documented, is to have the @Configuration class use an @ImportResource annotation and pull in an existing XML file:

@Configuration
@ImportResource("applicationContext-withComponentScan.xml")
public class BarConfig {}

I guess the question is "Is it bad form to abuse ApplicationContextAware in this way, or is it really not abuse"? Something just feels oddly dirty about the approach so I'd not be surprised if the Spring guys had covered this in some way or another that I've not spotted.

For the interested, the problem relates to scanning a Jersey setup with @Resource and @Provider classes that I'd rather not have to manually manage entries in a class/XML configuration.

like image 757
ptomli Avatar asked Jul 13 '11 18:07

ptomli


People also ask

How do I use context component scan in Spring?

Auto Components Scanning Put this “ context:component ” in bean configuration file, it means, enable auto scanning feature in Spring. The base-package is indicate where are your components stored, Spring will scan this folder and find out the bean (annotated with @Component) and register it in Spring container.

Can we use @component and @configuration together?

Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning. A @Configuration is also a @Component, but a @Component cannot act like a @Configuration.

What is the difference between @configuration and @component in Spring?

The main difference between these annotations is that @ComponentScan scans for Spring components while @EnableAutoConfiguration is used for auto-configuring beans present in the classpath in Spring Boot applications.

What does context component scan do?

context:component- scan element helps to identify the java classes which can be registered as a spring beans.


2 Answers

Now that Spring 3.1 is ready and out, you can safely use @ComponentScan if you are on Spring 3.1. It's not only for Spring MVC as one of the outdated answers mentions. You can use it as follows:

@Configuration
@ComponentScan({"com.foo.bar", "org.foo.bar"})
public class AppConfig{ /** config code */ }

Here is the documentation http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/context/annotation/ComponentScan.html

like image 83
user1713413 Avatar answered Nov 08 '22 03:11

user1713413


Is it bad form to abuse ApplicationContextAware in this way, or is it really not abuse

Yes, this is bad form. If you're going to fetch things out of the context manually, you may as well not bother with dependency injection in the first place.

However, your second option (@ImportResource("applicationContext-withComponentScan.xml")) is a good one - this is current best practice when you want to use these XML macros in combination with annotation-style config.

A third option is to use the current milestone build of Spring 3.1, which adds a way of doing these things all in Java, using @Feature. This is not yet production-ready, though.

like image 30
skaffman Avatar answered Nov 08 '22 04:11

skaffman