目录
目录
(2)配置数据源信息---application.properties
一、什么是springboot
springboot可以帮你简化spring的搭建,并且快速创建一个spring的应用程序。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置
二、springboot有什么特点
(1)可以创建独立的Spring应用程序,并且基于其Maven或Gradle插件,可以创建可执行的JARs和WARs;
(2)内嵌Tomcat或Jetty等Servlet容器;
(3)提供自动配置的“starter”项目对象模型(POMS)以简化Maven配置;
(4)尽可能自动配置Spring容器;
(5)提供准备好的特性,如指标、健康检查和外部化配置;
(6)绝对没有代码生成,不需要XML配置。
三、创建springboot工程
要求
1.JDK必须为1.8以上
2.spring的jar必须5.0以上
3.maven必须3.3以上
配置idea
创建一个controller 类
启动springboot工程并浏览器访问:
四、springboot配置文件
第一种: properties属性文件
# 修改springboot中tomcat端口号.
server.port=8888
第二种: yml文件
server:
port: 666
不管是哪种,他们的名字必须以application开始。
如果两个配置文件同时存在,而且有些内容一样。按照properties的优先级高。如果有些不一样,两个配置文件不一样的会合并在一起
五、读取springboot配置文件中的内容
#自定义的配置信息
student.name=ldh
student.age=15
student.hobby[0]=sing
student.hobby[1]=swing
@Data
@Component //该类对象的创建和销毁都有spring容器来管理
@ConfigurationProperties(prefix = "student") //读取springboot中的配置内容
public class Student {
private String name;
private Integer age;
private String[] hobby;
}
@Autowired //spring容器帮你注入该对象
private Student student;
@GetMapping("/index")
public Student index(){
return student;
}
六、Profiles文件的介绍
七、springboot注册web三大组件
Servlet和Filter以及Linstener监听器
(1)注册servlert
创建一个配置类
public class MyServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("~~~~~~~~~~");
}
}
@CacheConfig//该类为配置类 xml文件
public class MyConfig {
@Bean//理解为配置文件中<bean >
public ServletRegistrationBean<Servlet> registrationBean() {
//创建一个Servlet注册器
ServletRegistrationBean<Servlet> registrationBean =new ServletRegistrationBean<>();
registrationBean.setName("my");
registrationBean.setServlet(new MyServlet());
registrationBean.addUrlMappings("/my");
return registrationBean;