-
启动彩蛋修改:
项目resources目录下建立banner.txt文件就可替换原来的菜单
字符画生成的网站http://www.network-science.de/ascii/ http://patorjk.com/software/taag/
-
切换不同环境配置
在idea 启动配置program arguments加上–spring.profiles.active={profile},或在dos行加上–spring.profiles.active={profile};
或配置文件spring.profiles.active={profile}
各个环境公共的配置写在application.properties中
各个模块独有的配置配置在自己的application-{xxx}.properties文件中
程序读取的时候优先读取application.properties中选中的profile的配置,若读不到才会从application.properties去读
-
读取配置
必须先@Component 然后参数@Value("${cusvar}"
@Value("${app.name}")
private String cusvar ; 将${app.name}值赋予cusvar
name= HowieLi
age= 18
content= "name: ${name}, age: ${age}"
代码中直接调用content就可以了,访问启动的应用显示name: HowieLi, age: 18。
-
@RestController该注解是Spring4之后新加的注解,等同于@Controller和@ResponseBody的组合。
@RequestMapping(value = "/hello", method = RequestMethod.GET)== @GetMapping("/hello")
@RequestMapping(value = {"/hello", "/hi"}, method = RequestMethod.GET)访问/hello和/hi是一样的效果
@GetMapping(value = "/say/{id}")
public String helloGet(@PathVariable("id") int id, @RequestParam("name") String name) {return "id: " + id + ",name:" + name;}访问http://localhost:8080/say/5?name=howieli
-
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import lombok.Data;
/**
* Created by on 2017/07/01.
*/
@Data
@Component //将Person类交由Spring容器管理
@ConfigurationProperties(prefix = "person") //填写配置文件中的前缀
public class Person {
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;
// }
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
@Autowired
private Person person;
@RequestMapping("/hellTask")
public String hellTask(){
logger.info("访问hellTask");
return person.toString();
}
获得配置文件值
-
Preferences -> Editor -> General -> Appearance, uncheck "Show Spring Boot metadata panel"解决Spring Boot Configuration Annotation Processor not found in classpath
-
Usage of API documented as @since 1.6+ This inspection finds all usages of methods that have @since tag in their documentation. This may be useful when development is performed under newer SDK version as the target platform for production解决方法
File ->Project Structure->Project Settings -> Modules -> 你的Module名字 -> Sources -> Language Level->选个默认的就行。
-
1.5不支持diamond运算符,请使用source 7或更高版本以启用diamond运算符,怎么办?
<properties>加上
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>或
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
-
** WARNING ** : Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package. 因为application.java 文件不能直接放在main/java文件夹下,必须要建一个包把他放进去
-
热部署参看http://www.cnblogs.com/java-zhao/p/5502398.html
http://blog.csdn.net/jsshaojinjie/article/details/64125458
<!-- 用于将应用打成可直接运行的jar(该jar就是用于生产环境中的jar) 值得注意的是,如果没有引用spring-boot-starter-parent做parent, 且采用了上述的第二种方式,这里也要做出相应的改动 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork><!-- 如果没有该项配置,肯呢个devtools不会起作用,即应用不会restart -->
</configuration>
</plugin>
<!-- devtools可以实现页面热部署(即页面修改后会立即生效,这个可以直接在application.properties文件中配置spring.thymeleaf.cache=false来实现),实现类文件热部署(类文件修改后不会立即生效),实现对属性文件的热部署。即devtools会监听classpath下的文件变动,并且会立即重启应用(发生在保存时机),注意:因为其采用的虚拟机机制,该项重启是很快的-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional><!-- optional=true,依赖不会传递,该项目依赖devtools;之后依赖myboot项目的项目如果想要使用devtools,需要重新引入 -->
</dependency>
CTRL + alt + s --> 查找make project automatically --> 选中
ctrl+shift+alt+/ --> 查找Registry --> 找到并勾选compiler.automake.allow.when.app.running
关闭热部署spring.devtools.restart.enabled 属性为false
System.setProperty("spring.devtools.restart.enabled","false");
-
public static void main(String[] args) throws IOException {
Properties properties= new Properties();
//System.out.println(System.getProperty("user.dir"));
//自己设置文件位置
//InputStream in = new FileInputStream(System.getProperty("user.dir")
//+"/isoft-manager/isoft-manager-pojo/app.properties");
//Resources位置
InputStream in=Application.class.getClassLoader().getResourceAsStream("app"
+ ".properties");
properties.load(in);
SpringApplication app=new SpringApplication(Application.class);
app.setDefaultProperties(properties);
app.run(args);
}使用自己的启动设置
-
使用fastjson
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters(){
//1.需要定义一个convert转换消息的对象;
FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
//2:添加fastJson的配置信息;
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//3处理中文乱码问题
List<MediaType> fastMediaTypes = new ArrayList<MediaType>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
//4.在convert中添加配置信息.
fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
HttpMessageConverter<?> converter = fastJsonHttpMessageConverter;
return new HttpMessageConverters(converter);}
-
http://blog.csdn.net/xiaoyu411502/article/details/48049099application.properties配置说明