Spring Boot初学,进来了解了解吧
MVN执行流程
mvn compile
mvn test
测试是否成功
mvn package
mvn install
打jar包
Spring Boot
注解
@SpringBootApplication - SpringBoot核心注解 - 包含以下注解
- @SpringBootConfiguration
- @Configuration: 代表的是这是一个spring的配置文件,类似applicationContext.xml
- @EnableAutoConfiguration
- @AutoConfigurationPackage : 代表的是springboot会自动扫描启动类所在包,以及其子包
- @Import({AutoConfigurationImportSelector.class}) :代表springboot帮你自动导入了很多依赖关系
- @ComponentScan : 扫描组件,将组件注入给spring容器
- @SpringBootConfiguration
@ImportResource(locations=”classpath:applicationContext-service.xml”)
spring下的注解,扫描resources文件下的xml文件,让SpringBoot认识xml文件,然后在需要应用xml配置的类中使用@Service、@Controller等等标记即可通过Bean获取值
具体实现类
1
2
3
4
5
6
7
public class UserServiceImpl implements UserService {
public void addUser() {
System.out.println("---UserServiceImpl.addUser()---");
}
}applicationContext-service.xml配置
1
2
3
4
5
6
7
<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.xsd">
<bean id="userServiceImpl" class="com.ming.demo_01.service.UserServiceImpl"/>
</beans>
@Configuration
与上一注解做类似,可以通过下列方式获得Bean
1
2
3
4
5
6
7
8
9
public class ServiceConfig {
"userServiceImpl") (
public UserServiceImpl userServiceImpl(){
UserServiceImpl userServiceimpl = new UserServiceImpl();
return userServiceimpl;
}
}两者相同的测试就能通过,选其一
1
2
3
4
5
6
7
private ApplicationContext context;
public void testService() {
UserService userService = (UserService) context.getBean("userServiceImpl");
userService.addUser();
}
@PropertySource(value={“classpath:user.properties”})
Spring MVC 通过xml配置文件获取Bean对象,SpringBoot可以通过这个注解加载资源文件获取对象
@PropertySource : 进行分文件管理,是springboot识别PropertySource中value指定的文件,只能引入后缀为.properties的文件
By default the @PropertySource annotation isn’t usable with YAML files.
@Component,ConfigurationProperties
Component,表示该类是一个组件,才可以通过@Autowired自动植入
ConfigurationProperties,表示增加一个前缀,之后就可以通过前缀给User属性赋值,也可以不通过前缀,通过@Value方式赋值
User类
1
2
3
4
5"classpath:user.properties"}) (value={
"user") (prefix =
public class User {
}user.properties
1
2
3
4
5
6
7
8
9
10
11user.user-name=arvin
user.user-age=18
user.female=true
# List属性赋值方式
user.my-list={value1,value2}
# Map属性赋值方式
user.map.key1 = value1
user.map.key2 = value2
# 一用其他对象赋值方式
user.orders.orderId = 001000
user.orders.orderName=epochong通过@Value
1
2
3
4
5
6//通过properties文件复制
"${abc.def}") (
private String userName;
//直接赋值
"#{10*20}")//SpEL (
private int userAge;user.properties
1
abc.def=CaiXuKun
测试
1
2
3
4
5
6
7// 自动植入赋值
private User user;
public void contextLoadsUser() {
System.out.println(user);
}
配置文件
application.yml - SpringBoot默认配置文件,名称不能改变
该文件和properties文件都可给对象赋值,下面了解yml语法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24# application.yml 该文件是springboot的【默认配置文件】 学习语法
server:
port: 8081
#激活profile
#有点微服务的意思,指定application-pro.yml这个文件的配置
spring:
profiles:
active: pro
#给POJO类的属性进行赋值操作(松散语法)
#对其格式,代表所属级别,其中包含List和Map等特殊属性的赋值
user:
userName: arvin
userAge: 20
female: false
myList:
- listvalue1
- listvalue2
map:
key1: mapValue1
key2: mapValue2
key3: mapValue3
orders:
orderId: 00101
orderName: OrderNameSpringBoot配置文件的优先级别
- 项目根目录
**/*.file
- 项目根目录
*.file*
resources/**/*.file
- 在
resources/*.file
- 项目根目录