Spring基础笔记(萌新专用)

俗话说的好:万事开头难,做什么都需要坚持,学习也是一样,都要从基础做起,基础不牢,地动山摇,基础打牢了,我们才好进一步的发展!

一、第一章《Spring入门IOC、DI》

1:spring 春天 轻量级容器框架
作者:Rod Johnson
理论:轮子理论 (不需要重复生产轮子)
IT: 直接使用写好的代码

2:spring 框架宗旨
不重复开发的新技术,但是现有的技术使用起来更方便。

3:spring核心功能
IOC | DI
AOP
声明式事务

4:spring运行时环境(功能)
test
Core Container:核心容器 (spring运行的基本条件)
beans :对象管理
core
Context
SPEL

5: Spring 看成一个大容器
spring管理所有的内容,都ApplicationContext对象获取内容。
spring3以后版本将spring功能拆成了多个jar。

6: IOC 控制反转 Inversion of Control
原先由程序员通过new实例化对象事情,转交给spring负责。
控制反转中控制:控制类对象
控制反转中反转:转交给spring
作用:解耦
解除程序员和对象管理之间的耦合。

Student stu=new Student();
Service {
StudentDao dao=new StudentMyBatiesDao();
}

7:搭建Spring环境
1:导入基本Spring的jar包
commons-logging.jar 日志记录包 log4j.properties
spring-beans-xxx.jar
spring-core-xxx.jar
spring-context-xxx.jar
spring-expression-xxx.jar

2: 创建配置文件src–>xml–>applicationContext.xml (文件名自己取)

    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    </beans> 

8:配置文件

   <bean id="mysch" class="entity.School"> </bean>

id:获取对象的标识
class=创建类的对象
默认情况下:加载配置文件,创建xml文件中所有的对象 ,默认调用无参构造
设置:延迟创建对象,获取对象,才创建
元素上加上 default-lazy-init="true"属性
元素上加上 lazy-init="true"属性
默认情况下:单例模式 (不管获取多少次都是一个)
设置:scope属性
singleton:单例模式
prototype:多例模式

9:从容器中获取对象
ApplicationContext ac=new ClassPathXmlApplicationContext(“applicationContext.xml”);
School mysch=(School)ac.getBean(“mysch”);

10:Spring创建对象的三种方式
1:调用构造方法创建对象
无参构造 (默认)
有参构造(指定构造方法参数)

<bean scope="prototype" id="mysch" class="entity.School"> 
      <constructor-arg index="0" type="" name="" value="10010"></constructor-arg>
      <constructor-arg index="1" value="长沙新途"></constructor-arg>
</bean>   
 index:根据下标,从0开始
 type: 根据类型
 name: 根据名称
 value:基本类型和String类型赋值
 ref: 引用另一个对象

2:实例工厂
工厂模式: 创建对象,一个工厂创建多个对象
先创建工厂,再实例化对象。
/**
* 创建Person对象的工厂类
*/
public class PersonFactory {
//创建Person对象的方法
public Person newInstance() {
return new Person();
}
}

   <!-- 配工厂的bean -->
   <bean  id="pf" class="entity.PersonFactory"> </bean>
   <bean id="person" factory-bean="pf" factory-method="newInstance"></bean>

3:静态工厂
不需要创建工厂,快速实例化对象。
public class PersonFactory {
//创建Person对象的方法
public static Person newInstance() {
return new Person();
}
}

  <bean id="person" class="entity.PersonFactory" factory-method="newInstance"></bean>

10: 给Bean的属性赋值
1:构造方法赋值
2:设置注入(调用set方法)
设置基本类型和String类型

   <bean id="pojo" class="entity.Pojo">
      <!-- set方法给属性赋值  name:名称是set方法的setXXX名称-->
      <property name="id" value="250"></property>
      <property name="name" value="小新"></property>
  </bean> 
  等价于:
  <bean id="pojo2" class="entity.Pojo">
   <property name="id" >
   		<value>250</value>
   </property>
   <property name="name" >
       <value>小新</value>
   </property>
</bean> 
 数组:
  <property name="arrs">
       <array>
          <value>小崔</value>
          <value>小庆</value>

       </array>
   </property>
 Set:
 <property name="sets">
      <set>
         <value>1</value>
         <value>2</value>
      </set>
   </property>
List:
   <property name="sets">
      <list>
         <value>1</value>
         <value>2</value>
      </list>
   </property>
Map:
 <property name="map">
      <map>
         <entry key="a" value="A"></entry>
         <entry key="b" value="B"></entry>
      </map>
  </property>
pros:属性配置文件
<property name="pro">
      <props>
         <prop key="userName">scott</prop>
         <prop key="userPwd">tiger</prop>
      </props>
</property>

6:DI 依赖注入
IOC和DI 一样
在一个类对象(A)中需要依赖另一个对象(B),
将B赋值给A的过程,就叫依赖注入。

第二章《动态代理(jdk 和 cglib)》

1:静态代理
优点:目标对象不需要进行修改。
缺点:还是要调用方法,
并且一个代理只适用于一个目标对象。

思路: 真实业务和代理业务分离, 代理作为一个独立的服务业务。
适用于所有的接口。

