Spring - @Component
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.16</version>
</dependency>
Sample Program for @Component using 'Spring Framework'
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.ah.di"/>
</beans>
<context:component-scan base-package="com.ah.di"/>
package com.ah.di;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.
ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("beans_di.xml");
/*for(String bean :
context.getBeanDefinitionNames()){
System.out.println(bean);
}*/ var carObj = context.getBean("car",Car.class); carObj.displayCarInfo(); } }
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 org.springframework.stereotype.Component;
@Component public class Engine {
public void testEngine(){ System.out.println("Engine is working"); } }