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

SpringBoot学习笔记

2016-07-28 00:00 411 查看

SpringBoot学习笔记

1.创建pom文件

<?xml version="1.0" encoding="UTF-8"?>
<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>io.spring.boot</groupId>
<artifactId>BootDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 引入Spring Boot支持 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

<!-- Spring Boot 打包器 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

2. 第一个SpringBoot程序

// 启动类
@Configuration
@EnableAutoConfiguration
public class SpringBootStarter {

public static void main(String[] args) throws Exception {
Object[] resources = {SpringBootStarter.class,UserController.class};
SpringApplication.run(resources, args);
}
}

// SpringBoot中的一个controller
@Controller
public class UserController {

private Logger logger = LoggerFactory.getLogger(getClass());

@RequestMapping("/")
@ResponseBody
String home() {
logger.info("hello spring boot");
return "Hello World!";
}
}

2.1 @RestController and @RequestMapping

@RestController and @RequestMapping 注解都属于SpringMVC的注解

@RestController 是标识当前Controller类输入RestFul形式,请求后返回值会按照json格式返回

@RequestMapping 是指定http请求访问路径

eg.

@RestController
public class RsController {

@RequestMapping("/hello")
public String hello(){
return "hello spring boot";
}
}

2.2 @EnableAutoConfiguration

这个注解告诉SpringBoot去“猜”我们如何配置Spring,这个要建立在添加的jar包,

3.构建代码

3.1 使用 default package

正常来说,在我们的代码结构中,尽量使用域名倒置,并且按照模块进行分包,不建议直接将类放到src根目录下。
eg.我们的域名是geek.com ,建议 包命名为 com.geek。

3.2 项目结构

com
+- example
+- myproject
+- Application.java
|
+- domain
|   +- Customer.java
|   +- CustomerRepository.java
|
+- service
|   +- CustomerService.java
|
+- web
+- CustomerController.java

官方文档中说在
Application.java
的位置,并且配置成
@SpringBootApplication
或者
@ComponentScan
,可以扫描所有目录

但是个人感觉,如果扫描所有类必然会导致,加载速度过慢,所以建议使用
@ComponentScan
并指定扫描的目录
eg.

@Configuration
@EnableAutoConfiguration
@ComponentScan("com.geek.controller,com.geek.service")
public class SpringBootStarter {

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

4.加载类

4.1 使用@ComponentScan加载

@Configuration
@EnableAutoConfiguration
@ComponentScan("com.geek.controller,com.geek.service")
public class SpringBootStarter {

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

4.2 使用@Import加载

@Configuration
@EnableAutoConfiguration
@Import(UserController.class)
public class SpringBootStarter {

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

这种方式不太好用,如果类比较多,那就要写特别多的class

4.3 使用@ImportResource加载xml方式

@Configuration
@EnableAutoConfiguration
@ImportResource("classpath:beans.xml")
public class SpringBootStarter {

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

<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> 
<bean class="com.geek.controller.UserController"></bean>

</beans>

官方件建议使用注解的方式加载

5.自定义banner

在SpringBoot启动时会出现下面的画面

.   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/  ___)| |_)| | | | | || (_| |  ) ) ) )
'  |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot ::   v1.3.6.RELEASE

看起来很6的样子,其实SpringBoot为我们提供了自定的banner显示

5.1自定义banner

首先我们要在resource目录下添加一个banner.txt

在文档中输入显示的字符画即可

5.2 关闭banner

public static void main(String[] args) {

SpringApplication app = new SpringApplication(MySpringConfiguration.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: