标签:xml 拦截器 SpringBoot 项目 Spring void System public
在SpringBoot项目中使用Spring
SpringBoot集成Spring并不用导入任何的依赖(pom.xml)
Spring中IOC和DI的概念
IOC是概念,编程思想(好莱坞原则)
DI具体措施,因为Spring实现IOC,衍生DI,副作用,用来解决问题
# # 方式一.XML方式注入(已过气)
1).在目录下src/main/resources
创建applicationContext.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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!-- 注解方式使用包扫描 :component-scan
base-package 基准包,它可以包括本身目录和子目录所有的类
-->
</beans>
2)在applicationContext.xml
注册所有类,spring框架才会创建类
<bean id="hello" class="cn.tedu.pojo.Hello" />
id为类名首字母小写 class为类名目录
package cn.tedu.pojo;
public class Hello {
// 增加一个方法
public void hi() {
System.out.println("spring ioc xml");
}
}
3)进行测试
public class TestIocXML {
@Test
public void XML() {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
// 创建spring环境,死语法
Hello hello=(Hello)ac.getBean("hello");
System.out.println(hello);
hello.hi();
}
}
结:方式一是利用在xml中加入实现的
# # 方式二 在XML文件里添加组件扫描
1).在applicationContext.xml
中添加组件扫描
注解方式使用包扫描 :component-scan
base-package 基准包,它可以包括本身目录和子目录所有的类
<context:component-scan base-package="cn.tedu.pojo"/>
2).在Java类中,加入@Component
即可直接注入,@Controller
和@Service
中包含@Component
3).直接测试即可
# # Spring拦截器创建方式
1)创建Myinterceptor
类,实现(implements)HandlerInterceptor
接口
2)重写接口中三个方法
方法一:preHandle(方法执行前执行)
@Override // true放行 false拦截
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
System.out.println("我是拦截器preHandle");
return HandlerInterceptor.super.preHandle(request, response, handler);
}
方法二:postHandle(方法执行后执行)
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
long startTime = System.currentTimeMillis();
}
方法三:afterCompletion(业务方法执行之后执行)
@Override // 业务方法执行之后
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
long endTime = System.currentTimeMillis();
System.out.println("执行耗时:" + (endTime-startTime) +" ms" );
}
3)写拦截器配置类SysInterceptorConfig
跟配置器放在同包中,需要实现WebMvcConfigurer
接口
从写addInterceptors
方法,并注入之前创建的拦截器
@Autowired
private Myinterceptor myic;
@Autowired
private TimeInterceptor timeic;
/**
* 注册拦截器
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 注册自己写的拦截器,如果是多个拦截器,形成一个拦截器链
// 拦截器链执行顺序,前面先后出
// myic.pre>timeic.pre>timeic.post>myic.post
registry.addInterceptor(myic);
registry.addInterceptor(timeic);
}
#注意:Spring包放在SpringBoot启动类子包中就可以直接加入扫秒
标签:xml,拦截器,SpringBoot,项目,Spring,void,System,public 来源: https://blog.csdn.net/weixin_48621005/article/details/114035059
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。