1.初始化spring容器
//具体文件系统的文件路径 ApplicationContext context = new FileSystemXmlApplicationContext("xxx.xml"); //相对路径class目录 ApplicationContext context = new ClassPathXmlApplicationContext("xxx.xml");
2.bean 的声明
- 在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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="名字" class="类型"> </bean> </beans>
- 使用注解
@Component:当对组件的层次难以定位的时候使用这个注解 @Controller:表示控制层的组件 @Service:表示业务逻辑层的组件 @Repository:表示数据访问层的组件
3.依赖注入的方式
- 使用注解
@Value(value="xxx")
4.自动注入
在需要自动注入的bean的声明autowire属性。
- byType(根据类型自动装配): 若 IOC 容器中有多个与目标 Bean 类型一致的 Bean. 在这种情况下, Spring 将无法判定哪个 Bean 最合适该属性, 所以不能执行自动装配.
- byName(根据名称自动装配): 必须将目标 Bean 的名称和属性名设置的完全相同.
使用注解自动注入
@Autowired 注解自动装配具有兼容类型的单个 Bean属性(根据类型注入,若需要根据名字注入@Qualifier("xxx") )
@Resource 注解要求提供一个 Bean 名称的属性,若该属性为空,则自动采用标注处的变量或方法名作为 Bean 的名称
@Inject 和 @Autowired 注解一样也是按类型匹配注入的 Bean, 但没有 reqired 属性
建议使用 @Autowired 注解
5.扫描包
- 添加 xml 验证
xmlns:context="http://www.springframework.org/schema/context" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
- 在beans标签加上
<context:component-scan base-package="xxxx" />
加上包扫描后,我们可以在扫描的包上使用spring 注解
6.在 pom.xml 里,添加 AOP 依赖
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.12.RELEASE</version> </dependency> <!-- spring aop支持 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.3.12.RELEASE</version> </dependency> <!-- aspectj支持 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.7</version> </dependency>
7.配置aop
基于注解配置
1.添加切面对象(aspect对象)(加上注解)
2.把切面对象交给spring
3.配置文件
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <!-- spring扫描包 --> <context:component-scan base-package="com.xxxxx" /> <!-- 自动代理 --> <aop:aspectj-autoproxy /> </beans>
8.spring AOP切面表达式详解
在使用spring框架配置AOP的时候,不管是通过XML配置文件还是注解的方式都需要定义pointcut"切入点"
例如定义切入点表达式 execution (* com.sample.service.impl...(..))
execution()是最常用的切点函数,其语法如下所示:
整个表达式可以分为五个部分:
1、execution(): 表达式主体。
2、第一个号:表示返回类型,号表示所有的类型。
3、包名:表示需要拦截的包名,后面的两个句点表示当前包和当前包的所有子包,com.sample.service.impl包、子孙包下所有类的方法。
4、第二个号:表示类名,号表示所有的类。
5、(..):最后这个星号表示方法名,号表示所有的方法,后面括弧里面表示方法的参数,两个句点表示任何参数
9.常用通知
@Before[MethodBeforeAdvice]: 前置通知, 在方法执行之前执行 @AfterReturning[AfterReturningAdvice]:返回通知, 在方法返回结果之后执行 @AfterThrowing [ThrowsAdvice]: 异常通知, 在方法抛出异常之后 //由于没有抽象方法: public void afterThrowing(Method method, Object[] args, Object target, Throwable exeptionClass) @Around[MethodInterceptor]: 环绕通知, 围绕着方法执行 @After []:最终通知 , 在方法后执行