Close

Spring Framework - @ComponentScan Examples

Spring Framework 

import com.logicbig.example.bean.MyPrototypeBean;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.logicbig.example.bean")
public class AppConfig {

public static void main(String... strings) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(AppConfig.class);
System.out.println("Spring container started and is ready");
MyPrototypeBean bean = context.getBean(MyPrototypeBean.class);
bean.doSomething();
}
}
Original Post




@Configuration
@ComponentScan(basePackageClasses = MySingletonBean.class)
public class AppConfig {

@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public MyPrototypeBean prototypeBean() {
return new MyPrototypeBean();
}

public static void main(String[] args) throws InterruptedException {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(AppConfig.class);
MySingletonBean bean = context.getBean(MySingletonBean.class);
bean.showMessage();
Thread.sleep(1000);

bean = context.getBean(MySingletonBean.class);
bean.showMessage();
}
}
Original Post




@Configuration
@ComponentScan
public class AppRunner {

public static void main(String... strings) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppRunner.class);
OrderServiceClient bean = context.getBean(OrderServiceClient.class);
bean.showPendingOrderDetails();
}
}
Original Post




This example shows that classes annotated with @Component can register new beans using @Bean annotation on their methods, but such methods will not be CGLIB enhance proxies.

import org.springframework.context.annotation.*;
import org.springframework.stereotype.Component;

@Configuration
@ComponentScan(basePackageClasses = ScanFactoryComponentExample.class, useDefaultFilters = false,
includeFilters = {@ComponentScan.Filter(
type = FilterType.ASSIGNABLE_TYPE,
value = ScanFactoryComponentExample.TestBean.class)})
public class ScanFactoryComponentExample {

public static void main (String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(
ScanFactoryComponentExample.class);
AnotherTestBean bean = context.getBean(AnotherTestBean.class);
System.out.println(bean);

//accessing multiple times
bean = context.getBean(AnotherTestBean.class);
System.out.println(bean);

/*this will return a new instance because @Component methods are not
* CGLIB proxied*/
TestBean testBean = context.getBean(TestBean.class);
System.out.println(testBean.anotherTestBean());
}

@Component
public static class TestBean {
@Bean
public AnotherTestBean anotherTestBean () {
return new AnotherTestBean();
}
}

public static class AnotherTestBean {
}
}

Output

com.logicbig.example.ScanFactoryComponentExample$AnotherTestBean@26c86c18
com.logicbig.example.ScanFactoryComponentExample$AnotherTestBean@26c86c18
com.logicbig.example.ScanFactoryComponentExample$AnotherTestBean@47d5e379
Original Post

This example shows that classes annotated with @Configuration will be scanned as well, if they are in scan path.

import org.springframework.context.annotation.*;

@Configuration
@ComponentScan(basePackageClasses = ScanConfigurationExample.class, useDefaultFilters = false,
includeFilters = {@ComponentScan.Filter(
type = FilterType.ASSIGNABLE_TYPE,
value = ScanConfigurationExample.TestBean.class)})
public class ScanConfigurationExample {

public static void main (String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(
ScanConfigurationExample.class);
AnotherTestBean bean = context.getBean(AnotherTestBean.class);
System.out.println(bean);

//accessing multiple times
bean = context.getBean(AnotherTestBean.class);
System.out.println(bean);

/*this will return same instance because @Configuration classes even in scan path will be
* CGLIB proxied*/
TestBean testBean = context.getBean(TestBean.class);
System.out.println(testBean.anotherTestBean());
}

@Configuration
public static class TestBean {
@Bean
public AnotherTestBean anotherTestBean () {
return new AnotherTestBean();
}
}

public static class AnotherTestBean {
}
}

Output

com.logicbig.example.ScanConfigurationExample$AnotherTestBean@703b3d40
com.logicbig.example.ScanConfigurationExample$AnotherTestBean@703b3d40
com.logicbig.example.ScanConfigurationExample$AnotherTestBean@703b3d40
Original Post




@Configuration
@ComponentScan({"com.logicbig.example.scan"})
public class ConstBasedDIWithScan {

@Bean
public OrderService orderService() {
return new OrderService();
}

public static void main(String... strings) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
ConstBasedDIWithScan.class);
OrderServiceClient bean = context.getBean(OrderServiceClient.class);
bean.showPendingOrderDetails();
}

@Component
public static class OrderServiceClient {

private OrderService orderService;

@Autowired
OrderServiceClient(OrderService orderService) {
this.orderService = orderService;
}

public void showPendingOrderDetails() {
System.out.println(orderService.getOrderDetails("500"));
}
}
}
Original Post




@Configuration
@ComponentScan
public class AppRunner {
private Greeter greeter;

public AppRunner(Greeter greeter) {
this.greeter = greeter;
}

public static void main(String... strings) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(AppRunner.class);

AppRunner appRunner = context.getBean(AppRunner.class);
appRunner.greeter.greet("Joe");
}
}
Original Post




See Also