In this Spring auto component scanning tutorial, you learn about how to make Spring auto scan your components. In this article, we show you how to do component filter in auto scanning process.
Normally you declare all the beans or components in XML bean configuration file, so that Spring container can detect and register your beans or components. Actually, Spring is able to auto scan, detect and instantiate your beans from pre-defined project package, no more tedious beans declaration in in XML file. Following is a simple Spring project, including a customer service and dao layer. Let’s explore the different between declare components manually and auto components scanning in Spring.
In Spring framework, when your class contains multiple constructors with same number of arguments, it will always cause the constructor injection argument type ambiguities issue. To fix it, you should always specify the exact data type for constructor, via type attribute. Note: It’s always a good practice to explicitly declared the data type for each constructor argument, to avoid constructor injection type ambiguities issue above.
Since Spring 3.0, Spring supports for the standard JSR 330: Dependency Injection for Java. In Spring 3 application, you can uses standard: 1 – @Inject instead of Spring’s @Autowired to inject a bean, 2 – @Named instead of Spring’s @Component to declare a bean. Those JSR-330 standard annotations are scanned and retrieved the same way as Spring annotations, the integration just happened automatically, as long as the following jar in your classpath.
In Spring frameowork, Dependency Injection (DI) design pattern is used to define the object dependencies between each other. It exits in two major types : 1 – Setter Injection – it will injects the dependency via a setter method, 2 – Constructor Injection – it will injects the dependency via a constructor.
Follow the Stackoverflow question about: Spring/Java error: namespace element ‘annotation-config’ … on JDK 1.5 and higher to find the solution for fixing the error: java.lang.IllegalStateException: Context namespace element ‘annotation-config’ and its parser class [org.springframework.context.annotation.AnnotationConfigBeanDefinitionParser] are only available on JDK 1.5 and higher
In Spring, @Qualifier means, which bean is qualify to autowired on a field. When you have multiple similar beans are declared in bean configuration file, will Spring know which particular bean should autowired? To fix above problem, you need @Quanlifier to tell Spring about which bean should autowired.
In last Spring auto-wiring in XML example, it will autowired the matched property of any bean in current Spring container. In most cases, you may need autowired property in a particular bean only. In Spring, you can use @Autowired annotation to auto wire bean on the setter method, constructor or a field. Moreover, it can autowired property in a particular bean. Note: the @Autowired annotation is auto wire the bean by matching data type.