本文共 2453 字,大约阅读时间需要 8 分钟。
官网的界面简洁清新,导航很明确,进入projects
org.springframework spring-context 5.0.2.RELEASE
dependencies { compile 'org.springframework:spring-context:5.0.2.RELEASE'}
package hello;public interface MessageService { String getMessage();}
package hello;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;@Componentpublic class MessagePrinter { final private MessageService service; @Autowired public MessagePrinter(MessageService service) { this.service = service; } public void printMessage() { System.out.println(this.service.getMessage()); }}
package hello;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.*;@Configuration@ComponentScanpublic class Application { @Bean MessageService mockMessageService() { return new MessageService() { public String getMessage() { return "Hello World!"; } }; } public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(Application.class); MessagePrinter printer = context.getBean(MessagePrinter.class); printer.printMessage(); }}