package com.example.spring; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @ComponentScan(basePackages = "com.example.spring") public class App { public static void main(String[] args) { @SuppressWarnings("resource") ApplicationContext ctx = new AnnotationConfigApplicationContext(App.class); MathsUser mathsUser = ctx.getBean(MathsUser.class); mathsUser.doTask(); } // Another way public static void main2(String[] args) { @SuppressWarnings("resource") AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.scan("com.example.spring"); ctx.refresh(); MathsUser mathsUser = ctx.getBean(MathsUser.class); mathsUser.doTask(); } }