spring基础知识点总结

  • Bean

依赖注入

  1. bean 声明周期
    class :指定创建bean的bean类
    name/id : 标识符,获取bean
    scope:
    - singleton: 默认值,Bean以单例模式存在
    - prototype:每次调用bean时,返回一个新的实例
    - request:
    - session:
    - global-session:

    <bean id="aa" class="com.it.pojo.A"  scope="作用域"/>
    
  2. bean生命周期

    Bean的定义——Bean的初始化——Bean的使用——Bean的销毁
    配置bean的初始化和销毁方法

    <bean id="hou"  class="House"  init-method="init"  destroy-method="destroy"  />
    public clsas House{
    	public void init(){ ... }
    	public void destory(){ ... }
    }
    

    如果太多具有相同名称的初始化或销毁方法的bean

    <beans xmlns="...." 	default-init-method="init"  default-destory-method="dstory" />
    
  3. 构造函数依赖注入

    <bean id="" class="" >
    	<constructor-arg type="int" index="0" value="a">
    	<constructor-arg ref="aa"/>
    </bean>
    <bean id="aa" class="......">
    
     type:类型
     index:构造函数参数的索引类型
     value:值
     ref:引用类型的值
    
  4. property属性注入

    <bean id="" class="" >
    		<property name="aa1" ref="aa"/>
    		<property name="name" value="张三"/>
    </bean>
    <bean id="aa" class="......">
    
    <bean id="test2" class="" >
    		<property name="aa1" ref="aa">
    			//内部bean:和上面的aa没有任何关系 重新定义的一个bean
    			<bean class="com.it.pojo.aa"></bean>
    		</property>
    </bean>
    
     ref:引用类型(引用外部)
     value:值类型
     内部bean:不能通过appliactionContext获取get方法
    

    JAVA中值类型和引用类型的不同(面试常考)

  5. 集合注入

    <bean id="" class="" >
    	//list注入
    	<property name="list">
    		<list>
    				<value>11</value>
    				<value>22</value>
    				//如果是对象
    				<ref bean="aa"/>
    				//或者
    				<bean class="com.it.aa"/>
    		</list>
    	</property>
    	//set注入
    	<property name="sets">
    		<set>
    			<value>11</value>
    		</set>
    	</property>
    	//Map注入
    	<property name="maps">
    		<map>
    			<entry key="1" value="2">
    			<entry key="1" value-ref="aa">
    		</map>
    	</property>
    </bean>
    
  6. util赋值 (了解
    暂无笔记

  7. 级联属性赋值

    <bean id="" class="" >
    			<property name="aa1" ref="aa"/>
    			//改变aa对象的 a属性 (引用类型
    			<property name="aa.a" value="张三"/>
    </bean>
    <bean id="aa" class="......">
    
  8. 继承配置信息

    <bean id="stu1" class="com.it.student" >
    			<property name="name" value="张三"/>
    			<property name="age" value="男"/>
    </bean>
    
    //这个类继承了stu1配置信息 所有属性name="张三" age="男"
    <bean id="stu2" class="com.it.student" parent="stu1" >
    		
    </bean>
    
    
  9. 改变bean的创建顺便 depends-on属性

    //先创建stu2在创建stu1
    <bean id="stu1" class="com.it.student"  depends-on="stu2">
    			<property name="name" value="张三"/>
    			<property name="age" value="男"/>
    </bean>
    
    //这个类继承了stu1配置信息 所有属性name="张三" age="男"
    <bean id="stu2" class="com.it.student" parent="stu1" >
    		
    </bean>
    
    
  10. 通过P命名空间为bean赋值
    4.1 属性赋值 - 给age赋值

    <bean class="com.it.Person" p:age="11"/>
    
  11. 自动装配
    constructor:
    1.先按照类型进行装配,没有直接设为null
    2.如果找到多个根据参数名和id名做比较;找不到就为null
    3.不会报错

    <bean class="com.it.Person" autowire="constructor"/>
    
  12. spring表达式
    spel类似于el 比el强大 可以调用任何值 值的方法
    语法:#{age1}

    静态方法调用
    #{T(全类名).静态方法(1,2)}
    
  • 注解

    1. @Controller:控制器,默认值value="" 定义bean标签id的值

    2. @Repository:Dao

    3. @Component:以上都不是

    4. 组件扫描:

      <context:component-scan base-package="com.it">
      			<!-- [type="annotation"]:排除指定注解的的组件
      							expression=" "   注解的全类名	
      				  [type="assignable"]:排除指定的类
      				  			expression=" "   类的全类名
      			-->
      			<context:exclude-filter type="annotation" expression=""></context:exclude-filter>
      	</context:component-scan>
      
      //需要禁用掉默认规则 use-default-filters="false"	
      <context:component-scan base-package="com.it" use-default-filters="false">
      		<context:include-filter type="annotation" expression=""></context:include-filter>
      </context:component-scan>
      
    5. @Scope(“prototype”) :改变bean的作用域
      指定扫描的基础包,把基础包下所有加了注解的类,自动扫描进ioc容器
      (注意:必须导入aop包

    6. 1 @Autowired: 根据类型实现自动装配
      @Autowired(required=false) :required=false,没有找到也不会报错
      @Qualifier()
      private BookService bookService;

      1. 先按照类型去找对应的组件 bookService=ac.getBean(BookService .class);
      2. 找到一个 赋值
      3. 没找到 报错
      4. 找到多个
        4. 1. 按照变量名和id进行匹配
        4. 2. 按照变量名和id进行匹配
        4. 2. 1. 如果匹配上,赋值
        4. 2. 2. 如果没有匹配上,会报错,可以用Qualifier(“变量名的别名”)

      6.2 @Autowired 给方法上加Autowired

      1. 这个方法会在bean创建的时候自动运行
      2. 这个方法的参数会自动注入
    7. @Resource
      1.resource和autowired区别
      1.1 resource是e j2ee:java的标准,而autowired是spring自己的注解
      1.2 resource的扩展性更强,比如换成ejb容器,依旧可以
      1.3 @Autowired 与@Resource的区别(详细)

    8. @ContextConfigutation(localtion=“classpath:applicationContext.xml”)
      @RunWith(SpringJunit4ClassRunner.class) :指定spring的单元测试模块测试,默认junit
      (注解都写类上面,需要导入spring-test.jar)

      @ContextConfigutation(localtion="classpath:applicationContext.xml")
      @RunWith(SpringJunit4ClassRunner.class)
      public class Test{
      	@Autowired
      	Student stu;
      }
      
  • AOP

     1 AOP:中文名称 面向切面编程
     2.英文名称: (Aspect Oriented Programming)
     3.在一个执行方法前面或后面添加额外的功能就算aop
     	 3.1 不修改原有代码的基础上添加功能
     	 3.2 原有功能相当于释放了部分逻辑,让职责更加明确
    

    1.— AspectJ before 和 after类不需要实现接口

     <bean name="aBean" class="com.it.service.myAop"></bean>
    	<aop:config>
    		//ref代表通知类是哪个类
    		<aop:aspect id="myAspect" ref="aBean">
    			//配置切点
    			<aop:pointcut id="businessService" expression="execution(* com.it.service.*.*(..))" />
    			//环绕
    			<aop:around method="myAround" pointcut-ref="businessService" />
    			<aop:before method="myBefore" pointcut-ref="businessService" />
    			//调用aBean类的myAfer方法 根本xml文件中的先后顺序决定代码的执行顺序
    			//after标签出现异常后依然执行
    			<aop:after method="myAfter" pointcut-ref="businessService" />
    			//方法不出异常才执行
    			<aop:after-returning method="myAfterReturning" pointcut-ref="businessService" />
    			//异常通知
    			<aop:after-throwing method="myExection" pointcut-ref="businessService"  throwing="e" />
    		</aop:aspect>
    	</aop:config>
    

    通知类

    	public class myAop {
    		public void myAfter(){
    			System.out.println("after");
    		}
    		public void myBefore(){
    			System.out.println("before");
    		}
    		public void myThrow(Exception e) {
    			System.out.println(e.getMessage());
    		}
    		public void myAfterReturning(){
    			System.out.println("myAfterReturning");
    		}
    		public void myAround(ProceedingJoinPoint p) throws Throwable{
    			Object proceed = p.proceed();
    		}
    	}
    
     	1 异常通知(AspectJ中写法)
     	2 <aop:aspect>的ref属性表示:方法在哪个类中
     	3 <aop:xxx>表示什么通知
     	4 method 表示触发这个通知会执行哪个方法
     	5 throwing: 异常对象名,必须和通知中方法参数名一致
     	6 同时配置after和afterReturning根据xml文件中的顺便改执行
     	7 同时配置所有(包含环绕)执行顺序是 
     		7.1 环绕前置
     		7.2 前置
     		7.3 方法
     		7.4 环绕后置
     		7.5 后置或后置Rening
    

    2.Scheam-based 实现接口

    	
    	
    	<bean name="myBeforeAdvice" class="com.it.advice.MyBeforeAdvice"/>
    	<bean name="myAfterAdvice" class="com.it.advice.MyAfterAdvice"/>
    	<bean name="myThrow" class="com.it.advice.myThrow"/>
    	<aop:config>
    		<aop:pointcut id="pointcut" expression="execution(* com.it.service.*.*(..))"/>
    		<!-- 需要实现 MethodBeforeAdvice 接口 -->
    		<aop:advisor advice-ref="myBeforeAdvice" pointcut-ref="pointcut"/>
    		<!-- 实现接口 AfterReturningAdvice -->
    		<aop:advisor advice-ref="myAfterAdvice" pointcut-ref="pointcut"/>
    		<!-- 实现接口ThrowsAdvice接口 方法名必须为afterThrowing(Exection ex ) throws Throwable-->
    		<aop:advisor advice-ref="myThrow" pointcut-ref="mypoint" />
    	</aop:config>
    
    import java.lang.reflect.Method;
    import org.springframework.aop.MethodBeforeAdvice;
    public class myBeforeAdvice implements MethodBeforeAdvice{
    
    	@Override
    	public void before(Method arg0, Object[] arg1, Object arg2)
    			throws Throwable {
    		System.out.println("切点对象方法:"+arg0);
    		if(arg1.length>0){
    			System.out.println("参数:"+arg1[0]);
    		}
    		System.out.println("切点放法参数:"+arg1);
    		System.out.println("哪个对象对用的方法:"+arg2);
    		
    	}
    }
    
    

    2.1 环绕通知

    public class myAround implements MethodInterceptor{
    	@Override
    	public Object invoke(MethodInvocation arg0) throws Throwable {
    		System.out.println("前置");
    		//1.arg0.proceed()执行方法 
    		//2.proceed返回值
    		Object proceed = arg0.proceed();
    		System.out.println("后置");
    		return null;
    	}
    }
    
    

    配置文件

    <bean name="myaround" class="com.it.service.myAround"></bean>
    	<aop:config>
    		<aop:pointcut expression="execution(* com.it.service.*.*(..))"
    			id="mypoint" />
    		<aop:advisor advice-ref="myaround" pointcut-ref="mypoint"/>
    	</aop:config>
    

    用注解的方式配置aop

    <?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"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       ">
       
       	<context:component-scan base-package="com.it.service"></context:component-scan>
       	<!-- 开启cglib代理 -->
     	<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
    </beans>
    
    package com.it.service;
    
    import org.springframework.stereotype.Component;
    
    @Component
    public class Demo {
    	@Pointcut("execution(* com.it.advice.Demo.demo())")
    	public void demo(){
    		System.out.println("demo");
    	}
    }
    
    
    
     1.spring会自动扫描com.it.service包
     2.@Component 相当于bean
     	2.1如果没有参数 会把类名首字母小写
     	2.2@Component("自定义类名)
    

事务管理

1. 声明式事务

	<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"></bean>
	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="insert"/>
		</tx:attributes>
	</tx:advice>
	<aop:config>
		<aop:pointcut id="pointcut2" expression="execution(* com.it.service.*.*())"/>
		<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut2"/>
	</aop:config>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud是一套用于构建微服务架构的开源框架,而Spring Cloud Alibaba是Spring Cloud的衍生项目,通过集成阿里巴巴的技术栈,提供更多强大的功能和解决方案。 在《Spring Cloud / Alibaba 微服务架构实战》这本书,作者深入浅出地介绍了如何使用Spring Cloud Alibaba构建和部署微服务架构。 首先,作者从微服务的基本概念开始讲解,阐述了微服务架构相对于传统单体架构的优势。然后介绍了Spring Cloud Alibaba的特点和使用场景。 接着,作者详细介绍了微服务架构常用的组件和技术,如服务注册与发现、负载均衡、熔断限流、分布式配置心、消息队列等。通过实际案例,演示了如何使用Spring Cloud Alibaba的组件和技术来构建高可用、可扩展、易维护的微服务应用。 此外,作者还讲解了微服务架构的分布式事务、服务网关、监控和日志等关键问题,并提供了解决方案和最佳实践。 在书的最后,作者总结使用Spring Cloud Alibaba构建微服务架构的几点重要原则和注意事项,帮助读者更好地理解和应用这些技术。 总的来说,这本书详细解释了Spring Cloud Alibaba的各个组件和技术,提供了实战经验和案例,帮助读者从零开始构建微服务架构。无论是初学者还是有一定经验的开发者,都可以从获得宝贵的知识和经验,提升自己在微服务领域的能力。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值