IDEA使用maven手动创建Spring项目
- 创建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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yw</groupId>
<artifactId>my-shop</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<!-- Spring Begin -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.17.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.17.RELEASE</version>
</dependency>
<!-- Spring End -->
<!-- Servlet Begin -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- Servlet End -->
<!-- Log Begin -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- Log End -->
<!-- Commons Begin -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
</dependencies></project>
- 创建maven文件夹
- 创建三层架构包
- 创建前台网站资源
- 配置web.xml
为了引入spring web模块的自动装配,必须在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_3_1.xsd"
version="3.1">
<!--配置spring自动注册容器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-context*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.yw.myshop.web.controller.UserController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>
- Spring配置文件
- 在resources文件夹下建立spring-context.xml文件,用于配置需要加载的类
- 由于每一个模块都要用到获取类,所以在common模块建立一个通用类专门获取配置的类,为了获取application必须实现ApplicationContextAware接口用于获取,DisposableBean接口用于销毁,代码如下
package com.yw.myshop.commons;
import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class SpringContext implements ApplicationContextAware,DisposableBean {
private static final Logger logger = LoggerFactory.getLogger(SpringContext.class);
private static ApplicationContext applicationContext;
public static ApplicationContext getApplicationContext(){
return applicationContext;
}
public static <T> T getBean(String name){
assertContextInjected();
return (T) applicationContext.getBean(name);
}
public static <T> T getBean(Class<T> clazz){
assertContextInjected();
return applicationContext.getBean(clazz);
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContext.applicationContext = applicationContext;
}
public void destroy() throws Exception {
logger.debug("清除SpringContext中的ApplicationContext:{}",applicationContext);
}
private static void assertContextInjected() {
Validate.validState(applicationContext != null, "applicationContext 属性未注入,请在 spring-context.xml 配置中定义 SpringContext");
}
}
Spring配置的头文件如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="springContext" class="com.yw.myshop.commons.SpringContext" />
<bean id="userDao" class="com.yw.myshop.dao.impl.UserDaoImpl" />
<bean id="userService" class="com.yw.myshop.service.impl.UserServiceImpl"/>
</beans>
也可以使用注解的方式配置,配置如下,扫描包下所有注解的类,会自动创建
<context:annotation-config />
<context:component-scan base-package="com.yw.myshop" />
- log4j配置文件
在resources文件夹下创建log4j.properties配置日志
log4j.rootLogger=INFO, console, file
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=logs/log.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.A3.MaxFileSize=1024KB
log4j.appender.A3.MaxBackupIndex=10
log4j.appender.file.layout.ConversionPattern=%d %p [%c] - %m%n
-
代码测试
创建过程中遇到的两个坑
完全复制一样的pom.xml却发现在在调用Spring web 实现接口的时候报错找不到类,最后发现在Maven管理中依赖Dependencies和插件Plugins报错,导致包没有完全下载,但是pom文件没有报错,出现这个原因是因为没有配置该项目的本地仓库,全部使用默认的.m2里的仓库,这样就导致pom文件冲突,设置完成以后刷新即可
报错地方
解决办法
在mac电脑中 IntelliJ IDEA->preferences
在windows中 file->setting
这是针对当前项目的设置