From f06630dc7e7d5fb435591e86fde6ef8b6c654a38 Mon Sep 17 00:00:00 2001 From: Cheng Date: Wed, 27 Dec 2017 15:42:12 +0800 Subject: [PATCH] evolve(annotation): java source code as configuration --- .../springdemo/JavaConfigDemoApp.java | 32 +++++++++++++++++++ .../com/luv2code/springdemo/SportConfig.java | 11 +++++++ 2 files changed, 43 insertions(+) create mode 100644 spring-demo-annotations/src/com/luv2code/springdemo/JavaConfigDemoApp.java create mode 100644 spring-demo-annotations/src/com/luv2code/springdemo/SportConfig.java diff --git a/spring-demo-annotations/src/com/luv2code/springdemo/JavaConfigDemoApp.java b/spring-demo-annotations/src/com/luv2code/springdemo/JavaConfigDemoApp.java new file mode 100644 index 0000000..3b241c8 --- /dev/null +++ b/spring-demo-annotations/src/com/luv2code/springdemo/JavaConfigDemoApp.java @@ -0,0 +1,32 @@ +package com.luv2code.springdemo; + +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +public class JavaConfigDemoApp { + + public static void main(String[] args) { + + // read spring config java class + AnnotationConfigApplicationContext ctx = + new AnnotationConfigApplicationContext(SportConfig.class); + + // get the bean from spring container + TennisCoach theCoach = ctx.getBean("tennisCoach", TennisCoach.class); + + // call a method on the bean + System.out.println(theCoach.getDailyWorkout()); + + // call a method with dependency injection + System.out.println(theCoach.getDailyFortune()); + + // call getter methods with literal value injection + System.out.println(theCoach.getEmail()); + System.out.println(theCoach.getTeam()); + + + // close the context + ctx.close(); + + } + +} diff --git a/spring-demo-annotations/src/com/luv2code/springdemo/SportConfig.java b/spring-demo-annotations/src/com/luv2code/springdemo/SportConfig.java new file mode 100644 index 0000000..9b50a38 --- /dev/null +++ b/spring-demo-annotations/src/com/luv2code/springdemo/SportConfig.java @@ -0,0 +1,11 @@ +package com.luv2code.springdemo; + + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ComponentScan("com.luv2code.springdemo") +public class SportConfig { + +}