1、入口类和@SpringBootApplication
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
excludeFilters = {@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
), @Filter(
type = FilterType.CUSTOM,
classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
@AliasFor(
annotation = EnableAutoConfiguration.class,
attribute = "exclude"
)
Class>[] exclude() default {};
@AliasFor(
annotation = EnableAutoConfiguration.class,
attribute = "excludeName"
)
String[] excludeName() default {};
@AliasFor(
annotation = ComponentScan.class,
attribute = "basePackages"
)
String[] scanBasePackages() default {};
@AliasFor(
annotation = ComponentScan.class,
attribute = "basePackageClasses"
)
Class>[] scanBasePackageClasses() default {};
}
(3)将网站生成的字符复制到banner.txt文件里
package net.hw.bean;
/**
* Created by howard on 2017/3/31.
*/
public class User {
private int id;
private String name;
private String gender;
private String age;
private String telephone;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", age='" + age + '\'' +
", telephone='" + telephone + '\'' +
'}';
}
}
org.apache.tomcat.embed
tomcat-embed-jasper
javax.servlet
jstl
显示图书信息
编号
${bookId}
书名
${bookName}
作者
${bookAuthor}
单价
${bookPrice}
出版社
${bookPress}
package net.hw.webmvc;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* Created by howard on 2017/3/31.
*/
@Controller
@PropertySource("classpath:book.properties")
public class BookController {
@Value("${book.id}")
private String bookId;
@Value("${book.name}")
private String bookName;
@Value("${book.author}")
private String bookAuthor;
@Value("${book.price}")
private double bookPrice;
@Value("${book.press}")
private String bookPress;
@RequestMapping("/showBook")
public String showBook(ModelMap map) {
map.addAttribute("bookId", bookId);
map.addAttribute("bookName", bookName);
map.addAttribute("bookAuthor", bookAuthor);
map.addAttribute("bookPrice", bookPrice);
map.addAttribute("bookPress", bookPress);
return "showBook";
}
}
org.springframework.boot
spring-boot-starter-freemarker
1.5.2.RELEASE
package net.hw.webmvc;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by howard on 2017/3/31.
*/
@RestController
public class ProductController {
@Value("${product.id}")
private int id;
@Value("${product.name}")
private String name;
@Value("${product.price}")
private double price;
@RequestMapping("/showProduct")
public String showProduct() {
return "产品编号:" + id
+ "
产品名称:" + name
+ "
产品单价:" + price;
}
}
package net.hw.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* Created by howard on 2017/3/31.
*/
@Component
@ConfigurationProperties(prefix = "student")
public class Student {
private int id;
private String name;
private String gender;
private int age;
private String telephone;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
}
package net.hw.webmvc;
import net.hw.bean.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by howard on 2017/3/31.
*/
@RestController
public class StudentController {
@Autowired
private Student student;
@RequestMapping("/showStudent")
public String showStudent() {
return "学号:" + student.getId()
+ "
姓名:" + student.getName()
+ "
性别:" + student.getGender()
+ "
年龄:" + student.getAge()
+ "
电话:" + student.getTelephone();
}
}
package org.springframework.boot.autoconfigure.condition;
import org.springframework.boot.autoconfigure.condition.ConditionMessage;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.boot.autoconfigure.condition.ConditionMessage.Builder;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.annotation.Order;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.StandardServletEnvironment;
@Order(-2147483628)
class OnWebApplicationCondition extends SpringBootCondition {
private static final String WEB_CONTEXT_CLASS = "org.springframework.web.context.support.GenericWebApplicationContext";
OnWebApplicationCondition() {
}
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
boolean required = metadata.isAnnotated(ConditionalOnWebApplication.class.getName());
ConditionOutcome outcome = this.isWebApplication(context, metadata, required);
return required && !outcome.isMatch()?ConditionOutcome.noMatch(outcome.getConditionMessage()):(!required && outcome.isMatch()?ConditionOutcome.noMatch(outcome.getConditionMessage()):ConditionOutcome.match(outcome.getConditionMessage()));
}
private ConditionOutcome isWebApplication(ConditionContext context, AnnotatedTypeMetadata metadata, boolean required) {
Builder message = ConditionMessage.forCondition(ConditionalOnWebApplication.class, new Object[]{required?"(required)":""});
if(!ClassUtils.isPresent("org.springframework.web.context.support.GenericWebApplicationContext", context.getClassLoader())) {
return ConditionOutcome.noMatch(message.didNotFind("web application classes").atAll());
} else {
if(context.getBeanFactory() != null) {
String[] scopes = context.getBeanFactory().getRegisteredScopeNames();
if(ObjectUtils.containsElement(scopes, "session")) {
return ConditionOutcome.match(message.foundExactly("\'session\' scope"));
}
}
return context.getEnvironment() instanceof StandardServletEnvironment?ConditionOutcome.match(message.foundExactly("StandardServletEnvironment")):(context.getResourceLoader() instanceof WebApplicationContext?ConditionOutcome.match(message.foundExactly("WebApplicationContext")):ConditionOutcome.noMatch(message.because("not a web application")));
}
}
}
Character Encoding
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
package org.springframework.boot.autoconfigure.web;
import java.nio.charset.Charset;
import java.util.Locale;
import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(
prefix = "spring.http.encoding"
)
public class HttpEncodingProperties {
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
private Charset charset;
private Boolean force;
private Boolean forceRequest;
private Boolean forceResponse;
private Map mapping;
public HttpEncodingProperties() {
this.charset = DEFAULT_CHARSET;
}
public Charset getCharset() {
return this.charset;
}
public void setCharset(Charset charset) {
this.charset = charset;
}
public boolean isForce() {
return Boolean.TRUE.equals(this.force);
}
public void setForce(boolean force) {
this.force = Boolean.valueOf(force);
}
public boolean isForceRequest() {
return Boolean.TRUE.equals(this.forceRequest);
}
public void setForceRequest(boolean forceRequest) {
this.forceRequest = Boolean.valueOf(forceRequest);
}
public boolean isForceResponse() {
return Boolean.TRUE.equals(this.forceResponse);
}
public void setForceResponse(boolean forceResponse) {
this.forceResponse = Boolean.valueOf(forceResponse);
}
public Map getMapping() {
return this.mapping;
}
public void setMapping(Map mapping) {
this.mapping = mapping;
}
boolean shouldForce(HttpEncodingProperties.Type type) {
Boolean force = type == HttpEncodingProperties.Type.REQUEST?this.forceRequest:this.forceResponse;
if(force == null) {
force = this.force;
}
if(force == null) {
force = Boolean.valueOf(type == HttpEncodingProperties.Type.REQUEST);
}
return force.booleanValue();
}
static enum Type {
REQUEST,
RESPONSE;
private Type() {
}
}
}
4.0.0
net.hw
spring-boot-starter-greet
1.0-SNAPSHOT
jar
spring-boot-starter-greet
http://maven.apache.org
UTF-8
org.springframework.boot
spring-boot-autoconfigure
1.5.2.RELEASE
junit
junit
3.8.1
test
package net.hw.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Created by howard on 2017/4/2.
*/
@ConfigurationProperties(prefix = "greet")
public class GreetProperties {
private static String NAME = "World";
private String name = NAME;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package net.hw.service;
/**
* Created by howard on 2017/4/2.
*/
public class GreetService {
private String name;
public String greet() {
return "Hello, " + name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package net.hw;
import net.hw.config.GreetProperties;
import net.hw.service.GreetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Created by howard on 2017/4/2.
*/
@Configuration
@EnableConfigurationProperties(GreetProperties.class)
@ConditionalOnClass(GreetService.class)
@ConditionalOnProperty(prefix = "greet", value = "enabled", matchIfMissing = true)
public class GreetServiceAutoConfiguration {
@Autowired
private GreetProperties greetProperties;
@Bean
@ConditionalOnMissingBean(GreetService.class)
public GreetService greetService() {
GreetService greetService = new GreetService();
greetService.setName(greetProperties.getName());
return greetService;
}
}
net.hw
spring-boot-starter-greet
1.0-SNAPSHOT
package net.hw;
import net.hw.service.GreetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class TestMyStarterApplication {
@Autowired
private GreetService greetService;
@RequestMapping("/")
public String home() {
return greetService.greet();
}
public static void main(String[] args) {
SpringApplication.run(TestMyStarterApplication.class, args);
}
}