IT 梁朝伟带你走进 Spring框架

IT 梁朝伟带你走进 Spring框架

1.什么是sprnig

1.1spring简述

  • Spring 是一个开源框架
  • Spring 是为了简化企业级应用开发而生
  • JavaBean 实现只EJB才能开发的功能
  • Spring 是java/javaee 一站式框架
  • Spring是一个容器,它包含并且管理系统对象的生命周期和配置
  • 在项目中管理事务以及对象的注入
  • Spring 是非侵入式的:基于Spring开发的系统中对象一般不依赖Spring的类
  • 轻量级框架

1.2spring优点

  • 放便解耦,简单开发
  • Aop 编程支持
  • 声明事务的支持
  • 方便程序的测试
  • 方便框架的集合
  • 降低开发难度

2.spring Ioc

2.1什么是 Ioc

  • 控制反转(inversion of control ioc),也称依赖注入(Di),是面向对象编程中的一种设计理念,用来降低代码的耦合度
  • 把new对象的权力给spring

2.2 applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">
beans>

2.3 Ioc需要的jar包

org.springframework spring-core 5.1.8.RELEASE org.springframework spring-context 5.1.8.RELEASE org.springframework spring-beans 5.1.8.RELEASE org.springframework spring-expression 5.1.8.RELEASE commons-logging commons-logging 1.2 log4j log4j 1.2.17

<dependency>
  <groupId>org.springframeworkgroupId>
  <artifactId>spring-aopartifactId>
  <version>5.1.8.RELEASEversion>
dependency>

<dependency>
  <groupId>aopalliancegroupId>
  <artifactId>aopallianceartifactId>
  <version>1.0version>
dependency>

<dependency>
  <groupId>org.aspectjgroupId>
  <artifactId>aspectjweaverartifactId>
  <version>1.9.4version>
dependency>

2.4 bean

<bean id="HelloSpring" class="com.pojo.HelloSpring">
    <property name="who">
        <value>Springvalue>
    property>
bean>

注解定义bean

  • @Pepsoitory:用于标注Dao层
  • @Service:用于标注业务层
  • @Controller:用于标注控制层

2.5 p命名空间注入

<bean id="user" class="com.spring.demo.Person" p:name="zhang" p:age="">
bean>
<bean id="userService" class="com.spring.service.impl.userServiceImpl" p:dao-ref="userDao">
bean>

2.6注入特殊符号

使用

<bean id="user" class="com.spring.demo.Person">
		<property name="name" >
		<value>value>
		property>
bean>

3spring Aop

3.1什么是Aop

aop是一种编程思想,面向切面(Aspect Oriented Programming)

3.2AOP 重要概念

  • Aspect(切面) Aspect 就是对我们横切关注点的抽象和描述,定义了需要对哪些方法或者是类、字段进行拦截和操作。
  • Joinpoint(连接点) 简单点来说就是被拦截的方法或者是字段,通常都是方法。
  • Pointcut(切入点) Pointcut 就是对连接点的描述和定义,比如我们定义的正则式切入点,Spring AOP 通过 Pointcut 获取到我们需要拦截的方法。
  • Advice(通知/增强处理) Advice 对被拦截方法执行前后(包括异常)需要执行的一些操作。
  • Target object(目标对象) 被一个或者多个切面增强的对象
  • AOP proxy(AOP 代理)由aop框架所创建的对象,实现执行增强处理方法和一个切入点表达式关联,并在这个切入点上执行代码逻辑
  • Weaving (织入)将增强处理连接到应用服务器中的类型或者对象上的过程、
  • 增强处理类型:
    • 前置增强
    • 后置增强
    • 异常增强
    • 环绕增强
    • 最终增强

3.3 Aop需要的jar包

<dependency>
      <groupId>org.aspectjgroupId>
      <artifactId>aspectjweaverartifactId>
      <version>1.9.4version>
dependency>

