Engineering Full Stack Apps with Java and JavaScript
Create a simple spring program that uses Component and ComponentScan Annotations.
We will create a simple class, configure it and test it. You can configure a Spring application through xml, or Java configuration or annotations along with xml or java configuration. In this example we will use @Component and @ComponentScan annotations along with Java configuration. Note that these annotations can also be used along with xml configuration and we will see how to do that in the end.
@Component can be used over a class to tell spring to consider it as a bean component. @ComponentScan annotation can be used along with @Configuration classes to specify the packages that need to be scanned for components.
If you are using Maven, just copy paste right maven dependency into your pom file and all other required dependencies will be added automatically, as they are the transitive dependencies for below dependency.
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
Please use the latest version as available in the Spring website @ http://projects.spring.io/spring-framework.
Bean component class
We will use a similar class as in prevoius examples, but with @Component annotation.
JJWriterComp.java
package com.javajee.spring;
import org.springframework.stereotype.Component;
@Component("writer")
public class JJWriterComp {
public void write() {
System.out.println("Default Writer");
}
}
Config class
We need to have a class that is annotated with @Configuration, with @ComponentScan annotation as:
DemoConfig.java
package com.javajee.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;@Configuration
@ComponentScan(basePackages="com.javajee.spring")
public class DemoConfig {}
Instead of using @ComponentScan annotation, we can also use <context:component> element in an spring config xml.
Test Class
If we are using the Java configuration class, you can use the below class to test.
JJWriterCompMain.java
package com.javajee.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class JJWriterCompMain {
public static void main(String[] args) {ApplicationContext context = new AnnotationConfigApplicationContext(DemoConfig.class);
JJWriterComp writer = context.getBean("writer", JJWriterComp.class);
writer.write();}
}
Auto bean naming with @Component
Generally, if you don't specify a name as in this case (e.g. writer) for @Component, the bean name will be automatically generated. This will be the same name as that of the class, but with first letter made small. MyClass becomes myClass.
However, I saw a strange behaviour. If my class name start with two capital letters and I annotate with @Component, the auto generated name starts with capital letter. Else it starts with small letter.
JJwriterComp gives JJwriterComp
JaaJwriterComp gives jaaJwriterComp
I could not find any reference for this difference in any documentation.