Spring基础知识(二)

本文介绍了如何在Spring框架中配置JdbcTemplate,包括Maven依赖管理和数据源设置,然后详细讲解了AOP(面向切面编程)的概念、优势以及在Spring中的实现,涉及基于XML和注解的两种通知类型和切入点表达式。
摘要由CSDN通过智能技术生成

Spring JDBC

Spring 是个一站式框架:Spring 自身也提供了控制层的 SpringMVC 和 持久
层的 Spring JdbcTemplate

环境配置

Maven导入Spring JdbcTemplate 的jar包

<!-- spring-jdbc-->
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-jdbc</artifactId>
 <version>5.2.2.RELEASE</version>
 </dependency>
 <!-- 阿里数据源-->
 <dependency>
<groupId>com.alibaba</groupId>
 <artifactId>druid</artifactId>
 <version>1.1.10</version>
 </dependency>

导入属性文件

<context:property-placeholder location=“config.properties”/>

管理数据源对象

<!--spring 管理与数据库链接 (数据源)-->
 <bean id="dataSource"class="com.alibaba.druid.pool.DruidDataSource">
 <propertyname="driverClassName" value="${driverClassName}"></property>
 <property name="url" value="${url}"></property>
 <property name="username" value="${uname}"></property>
 <property name="password" value="${pwd}"></property>
 <property name="initialSize" value="10"></property>
 <property name="minIdle" value="5"></property>
 <property name="maxActive" value="20"></property>
 </bean>
<!--在配置文件中创建JdbcTemplate-->
 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
 </bean>
 <property name="dataSource" ref="dataSource"></property>

使用JdbcTemplate
@Autowired
JdbcTemplate jdbcTemplate;

JdbcTemplate 中常用的方法

execute:无返回值,可执行ddl,增删改语句
update:执行新增、修改、删除语句;
queryForXXX:执行查询相关语句;

方法演示

@Repository
public class LogDao {
    @Autowired
    JdbcTemplate jdbcTemplate;
    public void Savelog(){
        jdbcTemplate.update("insert into log(oper_time) value (now()) ");
        jdbcTemplate.update("insert into log(oper_time) value (now()) ");  
    }
}
 

AOP( 面向切面编程)

AOP概述:

AOP为AspectOrientedProgramming 的缩写,意为:面向切面编程,通过
预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。

AOP优势:

利用AOP可以对业务逻辑和非业务逻辑进行隔离,从而使得各部分之间的
耦合度降低,提高程序的可重用性,同时提高了开发的效率
注:面向切面编程只是面向对象编程的一种补充。

核心原理:

使用动态代理的方式在执行方法前后或者出现异常的时候做加入相关的逻辑.

AOP的基本概念

连接点(Joinpoint):类中可以被增强(添加某些功能)的方法,这个方法就被称为连接点
切入点(pointcut):类中有很多方法可以被增强,但实际中只有add和update
被增强了(添加某些功能),那么add和update方法就被称为切入点(实际实现的连接点)
通知(Advice): 通知是指一个切面在特定的连接点要做的事情即(增加的功能)。
切面(Aspect):把通知添加到切入点的整个过程称为切面.(添加功能的过程)
目标(Target): 代理的目标对象(连接点,切入点所在类)
代理(Proxy): 向目标对象应用通知时创建的代理对象

springAOP 实现

Spring 将 AspectJ (AOP框架)的对于 AOP 的实现也引入到了自己的框架中。

下载AOP相关jar

 <dependency>
		 <groupId>org.springframework</groupId>
		 <artifactId>spring-aspects</artifactId>
		 <version>5.2.2.RELEASE</version>
 </dependency>

基于aspectj的xml配置方式实现

<beanid="aopdemo"class="com.ff.spring.aop.AopDemo"></bean>
 <aop:config>
 <!--配置切入点-->
 <aop:pointcutexpression="execution(*
 com.ff.spring.service.UserService.adduser(..))"id="adduser"/>
 <aop:pointcutexpression="execution(*
 com.ff.spring.service.UserService.*(..))"id="allmethod"/>
 <!--配置通知和切入点-->
 <aop:aspectref="aopdemo">
 <aop:beforemethod="savelog"pointcut-ref="adduser"/>
 <aop:aftermethod="savelog"pointcut-ref="adduser"/>
 <aop:aroundmethod="aroundAdvice"pointcut-ref="adduser"/>
 <aop:after-throwingmethod="exceptionAdvice"pointcut-ref="allmethod"
 throwing="e"/>
 </aop:aspect>
 </aop:config>
定义的切入点表达式:

在这里插入图片描述

AspectJ中常用的通知有五种类型:

前置通知:方法调用前使用
后置通知:方法调用后执行
异常通知:方法出现异常是调用
最终通知:无论方法是否被调用都执行
在这里插入图片描述

环绕通知:具备前置通知,后置通知,异常通知,最终通知
在这里插入图片描述
在这里插入图片描述

基于注解方式的实现

注:启动AspectJ支持:aop:aspectj-autoproxy/

定义通知:
@Component
 @Aspect
 publicclassAOPDemo{
	 @Before("execution(*com.ff.spring.demo1.dao.UserDao.*(..))")
	 publicvoidbefore(){
	 System.out.println("before");
	 }
	 @After("execution(* com.ff.spring.demo1.dao.UserDao.*(..))")
 publicvoidafter(){
	 System.out.println("after");
	 }
	 @Around("execution(* com.ff.spring.demo1.dao.UserDao.*(..))")
publicvoidaround(ProceedingJoinPointpoint)throwsThrowable{
	 System.out.println("start");
	 point.proceed();
	 System.out.println("end");
	 }
	 @AfterThrowing(value="execution(*
	 com.ff.spring.demo1.dao.UserDao.*(..))",throwing="e")
 publicvoidafterthrow(Throwable){
	 System.out.println("afterthrow");
	 }
	@AfterReturning("execution(* com.ff.spring.demo1.dao.UserDao.*(..))")
	 public void afterreturn(){
	 System.out.println("afterreturn");
	 }
 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java Spring是一个广泛使用的轻量级开源框架,用于构建企业级Web应用程序。Spring框架提供了模块化的解决方案,简化了企业级应用的开发、管理以及依赖注入(Dependency Injection,DI)等功能。以下是一些Spring基础知识点的整理: 1. **IoC(Inversion of Control)和DI(Dependency Injection)**:Spring的核心思想就是IoC,它反转了传统的控制流动,使对象之间通过容器管理彼此的依赖关系,而不是硬编码。DI是IoC的一种具体实现方式,通过配置文件或注解自动为对象提供所需依赖。 2. **Bean的作用域和生命周期**:Spring中的Bean有多种作用域,如Singleton(单例)、Prototype(原型)、Request、Session等。每个Bean都有其生命周期,从创建、初始化到使用和销毁。 3. **Spring配置文件**:通常使用XML配置文件(如applicationContext.xml)或Java配置(@Configuration classes)来定义Spring应用的组件和依赖关系。 4. **AOP(Aspect Oriented Programming)**:Spring AOP支持面向切面编程,可以编写跨组件的行为,比如日志记录、事务管理等。 5. **Spring MVC**:Spring提供的web MVC架构,包括Controller处理HTTP请求,Model负责数据访问和业务逻辑,View负责渲染结果给用户。 6. **Spring Boot**:Spring Boot简化了Spring应用的初始搭建,自动配置了许多常用的功能,使得快速开发变得更容易。 7. **Spring Data**:提供了一套高级API,用于简化数据访问操作,如JPA、MongoDB等。 8. **Spring Security**:用于实现Web应用的安全管理,包括认证、授权、会话管理等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值