spring boot 依赖注入

来源:互联网 发布:php新闻发布系统登录 编辑:程序博客网 时间:2024/05/29 08:35

一.创建maven工程,编写pom.xml文件

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.4.1.RELEASE</version></parent><properties><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><!-- 添加依赖的jar包 --><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies>
二.创建Application.java程序入口类

//@ComponentScan//自动扫描bean//@EnableAutoConfiguration @SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScanpublic class Application {public static void main(String[] args) throws Exception {// 启动SpringApplication.run(Application.class, args);}}
三.创建SimpleService.java

@Servicepublic class SimpleService {public String say(){return "hello world";}}
四.创建SimpleController.java在其中注入SimpleService

@RestControllerpublic class SimpleController {@Autowired // 自动注入private SimpleService service;@RequestMapping("/")public String home() {return service.say();}}

五.启动应用,打开浏览器输入localhost:8080就可以访问到hello world


源码链接https://github.com/wangjianyangchn/SpringBootProject




0 1
原创粉丝点击