您的位置:首页 > 编程语言 > Java开发

SpringBoot学习(二),起步

2015-11-13 11:01 351 查看
        上次只是一个最基本的入门,这次稍微完善一下。

        首先是包结构:

       


        Starter是启动类,这个类要放在顶层包下,这样自动扫描的时候会扫描同级的其它包。

        然后在Starter这个启动类上需要加上3个注解

       

@Configuration
@EnableAutoConfiguration
@ComponentScan
        这三个注解表明通过这个类经行配置,也可以制定其它类进行配置,启动自动配置,并且从这个类所在包开始自动扫描,这样我们的@controller才能在不进行配置的情况下被识别。这3个注解还可以合并,用一个@SpringBootApplication替代,所以精简后就是这样子的:

       

@SpringBootApplication
public class Starter {

public static void main(String[] args) throws Exception {
SpringApplication.run(Starter.class, args);
}
}


        然后controller和service等就可以和传统spring一样使用了,区别是不需要配置文件哦。

       

@RestController
public class HelloWorldController {

@Resource
private HelloWorldService helloWorldService;

@RequestMapping("/")
String home() {
this.helloWorldService.home();
return "Hello World!";
}
}

@Service
public class HelloWorldService {

@Autowired
public void home() {
System.out.println("service==>home");
}
}


pom再贴一下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>org.lhc</groupId>
<artifactId>spbt</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.7.RELEASE</version>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>


转载请注明出处:http://blog.csdn.net/redstarofsleep

更多内容请关注微信公众号:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: