August 14, 2022

Spring Auto scanning components

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.

Spring Auto scanning components Read More

Constructor injection type ambiguities 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.

Constructor injection type ambiguities in Spring Read More

Spring 3 and JSR-330 @Inject and @Named example

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.

Spring 3 and JSR-330 @Inject and @Named example Read More

[org.springframework.context.annotation.AnnotationConfigBeanDefinitionParser] are only available on JDK 1.5 and higher

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

[org.springframework.context.annotation.AnnotationConfigBeanDefinitionParser] are only available on JDK 1.5 and higher Read More

Spring Auto-Wiring Beans with @Autowired annotation

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.

Spring Auto-Wiring Beans with @Autowired annotation Read More