您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Springboot入门使用实例分析

2024/3/10 9:23:47发布28次查看
入门springboot项目创建在idea中创建即可。
注意点:
1、所有文件都需要放在 :
application文件的同级或下级目录中
2、application.properties 为 spring-boot 项目主核心配置文件,且只能有一个核心配置文件。
3、多环境下的核心配置文件的使用, 文件名必须以 application- 开头!
  application-xxx.properties
(1)开发环境
# 开发环境配置文件server.port=9000server.servlet.context-path=/
(2)测试
# 测试环境配置文件
(3)生产环境
# 生产环境配置文件server.port=7000
在主核心配置文件中激活我们自定义的配置文件:
#激活我们编写的application-xxx.properties配置文件spring.profiles.active=dev
4、@value 注解
spring-boot核心配置文件 自定义的配置属性,如何获取
下边方式只能一个一个属性获取!
比如:在application.properties文件中自定义了一个配置 website=http://www.baidu.com
在项目中获取到这个自定义的配置:
使用注解 @value(${website}) 
也可以写一个默认值,如果配置项没有,会使用默认值@value(${website: 默认值}) 
package com.lxc.sprint_boot_01.web; import org.springframework.beans.factory.annotation.value;import org.springframework.stereotype.controller;import org.springframework.web.bind.annotation.requestmapping;import org.springframework.web.bind.annotation.responsebody; import javax.management.valueexp;import javax.print.docflavor; // 声明控制层@controllerpublic class indexcontroller {    @value(${website:values})    private string name; // 此时website值会赋给name属性     @requestmapping(value = /self)    @responsebody    public string self() {        return name;    }}
5、@component 和 @configurationproperties(prefix=xxx) 注解
spring-boot核心配置文件 将我们自定义的配置属性,映射为一个对象(获取的是一个对象),使用这种方式的前提:配置文件中的属性必须要写前缀!
application.properties文件
# 属性前边必须要有前缀,我这里前缀是useruser.name=lxcuser.password=123456
config -> user.java文件
package com.lxc.sprint_boot_01.config; import org.springframework.boot.context.properties.configurationproperties;import org.springframework.stereotype.component; @component // 将此类交给spring容器管理@configurationproperties(prefix = user) // 配置属性注解,参数前缀必须有值,值为我们定义的前缀// 配置完上边的两个注解,下边把配置文件中的属性映射到下边类中去public class user {    private string username;    private string password;     public string getusername() {        return username;    }     public void setusername(string username) {        this.username = username;    }     public string getpassword() {        return password;    }     public void setpassword(string password) {        this.password = password;    }}
调用属性
package com.lxc.sprint_boot_01.web; import com.lxc.sprint_boot_01.config.user;import org.springframework.beans.factory.annotation.autowired;import org.springframework.beans.factory.annotation.value;import org.springframework.stereotype.controller;import org.springframework.web.bind.annotation.requestmapping;import org.springframework.web.bind.annotation.responsebody; import javax.management.valueexp;import javax.print.docflavor;import java.util.hashmap;import java.util.map; // 声明控制层@controllerpublic class indexcontroller {    @autowired // @autowired 把user类注入进来    private user user;     @requestmapping(value = /many)    @responsebody    public string many() {        return user为:+user.getusername() + ,密码为:+user.getpassword();    } }
6、加上@configurationproperties注解,会出现上边红色警告,想解决此问题需要加一个依赖包:
<dependency>    <groupid>org.springframework.boot</groupid>    <artifactid>spring-boot-configuration-processor</artifactid></dependency>
7、如果在application.properties中有中文,会出现乱码,在idea中解决中文乱码的问题:
8、在配置文件中属性的键值对不能有空格,否则解析会有问题!
9、spring-boo集成jsp
首先在main文件夹下创建 webapp文件夹,然后 点击 file -> project structure -> modules  如下图:
然后在弹出的对话框中点击右边文件,找到我们刚才创建的webapp文件夹,确定即可,具体如下:
此时,webapp会变为如下样子。
配置pom.xml文件
(1)首先引入spring-boot内嵌的tomcat对jsp的解析依赖,不添加解析不了jsp
<!--引入spring-boot内嵌的tomcat对jsp的解析依赖,不添加解析不了jsp--><dependency>    <groupid>org.apache.tomcat.embed</groupid>    <artifactid>tomcat-embed-jasper</artifactid></dependency>
(2)spring-boot默认使用的是前端引擎thymeleaf,现在我们要使用springboot继承jsp,需要手动 指定jsp最后编译的路径,而且springboot继承jsp的路径是springboot规定好的位置: meta-inf/resources
<build>    <!--spring-boot默认使用的是前端引擎thymeleaf,现在我们要使用springboot继承jsp,需要手动指定jsp最后编译的路径,而且springboot继承jsp的路径是springboot规定好的位置:meta-inf/resources-->    <resources>        <resource>            <!--源文件-->            <directory>src/main/webapp</directory>            <!--指定编译路径:-->            <targetpath>meta-inf/resources</targetpath>            <!--指定源文件夹中的哪些资源需要被编译-->            <includes>                <include>*.*</include>            </includes>        </resource>    </resources>    <plugins>        <!-- ··· -->    </plugins></build>
最后一步:在 application.properties 中配置视图解析器
# 配置视图解析器spring.mvc.view.prefix=/ # 前缀spring.mvc.view.suffix=.jsp # 后缀
创建.jsp页面,测试:
<%@ page contenttype="text/html;charset=utf-8" language="java" %><html><head>    <title>title</title></head><body>  <h2>${msg}</h2></body></html>
package com.lxc.boot_02; import org.springframework.stereotype.controller;import org.springframework.web.bind.annotation.requestmapping;import org.springframework.web.servlet.modelandview; @controllerpublic class controller {    // 写法一:    @requestmapping(value=/say)    public modelandview say() {        modelandview mv = new modelandview();        // 给视图传值        mv.addobject(msg, hello);        // 设置 最终视图的名称        mv.setviewname(say);        return mv;    }     // 写法二:把视图和模型拆分开,返回一个视图(return的是视图的名字)    @requestmapping(value = /index)    public string index(model model) {        model.addattribute(msg, lxc;);        return say;    }}
写法一:
写法二:
以上就是springboot入门使用实例分析的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product