Spring - @ComponentScan & @Configuration
In this Part of Spring Framework we will avoid using Spring configuration xml file, instead we will maintain configuration at java side by using the '@Configuration' annotation.
package com.ah.di.samp;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "com.ah.di")
public class AppConf {
}
package com.ah.di;
import org.springframework.stereotype.Component;
@Component public class Car {
public void displayCarInfo(){ System.out.println("car class display method"); } }
package com.ah.di;
import com.ah.di.samp.AppConf; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.
AnnotationConfigApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context =
new AnnotationConfigApplicationContext
(AppConf.class);
var carObj = context.getBean("car",Car.class); carObj.displayCarInfo();
//displyAppContextBeanInfo(context);
} public static void displyAppContextBeanInfo
(ApplicationContext context ){
for(String bean : context.
getBeanDefinitionNames()){
System.out.println(bean);
} } }
Watch Demo
No comments:
Post a Comment