public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AnnotationConfig.class); //1
UseFunctionService useFunctionService = applicationContext.getBean(UseFunctionService.class); //2
System.out.println(useFunctionService.SayHello("World"));
applicationContext.close();
}
}
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.supremepole.springconfigbeanannotation")
public class AnnotationConfig {
}
import org.springframework.stereotype.Service;
@Service
public class FunctionService {
public String sayHello(String word) {
return "Hello " + word + "!";
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UseFunctionService {
@Autowired
FunctionService functionService;
public String SayHello(String word) {
return functionService.sayHello(word);
}
}