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);
MyBean bean = context.getBean(MyBean.class);
System.out.println(bean);

//accessing multiple times
bean = context.getBean(MyBean.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.myBean());
}

@Component
static class TestBean {
@Bean
public MyBean myBean () {
return new MyBean();
}
}
}

Output

com.logicbig.example.MyBean@1c95649a
com.logicbig.example.MyBean@1c95649a
com.logicbig.example.MyBean@373137a0
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);
MyBean bean = context.getBean(MyBean.class);
System.out.println(bean);

//accessing multiple times
bean = context.getBean(MyBean.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.myBean());
}

@Configuration
static class TestBean {
@Bean
public MyBean myBean () {
return new MyBean();
}
}
}

Output

com.logicbig.example.MyBean@28e5716a
com.logicbig.example.MyBean@28e5716a
com.logicbig.example.MyBean@28e5716a
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
import com.logicbig.example.client.Buyer;
import com.logicbig.example.client.Wholesaler;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = {"com.logicbig.example.client","com.logicbig.example.service"})
public class OnlineOrderApp {

public static void main(String... strings) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(OnlineOrderApp.class);
System.out.println("-- Spring container started and is ready --");
Buyer buyer = context.getBean(Buyer.class);
buyer.buySomething();

Wholesaler wholesaler = context.getBean(Wholesaler.class);
wholesaler.buySomethingInBulk();
}

}
Original Post




import com.logicbig.example.client.Consumer;
import com.logicbig.example.client.OnlineOrderClient;
import com.logicbig.example.client.Wholesaler;
import com.logicbig.example.service.OnlineOrderService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackageClasses = {OnlineOrderClient.class, OnlineOrderService.class})
public class OnlineOrderApp {

public static void main(String... strings) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(OnlineOrderApp.class);
System.out.println("-- Spring container started and is ready --");
Consumer consumer = context.getBean(Consumer.class);
consumer.buySomething();

Wholesaler wholesaler = context.getBean(Wholesaler.class);
wholesaler.buySomethingInBulk();
}
}
Original Post




import com.logicbig.example.client.RetailBuyer;
import com.logicbig.example.client.Wholesaler;
import com.logicbig.example.service.WholeSaleOrderService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;

@Configuration
@ComponentScan(basePackages = "com.logicbig.example.client;com.logicbig.example.service",
excludeFilters = @ComponentScan.Filter(
type = FilterType.ASSIGNABLE_TYPE,
classes = {WholeSaleOrderService.class, Wholesaler.class})

)
public class OnlineOrderApp {

public static void main(String... strings) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(OnlineOrderApp.class);
System.out.println("-- Spring container started and is ready --");
RetailBuyer retailBuyer = context.getBean(RetailBuyer.class);
retailBuyer.buySomething();

boolean wholeSaleOrderServiceRegistered =
context.containsBean("wholeSaleOrderService");
System.out.println("wholeSaleOrderService registered: "+ wholeSaleOrderServiceRegistered);
}
}
Original Post
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;

@Configuration
@ComponentScan(includeFilters = @ComponentScan.Filter(
type = FilterType.ASSIGNABLE_TYPE, classes = {OrderService.class}))
public class OnlineOrderApp {

public static void main(String... strings) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(OnlineOrderApp.class);
System.out.println("-- Spring container started and is ready --");
Buyer buyer = context.getBean(Buyer.class);
buyer.buySomething();
}
}
Original Post




import com.logicbig.example.client.RetailBuyer;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;

@Configuration
@ComponentScan(basePackages = "com.logicbig.example.client;com.logicbig.example.service",
excludeFilters = @ComponentScan.Filter(
type = FilterType.ANNOTATION,
classes = ExcludeFromScan.class
)

)
public class OnlineOrderApp {

public static void main(String... strings) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(OnlineOrderApp.class);
System.out.println("-- Spring container started and is ready --");
RetailBuyer retailBuyer = context.getBean(RetailBuyer.class);
retailBuyer.buySomething();

boolean wholeSaleOrderServiceRegistered =
context.containsBean("wholeSaleOrderService");
System.out.println("WholeSaleOrderService registered: " + wholeSaleOrderServiceRegistered);


boolean wholesaler =
context.containsBean("wholesaler");
System.out.println("Wholesaler registered: " + wholesaler);
}
}
Original Post




import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;

@Configuration
@ComponentScan(includeFilters = @ComponentScan.Filter(
type = FilterType.ANNOTATION, classes = {IncludeInScan.class}))
public class OnlineOrderApp {

public static void main(String... strings) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(OnlineOrderApp.class);
System.out.println("-- Spring container started and is ready --");
Buyer buyer = context.getBean(Buyer.class);
buyer.buySomething();
}
}
Original Post




See Also