- 创建Maven工程
- 添加SpringMVC及Servlet依赖
pom.xml文件添加如下:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
- 添加包
com.zhangjian
- 添加Spring配置
package com.zhangjian;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
@Configuration
@ComponentScan(basePackages = "com.zhangjian", useDefaultFilters = true, excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Controller.class)})
public class SpringConfig {
}
Alt+Enter会自动Import需要的类;
- @Configuration 注解表示这是一个配置类,在我们这里,这个配置的作用类似于 applicationContext.xml
- @ComponentScan 注解表示配置包扫描,里边的属性和 xml 配置中的属性都是一一对应的,useDefaultFilters 表示使用默认的过滤器,然后又除去 Controller 注解,即在 Spring 容器中扫描除了 Controller 之外的其他所有 Bean 。
@ComponentScan注解进行扫描的几种方式 - 添加SpringMVC配置
此处的Controller与上面的有区别,在导入时注意;
package com.zhangjian;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.servlet.mvc.Controller;
@Configuration
@ComponentScan(basePackages = "com.zhangjian",useDefaultFilters = false,includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = Controller.class)})
public class SpringMVCConfig {
}
注意,如果不需要在 SpringMVC 中添加其他的额外配置,这样就可以了。即 视图解析器、JSON 解析、文件上传……等等,如果都不需要配置的话,这样就可以了;
- 配置web.xml
此时,我们并没有 web.xml 文件,这时,我们可以使用 Java 代码去代替 web.xml 文件,这里会用到 WebApplicationInitializer ,具体定义如下:
WebInit 的作用类似于 web.xml,这个类需要实现 WebApplicationInitializer 接口,并实现接口中的方法,当项目启动时,onStartup 方法会被自动执行,我们可以在这个方法中做一些项目初始化操作,例如加载 SpringMVC 容器,添加过滤器,添加 Listener、添加 Servlet 等;
注意:
由于我们在 WebInit 中只是添加了 SpringMVC 的配置,这样项目在启动时只会去加载 SpringMVC 容器,而不会去加载 Spring 容器,如果一定要加载 Spring 容器,需要我们修改 SpringMVC 的配置,在 SpringMVC 配置的包扫描中也去扫描 @Configuration 注解,进而加载 Spring 容器,还有一种方案可以解决这个问题,就是直接在项目中舍弃 Spring 配置,直接将所有配置放到 SpringMVC 的配置中来完成,这个在 SSM 整合时是没有问题的,在实际开发中,较多采用第二种方案,第二种方案,SpringMVC 的配置如下:
package com.zhangjian;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.servlet.mvc.Controller;
@Configuration
@ComponentScan(basePackages = "com.zhangjian")
public class SpringMVCConfig {
}
这种方案中,所有的注解都在 SpringMVC 中扫描,采用这种方案的话,则 Spring 的配置文件就可以删除了;
- 测试
最后,添加一个 HelloController ,然后启动项目进行测试:
package com.zhangjian;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "hello";
}
}
启动项目,访问接口,结果如下:
在启动项目的时候,失败:
D:\JDK8_Package\JDK8\bin\java.exe -Dmaven.multiModuleProjectDirectory=D:\springboot-learn-001 -Dmaven.home=D:\IDEAJ_Package\plugins\maven\lib\maven3 -Dclassworlds.conf=D:\IDEAJ_Package\plugins\maven\lib\maven3\bin\m2.conf -Dmaven.ext.class.path=D:\IDEAJ_Package\plugins\maven\lib\maven-event-listener.jar -javaagent:D:\IDEAJ_Package\lib\idea_rt.jar=57391:D:\IDEAJ_Package\bin -Dfile.encoding=UTF-8 -classpath D:\IDEAJ_Package\plugins\maven\lib\maven3\boot\plexus-classworlds-2.6.0.jar org.codehaus.classworlds.Launcher -Didea.version2019.3
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.147 s
[INFO] Finished at: 2020-03-14T21:46:03+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] No goals have been specified for this build. You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>. Available lifecycle phases are: validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy, pre-clean, clean, post-clean, pre-site, site, post-site, site-deploy. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/NoGoalSpecifiedException
解决办法:
在pom.xml 文件中添加build
标签,并添加子标签: <defaultGoal>compile</defaultGoal>
;
再次运行:
build成功~
- 将项目发布到Tomcat中(idea方式)
IntelliJ版本:2019.3.3
先下载了最新的Tomcat<apache-tomcat-10.0.0-M1-windows-x64.zip>,配置时总是报错“Application
server libraries not found”网上查了下,又说版本太高,有说权限问题,有说选择路径问题,估计发生这个问题的情况还是挺多的
最终重新下载了<apache-tomcat-8.5.51-windows-x64.zip>,问题解决,看来我的情况是版本问题造成的
重新下载较低版本的Tomcat8并配置:
成功解决!