Example one:
Diconfig file:
Package Di;import Org.springframework.context.annotation.componentscan;import Org.springframework.context.annotation.configuration;//declares that the current class is a configuration class @configuration//all @service under the Automatic scan package, @Component , @Repository and @controller are registered as beans; @ComponentScan ("Di") public class Diconfig {}
Dfunctionservice file:
Package Di;import org.springframework.stereotype.service;//Injection: The current class is a Bean@servicepublic class for spring management Dfunctionservice {public string SayHello (string word) {return "Hello," + Word + "!";}}
Usefunctionservice file:
Package Di;import Org.springframework.beans.factory.annotation.autowired;import org.springframework.stereotype.service;//Injection: The current class is a spring-managed Bean@servicepublic class Usefunctionservice {// Inject the entity bean of the Dfunctionservice class into Usefunctionservice and let Usefunctionservice have dfunctionservice function//equivalent annotations: @[email Protected][email protected] @AutowiredDFunctionService functionservice;public string SayHello (string word) {return Functionservice.sayhello (Word);}}
Main file:
Package Di;import Org.springframework.context.annotation.annotationconfigapplicationcontext;public class Main { public static void Main (String args[]) {Annotationconfigapplicationcontext context = new Annotationconfigapplicationcontext (Diconfig.class); Usefunctionservice Userfunctionservice = Context.getbean (Usefunctionservice.class); System.out.print (Userfunctionservice.sayhello ("word")); Context.close ();}}
Run Main.java with the result of Hello Word
Spring Boot: General injection Instructions (i)