springmvc设置注入到类里面

<dependency>
<groupId></groupId>
<artifactId></artifactId>
<!--
作者:2513643706@qq.com
时间:2019-05-28
描述:排除掉springboot默认的tomcatweb容器,再加上下面的话,配置
jetty,然后点击maven中的compile进行相关依赖的下载。
在下载完成后点击maven栏中的Reimport all maven projects的小圈圈进行重新的导入
然后点击进行项目的重启,后台日志就会是启动jetty
-->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
-------------------------------
核心类(启动类)中还能启动springboot的自动配置@SpringBootApplication注解
将核心类作为一个springioc容器进行spring配置以及bean的管理
-------------------------------------------
application.properties配置文件,作用进行系统参数配置,还可以进行默认信息的配置
-------------------------------------------------student.java
package com.example.hellospringboot;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
//下面指定前缀student,然后以名称匹配的方式对属性进行赋值
//component用于对寻找注解所标识的类用来匹配controller中设置的类
@Component
@ConfigurationProperties(prefix = "student")
//在application.properties内已经设置好student.name等属性,这个注解是为了匹配它的,"student."通过前缀进行区分
public class Strudent {
private String name;
private String age;

public String getName() {
return name;
}

public String getAge() {
return age;
}

public void setName(String name) {
this.name = name;
}

public void setAge(String age) {
this.age = age;
}
}
-----------------------------------------------hellospringcontroller.java
package com.example.hellospringboot;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
//使用autoware注解进行装配
//如果配置别的web容器的话还需要按reimport all maven project(右侧maven中的圈按钮)
@RestController
public class helloSpringController {
@Autowired
//将student的属性值注入
private Strudent student;
@RequestMapping("/sayHello")//声明方法
@ResponseBody
public String sayHello(){
return "hello"+ student.getName()+student.getAge();
}
}

------------------------------------总结一下步骤
先建立student类@ConfigurationProperties(prefix = "student")
然后在controller里面用@Autowired注入
最后在student类中添加@Component

-----------------------------------------------------------------------------------------------手动分割线----------------------

这是配置文件的一种使用方式,缺点是配置很多时不够条理清晰
再建立一个application.yml
student:
name: xiaoming;
age: 18;
推荐使用这种配置方式,注意属性前面注意要有空格。
配置比较多的时候显得条理清晰。
这里还可以配置其他的信息,这是springboot内置的配置
server:
port: 8081
这样修改默认端口8080
可以配置自定义参数,也可以配置系统的全局参数
建议使用application.yml格式的文件
----------------
在企业级开发中配置很多,不可能写在一个配置里面,分组开发会写在不同位置。
假如在resources中建一个config文件夹然后写一个配置文件,把yml中的拷入其配置文件中,重启项目
就会找不到。因为找不到配置文件了,刚才能找到yml的原因一是因为名称,二是因为propertiesheyml都在同一个目录下面
都是系统能够默认读取的,如果系统读取不到的,需要在核心类里面,加上注解指定配置文件路径。
@SpringBootApplication
@PropertySource("classpath:/config/config.properties")
public class HellospringbootApplication {

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

}