<dependency>
  <groupId>aopalliancegroupId>
  <artifactId>aopallianceartifactId>
  <version>1.0version>
dependency>

<dependency>
  <groupId>org.springframeworkgroupId>
  <artifactId>spring-aopartifactId>
  <version>5.1.9.RELEASEversion>
dependency>

3.4定义切面

public class Mysout {
    public void around(ProceedingJoinPoint pj){
        System.out.println("进入前置");
        Object str=null;
        try {
            str=pj.proceed();
            System.out.println("进入后置");
        }catch (Throwable e){
            System.out.println("进入异常");
        }finally {
            System.out.println("进入最终");
        }
    }
}

3.5 配置切面

<bean id="mysout" class="com.utils.Mysout">bean>

3.6定义切入点

<aop:config>
    <aop:pointcut id="pointCut" expression="execution(* com..*.UserAction.UserFind())"/>
    <aop:aspect  ref="mysout">
        <aop:before method="print" pointcut-ref="pointCut">aop:before>
        <aop:after-throwing method="error" pointcut-ref="pointCut" throwing="ex">aop:after-throwing>
        <aop:around method="around" pointcut-ref="pointCut" >aop:around>
    aop:aspect>
aop:config>

4.spring 注解

4.1 使用注解定义bean

  • @Component

         import org.springframework.stereotype.Component;
    
  • @Repository:用于标注DAO类

    	 import org.springframework.stereotype.Repository;
    
  • @Service:用于标注业务类

		   import org.springframework.stereotype.Controller;
  • @Controller:用于标注控制器类
           import org.springframework.stereotype.Controller;
  • @Component的作用等价于@Repository,@Service,@Controller

4.2使用

  • @Service("orderBiz")
    public class OrderBizImpl implements OrderBiz {
        @Resource(name = "orderMapper")
        private OrderMapper orderMapper;
        @Resource(name = "orderDetailMapper")
        private OrderDetailMapper orderDetailMapper;
    
        @Override
        public List<Order> getOrderList() {
    
            return orderMapper.getOrderList();
        }
    
        @Override
        @Transactional
        public Integer add(Order order, OrderDetail orderDetail) {
            int rst= orderMapper.addOrder(order);
            orderDetail.setOrderId(order.getId());
            rst=orderDetailMapper.addOrderDetail(orderDetail);
            return rst;
        }
    
    
        public OrderMapper getOrderMapper() {
            return orderMapper;
        }
    
        public void setOrderMapper(OrderMapper orderMapper) {
            this.orderMapper = orderMapper;
        }
    
        public OrderDetailMapper getOrderDetailMapper() {
            return orderDetailMapper;
        }
    
        public void setOrderDetailMapper(OrderDetailMapper orderDetailMapper) {
            this.orderDetailMapper = orderDetailMapper;
        }
    }
    
  • @Controller("lbwnb")
    public class OrderControllerImpl implements OrderController{
        @Resource(name = "orderBiz")
        private OrderBiz orderBiz;
    
        @Override
        public List<Order> getOrderList() {
            return orderBiz.getOrderList();
        }
    
        @Override
        public Integer add(Order order, OrderDetail orderDetail) {
            return orderBiz.add(order,orderDetail);
        }
    
        public OrderBiz getOrderBiz() {
            return orderBiz;
        }
    
        public void setOrderBiz(OrderBiz orderBiz) {
            this.orderBiz = orderBiz;
        }
    }
    

4.3. 加载注解定义的Bean


<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:tx="http://www.springframework.org/schema/tx"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       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
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
    <context:component-scan base-package="controller,biz">context:component-scan>
beans>

5.事务

    <tx:annotation-driven transaction-manager="txManager">tx:annotation-driven>

    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="add" propagation="SUPPORTS"/>
        tx:attributes>
    tx:advice>

    <aop:config>
        <aop:pointcut id="pointcut" expression="execution(* biz..*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut">aop:advisor>
    aop:config>

注解使用事务

@Transactional

你可能感兴趣的:(java)