SpringBoot学习小结(2018-08-11)

SpringBoot的基本配置:

org.springframework.boot.autoconfigure.SpringBootApplication:

.@SpringBootApplication:核心注解,注解在带main方法的主类上,是整个项目的入口,同时加入SpringApplication.run(App.class, args)启动整个项目.

方法示例:

/*@SpringBootApplication(XXX.class):这种格式用来过滤掉不需要的默认配置*/
@SpringBootApplication
/*@SpringBootConfiguration
 *@EnableAutoConfiguration
 *@ComponentScan*/
/*这3个注解加起来的效果等同于@SpringBootApplication*/
public class App
{
    public static void main( String[] args )
    {
     //String a = "123";
        System.out.println( "Hello World!" );
        SpringApplication.run(App.class, args);
    }
}

二. @RestConroller定义一个controller注解指定类为控制器。@RequestMapping:作用于类中的方法,用于指定访问URL路径

示例:

@RestController
public class HelloWorldController {
 @Value(value="${test.name}")
 String name ;
 @Value(value="${test.age}")
 String age;
 
 @Autowired
 private UserConfig userConfig;
 @RequestMapping("/hello111")
 public String Hello(){
  String message = "name="+name+"age="+age;
  return message;
 }
 @RequestMapping("/hello222")
 public String HelloWorld(){
  String message = "name="+name+"age="+age;
  return message;
 }
 
 @RequestMapping("/hello333")
 public String HelloWorldOk(){
  String message = "name="+userConfig.getName()+"age="+userConfig.getAge();
  return message;
 }
}

上例中共用2种方式实现了参数的自动配置:

@Value(value="${test.name}")
 String name ;
 @Value(value="${test.age}")
 int age;

@Value注解形式为在src/main/resources 目录下的application.properties配置文件中定义了以下参数:

server.port=8081
test.name=张三
test.age=24

此配置文件中可以更改端口信息(server.port=8081)和定义属性(test.name=张三
test.age=24)等信息,这种方式定义属性不安全,推荐使用第2种

第二种方式为定义一个实体对象的配置文件:user.proterties,里面定义出UserConfig实体对象私有属性值:

user.name=李四
user.age=12

然后新建这个实体对象UserConfig.java,设置get()和set()方法,

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix="user",locations={"classpath:user.properties"})
public class UserConfig {
 private String name ;
 private int age;
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
}

@Component注解用于确定此类是一个配置对象,

@ConfigurationProperties(prefix="user",locations={"classpath:user.properties"}),注解用于指定此类获取配置文件的位置,

prefix="user"指配置文件中是否有前缀,这样配置后,都会去配置文件中找到各自定义的属性值。

在带main方法的主类里面run as java application 即可启动整个项目。启动后会出现SpringBoot特殊图案:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.4.5.RELEASE)---------------截图不好看0.0.。。。。。。。。。。。。。。。。。。。

启动成功后会有启动成功信息:

: Tomcat started on port(s): 8081 (http)

: Started App in 5.284 seconds (JVM running for 5.836)

: Initializing Spring FrameworkServlet 'dispatcherServlet'
: FrameworkServlet 'dispatcherServlet': initialization started
: FrameworkServlet 'dispatcherServlet': initialization completed in 41 ms

确认启动的端口号等信息,即可通过浏览器访问了。

遇到的坑:myeclipse10对1.8版本的jdk支持性不是很好,而最新版本的maven要求jdk版本最低为1.7,之前用1.8的jdk,在Controller中定义name和age变量时直接报错了,网上查了许久换成了1.7,马上就好了,服!!!

当启动后不知道怎么关闭服务,所以当新建第二个maven项目跑的时候,报错了,第一个端口已经被占用,需要重启时,需要在cmd中输入命令:netstat -aon |findstr 8080   8080即为第一次启动时的端口号,找出占用当前端口的线程(监听),并杀掉:taskkill  /f /t /im AAAA,AAAA即为占用的线程。很痛苦啊有木有!!!!!!!!!!!!!!!


 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot与Shiro结合可以实现权限控制和会话管理。Shiro是一个易于使用的安全框架,提供了认证、授权、加密和会话管理等功能。与Spring Security相比,Shiro更加灵活和易于集成。在Spring Boot中使用Shiro,可以通过配置文件或者编程方式来定义用户和角色的权限,并且还可以自定义认证和授权的逻辑。 在Spring Boot中,可以使用Shiro的Session管理功能来管理用户的会话。Spring Boot默认使用的是Servlet容器的会话管理机制,可以通过配置文件或者编程方式来定义会话的超时时间和会话验证机制。另外,还可以使用Shiro提供的一些会话管理的特性,比如分布式会话和集群会话等。 总结来说,Spring Boot与Shiro结合可以实现灵活的权限控制和会话管理。可以根据具体的需求来配置和使用Shiro的功能,同时也可以扩展和定制Shiro的行为。这样可以确保系统的安全性和稳定性,提供更好的用户体验。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [SpringBoot学习小结之权限控制Shiro](https://blog.csdn.net/qq_23091073/article/details/125241300)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [springboot-2.0-shiro.rar](https://download.csdn.net/download/jcuiming/12609732)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值