Spring框架 — Spring核心概念
1.什么是SpringIOC控制反转
控制反转(Inversion of Cpntrol IoC),也被称为依赖注入,是面向对象编程的中的一种设计理念,用来降低程序代码之间的耦合
2.创建Spring项目
-
在pom.xml中导入spring依赖
<properties> <!-- spring版本号 --> <spring.version>4.3.6.RELEASE</spring.version> </properties> <!--Spring相关依赖--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency>
-
创建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: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/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> </beans>
-
把对象创建交给spring管理
<bean id="accountService" class="com.accp.service.impl.AccountServiceImpl"></bean>
-
创建测试类
//1.获取核心容器对象 ClassPathXmlApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml"); //2.根据id获取bean对象 AccountService as =ac.getBean("accountService",AccountService.class); //3.根据对象调取方法 as.saveAccount(); //4.关闭核心容器对象 ac.close();
3.SpringAOP面向切面编程
面向切面编程是软件编程思想发展到一定阶段的掺入,是面向对象编程的有益补充。AOP一般适用于具有横切逻辑的场合,如访问控制、事务管理、性能监测等。
4.在applicationContext.xml中配置AOP切面
- 把通知Bean也交给spring来管理
- 使用aop:config标签表明开始配置AOP
- 使用aop:aspect标签表明配置切面
- 在aop:aspect内部使用对应标签来配置通知类型
<!--配置Logger类-->
<bean id="logger" class="com.accp.utils.Logger"></bean>
<aop:config>
<!--配置切入点表达式 id表示指定表达式的唯一标识 expression属性用于指定表达式内容
此标签写在aop:aspect标签内部只能当前切面使用
它还可以写在aop:aspect外面,此时变为所有切面可以用。注意必须写在aop:aspect前面
-->
<aop:pointcut id="proxy" expression="execution(* com.accp.service.*.*(..))"/>
<aop:aspect id="logAdvice" ref="logger">
<!--配置前置通知 在切入点方法之前执行-->
<aop:before method="prLogger" pointcut-ref="proxy"></aop:before>
<!--配置后置通知 在切入点方法正常执行之后执行-->
<aop:after-returning method="afterReturnPrLogger" pointcut-ref="proxy"></aop:after-returning>
<!--配置异常通知 在切入点方法产生异常之后执行-->
<aop:after-throwing method="afterThrowingPrLogger" pointcut-ref="proxy"></aop:after-throwing>
<!--配置最终通知 无论切入点是否正常都会执行-->
<aop:after method="afterPrLogger" pointcut-ref="proxy"></aop:after>
</aop:aspect>
5.配置AOP环绕通知
<!--配置环绕通知-->
<aop:around method="aroundLogger" pointcut-ref="proxy"></aop:around>
if ("".equals(username) && "".equals(pwd)) {
try {
pp.proceed();
} catch (Throwable throwable) {
throw new RuntimeException(e);
}
} else {
System.out.println("用户名或密码错误");
}
spring框架为我们提供一个接口:该接口中有一个方法proceed()此方法相当于明确调用切入点方法。该方法可以作环绕通知方法的参数,在程序执行时,spring框架会为我们提供该接口的实现类供我们使用
6.bean标签的作用范围
- singleton:单例的(默认值)
- prototype:多例的
- request:用于web应用的请求范围
- session:用于web应用的会话范围
- global_session:作用于web应用的集群范围(全局会话范围),当不是集群范围的时候他就是session
7.bean对象的的生命周期
-
单例对象:出生:当容器创建时就一直活着
活着:只要容器还在就一直活着
死亡:容器销毁,对象死亡
总结:单例对象的生命周期和容器相同
-
多例对象:出生:当我们使用对象时spring框架为我们创建
活着:对象在使用过程中就就一直活着
死亡:当对象长时间不使用或者没有其他对象应用是,有java的垃圾回收机制回收