2:动态代理
jdk动态代理
1:目标对象必须要实现接口
代理对象和目标对象实现的是同一个接口
2: 实现InvocationHandler接口

cglib动态代理
1:目标对象可以实现接口,也可以不实现接口
2:需要导入jar包 cglib-nodep-2.1_3.jar
3: cglib的代理对象是目标对象的子类。
4:实现MethodInterceptor接口

第三章《AOP》

1:AOP 面向切面编程 (Aspect Oriented Programming)
正常程序执行流程:纵向执行流程
在原来的纵向执行过程中,针对于一个方法或一些方法添加通知,
形成横向切面的过程。

好处:
1:扩展性
2:功能明确(通知分走部分功能)

2:常用概念
切点: 原有功能 pointcut (demo2方法)
前置通知:在切点之前执行 before advice
后置通知:在切点之后执行 after advice
异常通知:在切点出现异常时才会触发 throw advice
切面:所有功能总称为切面
织入:将切面嵌入到原有功能

3:Spring提供AOP实现方式
1:Schema-base 方式
每一个通知必须实现接口和类
配置文件中的aop:config中配置
2:AspectJ 基于xml方式
每一个通知不需要实现接口和类
配置文件中的aop:config中的aop:aspect子节点中配置
3:AspectJ 基于注解方式

4:搭建Aop环境
1:导入jar包
beans core context expression
commons-log log4j
aop aspects tx aopalliance aspectjweaver
cglib

2:配置文件
引入aop的命名空间

   <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>

5:AspectJ基于 xml方式实现AOP
通知不需要实现接口和类
1:写一个java类,一个通知就写一个方法

2:配置文件配置

    <bean id="myAdvice" class="advice.MyAdvice"> </bean>  
    <!-- 配置AOP -->
    <aop:config >
        <!--切面 -->
        <aop:aspect ref="myAdvice">
            <!-- 切点 -->
            <aop:pointcut expression="execution(* demo.Demo.demo2())" id="mypoint"/>
            <!-- 前置通知 -->
            <aop:before method="myBefore" pointcut-ref="mypoint"/>
            <!-- 后置通知 :不管是否出现异常,都执行-->
            <aop:after method="myAfter" pointcut-ref="mypoint"/>
            <!-- 后置通知 :只有正常情况,才会执行-->
            <aop:after-returning method="myAfter2" pointcut-ref="mypoint"/>
            <!--异常通知:只有切点出现异常时,才会执行异常通知-->
            <aop:after-throwing method="myThorw" pointcut-ref="mypoint"/>
            <!-- 环绕通知 -->
            <aop:around method="myArround" pointcut-ref="mypoint"/>        
	</aop:aspect>
    </aop:config>

3:通配符和方法参数
* :匹配任意一个包,任意一个类,任意一个方法
如:execution ( * service..())
… :代表任意参数的方法

4:获取切点参数
execution(* demo.Demo.demo1(String)) and args(name)
public void myBefore(String name) {
System.out.println(“前置通知…”+name);
}

5:配置多个切点多个通知

6:AspectJ基于注解方式实现AOP
@Aspect 切面注解
@Pointcut(execution (* …) ) 切点注解
@Before("…") 前置通知注解
@After("…") 后置通知注解
@AfterReturning("…") 后置通知注解
@AfterThrowing("…") 异常通知
@Around("…") 环绕通知

默认情况下,Spring是不会自动扫描注解,
指定Spring扫描哪些包下可能有注解
1:配置文件加入context的命名空间
2:加上自动扫描和cglib动态代理

<context:component-scan base-package=“advice,demo”></context:component-scan>

<!--设置动态代理为cglib代理   false:jdk动态代理  true:cglib代理-->
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>

第四章《组件装配》

1:自动注入
在Spring的配置文件中,对象属性名和id的ref名称相同时,
省略配置。
采用autowire属性:是否自动装配

1:在上加上autowire="“属性。只对当前bean进行自动装配
2:在上加上default-autowire=”"属性。对所有的bean进行自动装配。
3:autowire 包含的属性值
byName:根据名称自动装配 (set方法名和id名相同)
byType:根据类型自动装配 (相同类型只能有一个,多个会报错)
constructor:根据构造方法自动装配 (必须提供带对象有参构造)
default: 默认装配和上default-autowire的相同
no: 不自动装配

2:Spring常用的注解
1: @Component() 创建对象 等于
默认情况下对象id名为类名(首字母小写)。通过value属性指定id名
2:@Service() 创建对象 等于
加上serviceImpl类上
3:@Repository 创建对象 等于
加上dao层类上
4:@Controller 创建对象 等于
加载前端控制器类上
5:注入注解
@Resource jdk提供注解 不需要get/set方法
根据byName名称自动装配,如果id名和set方法名相同,就自动装配。
如果如果id名和set方法名没有找到相同,根据byType类型自动装配
@Resource(name = “student”) 最好指定名称
6:@Autowired spring提供注解 不需要get/set方法
根据byType类型自动装配
7: @Aspect 切面注解 加在通知类上
8: @Pointcut 切点
9: @Before 前置
10:@After 后置
11:@AfterReturning 后置
12:@AfterThrowing 异常
13:@Around 环绕
14:@Value() 获取配置文件的值

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值