Just like in an ordinary bean class, we can also implicitly inject dependency in @Configuration classes (only in Spring 4.3 and later).
@Configuration
package com.logicbig.example; import org.springframework.stereotype.Component; @Component public class Greeter { public void greet(String name){ System.out.println("hi there, "+name); } }
package com.logicbig.example; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @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"); } }
hi there, Joe
Dependencies and Technologies Used:
Version compatibilities of spring-context with this example:
Versions in green have been tested.