我的项目结构:
1.使用idea的maven的quickstart构建父项目
接下来输入:groupId填com.imooc.security ArtifactId填imooc-security-parent
删除它自建的src目录,并将pom.xml改成如下:(将packing写成pom)
<?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">
<parent>
<artifactId>imooc-security-parent</artifactId>
<groupId>com.imooc.security</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>imooc-security-demo</artifactId>
<packaging>jar</packaging>
<name>imooc-security-demo Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<dependencies>
<dependency>
<groupId>com.imooc.security</groupId>
<artifactId>imooc-security-browser</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
然后右击项目,--》new Mudel创建imooc-security-core模块
修改其pom.xml如下:(一下packing里都是jar的形式)
<?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">
<parent>
<artifactId>imooc-security-parent</artifactId>
<groupId>com.imooc.security</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>imooc-security-core</artifactId>
<name>imooc-security-core</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<packaging>jar</packaging>
<!--核心代码,引入的第三方项目都在这里-->
<dependencies>
<!--这里不用写版本号version,因为父项目的springio,cloud和dependencemanager会统一版本,并且保证兼容性-->
<!--它会引入大量的jar包,主要引入springsecurity jar包和springsecurity oauth2-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
</dependency>
<!-- 存储token,绑定qq,微信等-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 通过spring-social来集成第三方服务,如qq-->
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-web</artifactId>
</dependency>
<!--开发工具包,比如字符的处理-->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
</dependency>
<!-- 集合操作-->
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
</dependency>
<!-- 反射包,反射操作-->
<dependency>
<groupId>commons-beautils</groupId>
<artifactId>commons-beautils</artifactId>
</dependency>
</dependencies>
</project>
app,browser模块都是按core方式创建即可,它们都父项目都是security-parent,并依赖于core模块,demo模块创建maven时选择webapp创建:(别的都一样)
app模块的pom.xml:
<?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">
<parent>
<artifactId>imooc-security-parent</artifactId>
<groupId>com.imooc.security</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>imooc-security-app</artifactId>
<name>imooc-security-app</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.imooc.security</groupId>
<artifactId>imooc-security-core</artifactId>
</dependency>
</dependencies>
</project>
browser模块的pom.xml:
<?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">
<parent>
<artifactId>imooc-security-parent</artifactId>
<groupId>com.imooc.security</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>imooc-security-browser</artifactId>
<name>imooc-security-browser</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<packaging>jar</packaging>
<dependencies>
<!-- 集群环境下的session管理-->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
</dependency>
<dependency>
<groupId>com.imooc.security</groupId>
<artifactId>imooc-security-core</artifactId>
</dependency>
</dependencies>
</project>
demo模块的pom.xml:
<?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">
<parent>
<artifactId>imooc-security-parent</artifactId>
<groupId>com.imooc.security</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>imooc-security-demo</artifactId>
<packaging>jar</packaging>
<name>imooc-security-demo Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<dependencies>
<dependency>
<groupId>com.imooc.security</groupId>
<artifactId>imooc-security-browser</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
demo的测试代码如下:
package imooc.security;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class Demo {
public static void main( String[] args )
{
SpringApplication.run(Demo.class,args);
}
@GetMapping("/hello")
public String hello(){
return "你好,世界";
}
}
访问localhost:8080/hello 即可看到运行成功,环境搭建完成。
后续可以打包项目成.jar格式通过java命令启动(进入jar包所在的目录,此时可以看到springboot项目启动了==idea使用main函数启动一样,它也和启动tomcat,运行web一样,其实springboot的项目不需要tomcat服务器,因为它有内嵌的tomcat插件,所以只需要打包成jar文件,丢到服务器,然后进入可运行的jar所在的目录,用命令 java -jar demo.jar 运行jar文件,我们的web项目就像tomcat发布项目一样跑起来了) java -jar demo.jar