第一章 SSM传统配置
1.回顾xml配置Spring
创建maven工程,打包方式选择war包.引入坐标依赖.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itboy</groupId>
<artifactId>xmlssm</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
</dependencies>
</project>
构建web工程的目录:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-AXVDYa4j-1587440569451)(img/image-20200323084119296.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dTZxbP5K-1587440569456)(img/image-20200323084308381.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-j92BUaM1-1587440569459)(img/image-20200323085042526.png)]
构建出web的目录:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-y5joJGxY-1587440569462)(img/image-20200323085213206.png)]
创建spring的配置文件:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-e7nQYqDZ-1587440569464)(img/image-20200323085422292.png)]
applicationConfig.xml的配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.itboy" use-default-filters="true">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/><!--不扫描controller注解-->
</context:component-scan>
</beans>
spring-servlet.xml的配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.itboy" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<mvc:annotation-driven />
</beans>
web.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationConfig.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
书写controller类:
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@GetMapping("/hello")
public String hello(){
return helloService.sayHello();
}
}
书写service类:
@Service
public class HelloService {
public String sayHello() {
return "hello java!";
}
}
项目的结构:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uPKLAvnb-1587440569466)(img/image-20200323092648299.png)]
配置tomcat,
解决中文乱码的问题:
@GetMapping(value = "/hello", produces = "text/html;charset=utf-8")
2.java配置创建SSM
创建maven工程:
导入依赖坐标:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itboy</groupId>
<artifactId>javassm</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
</dependencies>
</project>
项目结构:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XSJUkgU3-1587440569469)(img/image-20200323103332750.png)]
SpringConfig类:
@Configuration
@ComponentScan(basePackages = "com.itboy", useDefaultFilters = true, excludeFilters = {@ComponentScan.Filter(type =
FilterType.ANNOTATION, classes = Controller.class)})
public class SpringConfig {
}
SpringMVCConfig类:
@Configuration
@ComponentScan(basePackages = "com.itboy", useDefaultFilters = false, includeFilters = {@ComponentScan.Filter(
type = FilterType.ANNOTATION, classes = Controller.class), @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Configuration.class)})
public class SpringMVCConfig {
}
WebInit类:
public class WebInit implements WebApplicationInitializer {
public void onStartup(javax.servlet.ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.setServletContext(servletContext);
ctx.register(SpringMVCConfig.class);
ServletRegistration.Dynamic springmvc = servletContext.addServlet("springmvc", new DispatcherServlet(ctx));
springmvc.addMapping("/");
springmvc.setLoadOnStartup(1);
}
}
第二章 springboot配置tomcat
学习网址:
https://www.javaboy.org/springboot/
https://www.ityouknow.com/spring-boot.html
banner定制:
网址:http://patorjk.com/software/taag
在resources的目录下新建banner.txt文件拷贝进去.
1.容器相关配置:tomcat
yml中的配置:
server:
port: 8082 # 修改服务器端口号
servlet:
context-path: /itboy # 修改上下文路径 访问路径前面加localhost:8082/itboy/hello
tomcat:
uri-encoding: utf-8 # 配置tomcat的url编码
配置其他的容器:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--去除掉Tomcat的配置-->
<!--<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>-->
</dependency>
<!--添加jetty的依赖-->
<!--<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>-->
2.spring的属性注入:
一般用来配置数据库的连接
实体类:
@Component
@PropertySource("classpath:book.properties")//类型安全的属性注入
@ConfigurationProperties(prefix = "book")
public class Book {
// @Value("${book.id}")
private Long id;
// @Value("${book.name}")
private String name;
// @Value("${book.author}")
private String author;
@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", author='" + author + '\'' +
'}';
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
properties文件:
book.id=10
book.name=哈哈
book.author=hehe
测试类:
@SpringBootTest
@RunWith(SpringRunner.class)
class SpringbootDemo01ApplicationTests {
@Autowired
Book book;
@Test
void contextLoads() {
System.out.println(book);//Book{id=89, name='三国演义', author='罗贯中'}
}
}
3.yaml或yml配置
与properties的区别:
1.yml配置有序的,properties配置是无序的
2.目前暂时不支持使用注解直接注入到springboot项目中
4.profile
application.properties
spring.profiles.active=dev#指定使用哪个配置文件
application-dev.properties
application-prod.properties