##常用依赖
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<!-- Spring框架整体依赖 -->
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.18</version>
</dependency>
<!-- 测试类实现依赖 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--AOP实现依赖-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>
##自动装配导入配置文件
<?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:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--开启注解的支持-->
<context:annotation-config/>
</beans>
##注解说明
-
@Autowired:自动装配类型:1.类型 2.名字
如果@Autowired不能唯一自动装配上属性,则需要@Qualifier(value=“xxx”); -
@Nullable 字段标记了这个注解,说明这个字段可以为null;
-
@Resource: 自动装配类型:1.名字 2.类型
-
@Component:组件,放在类上,说明这个类被Spring管理了,就是Bean!
-
@Repository :是Dao层自动装配的注解
-
@Service :是Service层自动装配的注解
-
@Controller :是Controller层自动装配的注解
-
@Scope(“singleton”) 单例模式 或者@Scope(“prototype”) 原型模式;
-
@Configuration 这是相当于把一个Java文件变成applicationContext配置文件
-
@ComponentScan 这是扫描包,使当前包下的注解生效
-
@Import 把其他 applicationContext融合过来
-
@Bean 相当于bean标签,用来实例化对象
-
@Value 相当于Property,给对象注入值
-
@ConfigurationProperties(prefix = “person”) 这个注解修饰当前类Person,可以把当前类的属性与 application.yaml里的person对象属性绑定,把值注入给当前类
-
//加载指定配置文件
@PropertySource(value = “classpath:qingjiang.properties”)
##AOP注解
- @Aspect 把这个类当成切面
- @Before(“execution(修饰符 返回值 类名.方法名.参数)”) 表示这个方法切入切点的前面
- @After(“execution(修饰符 返回值 类名.方法名.参数)”) 表示这个方法切入切点的后面
- @Around(“execution(修饰符 返回值 类名.方法名.参数)”) 表示这个方法环绕切入切点
- aop:aspectj-autoproxy/需要这句话开启注解的支持!
MVC注解
@DateTimeFormat(pattern = "yyyy-MM-dd")//给实体类自动装配,日期类传进来字符串,转日期类注解
@RequestMapping(value = "insert",produces = {"text/html;charset=utf-8"})//前端响应回去加响应头,解决乱码问题
// 传进来,和对象一一对应的时候,时间字符串转化为时间对象,
// 或者,时间戳转化为时间对象的注解。
@DateTimeFormat(pattern = "yyyy-MM-dd")
// 把Java转JSON对象的时候,一一对应的,将Java日期类默认转化为时间戳
// 时间戳在前端转换为日期,也是格林威治时间,所以也要转换。
// 要想按照日期格式转换必须加这个注解,
// 然后时区默认时格林威治时间的时间,和中国时间差8小时,所以加8
@JsonFormat(pattern = "yyyy-MM-dd",timezone = "GMT+8")
<!-- 配置状态码异常错误页面设置 -->
<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>
package com.qf.common;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
/**
* 统一异常处理类
*/
@ControllerAdvice
public class MyExceptionHandler {
@ExceptionHandler(NullPointerException.class)
public String nullExp(){
return "/err1.jsp";
}
@ExceptionHandler(NumberFormatException.class)
public String NumFormatExp(){
return "/err2.jsp";
}
}