相关资源:
1. spring功能介绍官方文档:https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html
2. spring API文档:https://docs.spring.io/spring/docs/5.0.1.RELEASE/javadoc-api/
3. spring与其他java web框架整合的文档:https://docs.spring.io/spring/docs/current/spring-framework-reference/integration.html
4.spring支持下的事务管理:https://docs.spring.io/spring/docs/current/spring-framework-reference/data-access.html#transaction
只是使用spring的bean管理
在maven下的配置为:
<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.hurricane.web</groupId>
<artifactId>spring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<!-- log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
</project>
相应引入的jar包有:
spring相关(spring-context及其依赖):
1.spring-context-4.3.10.RELEASE.jar
2.spring-aop-4.3.10.RELEASE.jar
3.spring-beans-4.3.10.RELEASE.jar
4.spring-core-4.3.10.RELEASE.jar
5.commons-logging-1.2.jar
6.spring-expression-4.3.10.RELEASE.jar
日志相关(log4j及其依赖):
1.log4j-1.2.17.jar
测试相关(junit及其依赖):
1.junit-4.10.jar
2.hamcrest-core-1.1.jar
相应的spring配置文件头参考:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xmlns:task="http://www.springframework.org/schema/task" 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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
spring中使用AOP
1.当被应用aop的类是实现了一个接口,并且被pointcut匹配到的方法为接口中的一个方法时,spring会使用spring自己的实现(以jdk支持的代理为基础),生成该类的代理(proxy),来支持aop。
2.当被应用aop的类没有实现接口,那么为了给pointcut匹配到的方法应用aop,spring会使用cglib的实现(具体为操作生成的class字节码)。
ps.当某个实现了接口类中某个方法被应用aop,那么spring会为这个类生成代理,因此若是应用spring的API中的getBean(xxx.class)是无法得到相应的bean的,只能通过在spring容器中该bean的id,name来获取该bean。若某个类没有实现接口,那么是可以使用getBean(xxx.class)来获取bean的。
可以明确指定只是使用cglib生成代理,通过:
<aop:aspectj-autoproxy proxy-target-class="true"/>
使用spring的aop需要引入cglib的jar包,依赖如下:
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.11</version>
</dependency>
引入的jar为:aspectjweaver-1.8.11.jar,应用around的aop时,around方法中的ProceedingJoinPoint参数在该包中定义,因此必须引入,即使每个被代理的类都实现了接口。
spring中添加的配置:
<aop:aspectj-autoproxy/>
spring注解声明bean与自动注入
在spring的配置文件中声明:
<context:component-scan base-package=""/>
这个配置的作用:
Scans the classpath for annotated components that will be auto-registered as Spring beans. By
default, the Spring-provided @Component, @Repository, @Service, @Controller, @RestController,
@ControllerAdvice, and @Configuration stereotypes will be detected. Note: This tag implies the
effects of the 'annotation-config' tag, activating @Required, @Autowired, @PostConstruct,
@PreDestroy, @Resource, @PersistenceContext and @PersistenceUnit annotations in the component
classes, which is usually desired for autodetected components (without external configuration). Turn
off the 'annotation-config' attribute to deactivate this default behavior。