Spring快速学习入门

Spring-Ioc

Ioc(控制反转)和DI(依赖注入)

Ioc控制反转指的是,通过Spring对象工厂完成对象的创建
DI依赖注入,在Spring完成对象创建的同时依赖Spring容器完成对象属性的赋值
Ioc包含DI
我们可以通过Spring容器进行创建对象和为对象赋初值。

Ioc

当我们需要通过Spring对象工厂对创建某个类的对象的时候,需要将这个交给Spring管理–通过bean配置

<!-- 通过bean将实体类配置给Spring进行管理 id表示实体类的唯一标识-->
       <bean id="student" class="com.lxr.ioc.domain.Student"></bean>

DI

通过Spring容器给创建的对象赋值

<bean id="student" class="com.lxr.ioc.domain.Student"><!--//Ioc-->
              <!--DI-->
              <property name="stuNum" value="10002" />
              <property name="stuName" value="李四" />
              <property name="stuAge" value="21" />
              <!--ref表示从Spring容器拿到id=date的对象,赋值给enterence-->
              <property name="enterenceTime" ref="date"/>
       </bean>
       <bean id="date" class="java.util.Date"/>

依赖注入方式

Spring容器加载配置文件后,通过反射创建类的对象,并给属性赋值 Spring容器通过反射实现注入有三种方式:set方法,构造器注入和接口注入

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

当Spring容器进行初始化的时候,就会加载并解析这个配置文件(dom4j):把配置文件中的内容读取出来,存放的一个Java对象中

Stirng classPath = "com.lxr.ioc.domain.Student";
//通过路径得到类
Class<?> c = Class.forName(classPath);
//通过反射创建对象
Object obk = c.newInstance()        ;
//通过反射获取类中的属性
Field[] fiels = c.getDeclaredFields();
for(Field f: fiels){
          String fieldName = f.getName();
          String steMethodname = "set" + fieldName.subString(0,1).toUpperCase() + filedName.subString(1);
          if("stuNum".equals(fieldName)){
             //getType获取当前实例的运行类型
             Method setMethod = c.getDeclaredMethod(setMethodName,f.getType());
             setMethod.invoke(obj,"10001");
		  }
}
这样就得到了student的stuNum的值10001;
set注入

在bean标签中通过配置property标签给属性赋值,实际上就是通过反射调用set方法完成属性的注入

简单类型及字符串

直接通过property标签的value属性赋值

给日期赋值

方式1:在property标签中通过ref引用Spring容器中的对象

<bean id="student" class="com.lxr.ioc.domain.Student"><!--//Ioc-->
              <!--DI-->
              <property name="stuNum" value="10002" />
              <property name="stuName" value="李四" />
              <property name="stuAge" value="21" />
              <!--ref表示从Spring容器拿到id=date的对象,赋值给enterence-->
              <property name="enterenceTime" ref="date"/>
       </bean>
       <bean id="date" class="java.util.Date"/>

方式2:在property标签中添加子标签bean来指定对象

<bean id="student" class="com.lxr.ioc.domain.Student">
              <property name="enterenceTime">
                     <bean class="java.util.Date"/>
              </property>
       </bean>
自定义对象类对象属性

和日期类赋值一致

集合类对象属性
List:
     里面是简单类型或字符串,直接赋值即可
     <property name="hobbies" value="旅游,电影" />
     如果List里面是对象类型
     方法一:
     <property name="hobbies" >
          <list>
                 <bean class="com.lxr.ioc.domain.Book" />
                 <bean class="com.lxr.ioc.domain.Book" />
                 <bean class="com.lxr.ioc.domain.Book" />
          </list>
     </propery>
     方法二:
     <bean id="book" class="com.lxr.ioc.domain.Book"></bean>
     <property name="hobbies" >
          <list>
                 <ref bean="book"></ref>
                 <ref bean="book"></ref>
          </list>
     </propery>
Set:
      和List一样只是<list>换成<set>
Map:
       <property name="maps">
                 <map>
                        <entry>
                               <key>
                                      <value>k1</value>
                               </key>
                               <value>v1</value>
                        </entry>
                        <entry>
                               <key>
                                     <value>k2</value>
                               </key>
                               <value>v2</value>
                        </entry>
                 </map>

          </property>

Bean标签范围配置

Scope:指对象的作用范围

取值范围说明
singleton默认值,单例的
prototype多例的
requestWEB项目中,Spring创建一个Bean对象,将对象存入到request域中
sessionWEB项目中,Spring创建一个Bean对象,将对象存入到session域中
global sessionWEB项目中,应用在portlet环境,如果没有portlet环境那么 global Session相当于session
public void test3(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Book book1 = (Book)context.getBean("book");
        Book book2 = (Book)context.getBean("book");
        System.out.println(book1);
        System.out.println(book2);
    }
<bean id="book" class="com.lxr.ioc.domain.Book">
为单例,book1和book2是同一个对象
<bean id="book" class="com.lxr.ioc.domain.Book" scope="prototype">
为多例,多个对象
在bean标签可以通过scope属性指定对象的作用域
scope="singleton" 表示当前bean是单例模式,且默认为饿汉式
bean里写 lazy-init="true" 变为懒汉式
scope="prototype"表示当前bean为非单例模式,每次通过Spring容器获取此bean的对象都会去创建一个新的对象

Bean的声明周期方法

Book类

public void init(){
        //初始化方法:在创建当前类对象时调用的方法,进行一些资源准备工作
        System.out.println("-----init");
    }
    public void destory(){
        //销毁方法,在Spring容器销毁对象时调用此方法,进行一些资源回收性操作
        System.out.println("----destory");
    }
<bean id="book" class="com.lxr.ioc.domain.Book" scope="prototype" init-method="init" 
destroy-method="destory"></bean>
init-method:初始化每次调用是构造器执行之后执行,也就是在创建对象的时候,上面调用两次book1和book2
            只输入一次-----init
destroy-method:单例模式是Spring容器关闭时销毁
                多例模式是由gc回收时销毁
                都在销毁之前执行

自动装配

Spring在实例化当前bean的时候从Spring容器中找到匹配的实例(已有的bean)赋值给当前bean的属性
自动装配的策略有两种:byName 和 byType

autowire自动装配
当对象创建时,这个对象需要某些属性,这个时候先从Spring容器中找,
有没有某个对象可以赋值给他,如果有,不用设置就会自动把值赋给他
byName

<bean id="clazz" class="com.lxr.ioc.domain.Clazz"></bean>
<bean id="student2" class="com.lxr.ioc.domain.Student" autowire="byName"></bean>

byType

<bean id="clazz222" class="com.lxr.ioc.domain.Clazz"></bean>
<bean id="student2" class="com.lxr.ioc.domain.Student" autowire="byName"></bean>

autowire有两个属性可以取
              byName:根据当前bean的属性名在Spirng容器中寻找名字相同的bean的对象,
              如果根据name找到了bean但类型不匹配则抛出异常
                       缺点:当有一个不同类型的bean有和寻找的名字相同的名字,就会报错
              byType:根据当前bean属性类型在Spring容器中寻找匹配对象,
              如果根据类型找到了多个匹配的bean,也会抛出异常

Spring-Ioc使用案例

service中有一个ProductServiceImpl类

public class ProductServiceImpl implements ProductService {
    public void listProducts(){
        System.out.println("查询商品信息");
    }
}

servlet中有一个TestServlet类

public class TestServlet {
    private ProductService productService;

    public ProductService getProductService() {
        return productService;
    }

    public void setProductService(ProductService productService) {
        this.productService = productService;
    }

    public void doGet(){
        doPost();
    };
    public void doPost(){
        productService.listProducts();
    }
}

applicationContext

<bean id="productService" class="com.lxr.ioc.service.Impl.ProductServiceImpl"></bean>

<bean id="testServlet" class="com.lxr.ioc.servlets.TestServlet" autowire="byName">
       </bean>

Spring-Ioc原理

请添加图片描述

Spring-Ioc 基于注解

SpringIoc的使用,需要我们通过XML将类声明给Spring容器进行管理,从而通过Spring工厂完成对象的创建,以及属性值的注入。
Spring除了提供基于XML的配置方式,同时提供了基于注解的配置:直接在实体类中添加注解声明给spring容器管理,以简化开发步骤

Spring配置文件

因为Spring容器初始化时,只会加载applicationContext.xml文件,那么我们在实体类中添加的注解就不会被Spring扫描,所以我们需要在applicationContext.xml中声明Spring的扫描范围,以达到Spring工厂初始化时,扫描带有注解的实体类并完成初始化工作

Ioc常用注解

@Component

Student类

@Component("student")
//如果票Component里面没有给值,默认将类名首字母转小写
public class Student {
    private String stuNum;
    private String stuName;
    private int stuAge;
    private Date enterenceTime;

    public Student() {
    }

    public Student(String stuNum, String stuName, int stuAge, Date enterenceTime) {
        this.stuNum = stuNum;
        this.stuName = stuName;
        this.stuAge = stuAge;
        this.enterenceTime = enterenceTime;
    }

    public void setStuNum(String stuNum) {
        this.stuNum = stuNum;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public void setStuAge(int stuAge) {
        this.stuAge = stuAge;
    }

    public void setEnterenceTime(Date enterenceTime) {
        this.enterenceTime = enterenceTime;
    }
}

applicationContext.xml

       <!--声明使用注解配置-->
       <context:annotation-config/>
       <!--声明Spring工厂注解的扫描范围-->
       <context:component-scan base-package="com.lxr.beans"/>

Test类

@Test
    public void test1(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //通过Spring工厂获取Student对象
        Student student = (Student) context.getBean("student");
        System.out.println(student);
    }
1.类注解,声明此类被Spring容器进行管理,相当于bean标签的作用
2.@Component(value="")  value属性用来指定当前bean的id,相当于bean标签的id属性
    value属性也可以省略,如果省略当前类的id默认为类名首字母改小写
3.@Service,@Controller,@Repository,这三个注解也可以将类声明给Spring管理,他们区别主要是语义上的区别
@Controller 注解主要声明将控制器类配置给Spring管理,例如Servlet
@Service 注解主要声明业务处理类配置给Spring管理,例如Service接口的实现类
@Repository 注解主要声明持久化类配置给Spring管理,DAO接口
@Component 注解是除了以上声明外都使用此注解
@Scope

类注解,用于声明当前类是单例还模式还是非单例模式,相当于bean标签的scope属性
@Scope(“prototype”),表示声明为非单例模式,默认为单例

@Lazy

类注解,用于声明一个单例模式的bean是否为懒汉模式
@Lazy(true)表示声明为懒汉模式

@Scope(value = “singleton”)
@Lazy(true)

@PostConstruct、@PreDestory

@PostConstruct是方法注解,声明一个方法为当前类的初始化方法(在构造器后执行),相当于bean标签的init-method属性

@PostConstruct
public void init(){
    System.out.println("------------------init");
}

@PreDestory是方法注解,声明一个方法为当前类的销毁方法(在对象从容器中释放之前执行),相当于bean标签的destory-method属性

@PreDestroy
public void destory(){
    System.out.println("-----------------destory");
}
@Autowired

@Autowired 属性注解,声明当前属性自动装配默认byType,默认必须(如果没有找到类型与属性类型匹配的bean则抛出异常)

@Autowired
    private Clazz calzz;

还可以方法注解

@Autowired(required = false)
    public void setCalzz(Clazz calzz) {
        this.calzz = calzz;
    }
@Autowired(required = true)默认必须(如果没有找到类型与属性类型匹配的bean则抛出异常)
@Autowired(required = false) 不会抛出异常

根据名字赋值
相当于

  @Autowired
    public void setCalzz(@Qualifier("clazz") Clazz calzz) {
        this.calzz = calzz;
    }
@Resource

属性注解,也用于声明属性自动装配
默认装配方式为byName,如果根据byName没有找到对应的bean,则继续根据byType寻找对应的bean,根据byType依然没有找到bean或者找到不止一个类型匹配的bean则抛出异常

@Resource
private Clazz calzz;

Spring-AOP

AOP,面向切面编程,是一种利用“横切”的技术(底层实现就是动态代理),对原有的业务量逻辑进行拦截,并且可以在这个拦截的横切面上添加特定的业务逻辑,对原有的业务进行增强。
基于动态代理实现在不改变原有业务的情况下,对业务逻辑进行增
请添加图片描述

Spring AOP框架部署

导入依赖
<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.13.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.2.13.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>3.3.0</version>
        </dependency>
创建并配置spring配置
<?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>

AOP配置 基于xml

在DAO的方法前后添加开启事务和提交事务的逻辑

总结

就是在aop:config中声明一个切面类aop:aspect 调用里面的方法把它放在切点aop:pointcut的前面或者后面。

创建一个类,定义要添加的业务逻辑

面向切面编程,就是慢性txManager切面类进行编程
类为txManager

public class TxManager {
    public void begin(){
        System.out.println("--------开启事务");
    }
    public void commit(){
        System.out.println("--------提交事务");
    }
}
配置
<bean id="bookDAO" class="com.lxr.dao.BookDAO"></bean>
<bean id="studentDAO" class="com.lxr.dao.StudentDAO"></bean>

<bean id="txManager" class="com.lxr.utils.TxManager"></bean>
<aop:config>
       <!--声明切入点为BookDAO的update()方法-->
       <aop:pointcut id="book_update" expression="execution(* com.lxr.dao.BookDAO.update())"/>
        <!--*表示所有返回类型-->
       <!--update(..)这两个点表示不管是无参还是有参都可以运行-->
       <aop:pointcut id="book_update3" expression="execution(* com.lxr.dao.BookDAO.update(..))"/>
       <!--声明契入点为bookDAO中所有的方法-->
       <aop:pointcut id="book_update1" expression="execution(* com.lxr.dao.BookDAO.*())"/>
        <!--声明契入点为DAO中所有类的所有的方法-->
       <aop:pointcut id="book_update2" expression="execution(* com.lxr.dao.*.*())"/>
       <!--声明txManager为切面类-->
       <aop:aspect ref="txManager">
              <!--通知-->
              <!--把txManager类中的begin方法放在BookDAO类中的update方法前面  -->
              <aop:before method="begin" pointcut-ref="book_update"></aop:before>
              <aop:after method="commit" pointcut-ref="book_update"></aop:after>
       </aop:aspect>
</aop:config>

Test

@Test
//通过Spring容器获取BookDAO的对象,并调用方法
public void test1(){
     ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
     BookDAO bookDAO = (BookDAO)context.getBean("bookDAO");
     bookDAO.update();
 }

AOP开发步骤

1.创建切面类,在切面类中定义方法
2.将切面类配置给Spring容器
3.声明切入点
4.配置AOP的通知策略
切入点的声明
public class LogManager {

    public void printLog(){
        System.out.println("-------执行:" + new Date());
    }

}
<aop:config>
       <!--使用声明 aop:pointcut 标签声明切入点
       切入点可以是一个方法
       -->
       <aop:pointcut id="book_insert" expression="execution(* com.lxr.dao.BookDAO.insert())"/>
       <!--表示BookDAO中所有无参,无返回值的方法-->
       <aop:pointcut id="book_pc1" expression="execution(void com.lxr.dao.BookDAO.*())"/>

       <!--表示BookDAO中所有无返回值的方法-->
       <aop:pointcut id="book_pc2" expression="execution(void com.lxr.dao.BookDAO.*(..))"/>

       <!--表示BookDAO中所有无参的方法-->
       <aop:pointcut id="book_pc3" expression="execution(* com.lxr.dao.BookDAO.*())"/>

       <!--表示BookDAO中所有方法-->
       <aop:pointcut id="book_pc4" expression="execution(* com.lxr.dao.BookDAO.*(..))"/>

       <!--表示dao包中所有类中的所有方法-->
       <aop:pointcut id="pc5" expression="execution(* com.lxr.dao.*.*(..))"/>

       <!--表示dao包中所有类中的insert方法-->
       <aop:pointcut id="pc6" expression="execution(* com.lxr.dao.*.insert(..))"/>

       <!--表示-->
       <aop:pointcut id="pc7" expression="execution(* *(..))"/>

       <!--将logManager声明为切面类-->
       <aop:aspect ref="logManager">
            <aop:before method="printLog" pointcut-ref="pc7"></aop:before>
       </aop:aspect>

</aop:config>
AOP使用注意事项

如果要使用Spring AOP面向切面编程,调用切入点方法的对象必须通过Spring容器获取.
如果一个类中的方法被声明为切入点并且织入了切点之后,通过Spring容器获取该类对象,实则获取到的是一个代理对象。
如果一个类中的方法没有被声明为切入点,通过Sping容器获取的就是这个类真实创建的对象。

切点的通知策略

AOP通知策略:就是将切面类中的切点方法如何织入到切入点

<bean id="myAspect" class="com.lxr.utils.MyAspect"></bean>
<aop:config>
       <aop:pointcut id="book_insert" expression="execution(* com.lxr.dao.BookDAO.insert())"/>
       <aop:aspect ref="myAspect">
              <!--前置通知,切入到指定切入点之前-->
              <aop:before method="method1" pointcut-ref="book_insert" />
              <!--后置通知,切入到指定切入点之后-->
              <aop:after method="method2" pointcut-ref="book_insert" />
              <!--异常通知,切入点抛出异常之后-->
              <aop:after-throwing method="method3" pointcut-ref="book_insert" />
              <!--方法返回之后,对于一个Java方法而言,return返回值也是方法的一部分因此 方法返回之后 和 方法执行结束是同一个时间点
              所以 after 和 after-returning是根据配置的顺序,决定执行顺序-->
              <aop:after-returning method="method4" pointcut-ref="book_insert" />
              <!--环绕通知-->
              <aop:around method="method5" pointcut-ref="book_insert" />
       </aop:aspect>
</aop:config>
//环绕通知的切点方法,必须遵守如下定义
    //1.必须带有一个参数
    //2.必须有Object类型的返回值
    //3.在前后增强的业务逻辑之间执行Object v = point.proceed();
    //4.最后返回v
    public Object method5(ProceedingJoinPoint point) throws Throwable {
        System.out.println("method5---before");
        Object v = point.proceed();
        System.out.println("method5---after");
        return v;
    };

Spring AOP注解配置

Spring配置文件
<?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:context="http://www.springframework.org/schema/context"
       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/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--声明使用注解配置-->
    <context:annotation-config/>
    <!--声明Spring工厂注解的扫描范围-->
    <context:component-scan base-package="com.lxr.beans"/>
    
    <!--基于注解配置的aop代理-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
Spring注解配置例
//@Component 交给Spring管理
//@Aspect 声明切面类
@Component
@Aspect
public class TxManager {

    //定义切入点
    @Pointcut("execution(* com.lxr.dao.BookDAO.*(..)")
    public void pc1(){}

    @Before("pc1")
    public void begin(){
        System.out.println("--------开启事务");
    }
    @After("execution(* com.lxr.dao.BookDAO.*(..)")
    public void commit(){
        System.out.println("--------提交事务");
    }
    
    @Around
    public void printExecuteTime(ProceedingJoinPoint point){
        long time1 = System.currentTimeMillis();
        Object v = point.proceed();
        long time2 = System.currentTimeMillis();
        System.out.print(time2 - time1);
        return v;
    }
    
}
注意

注解配置虽然方便,但是只能在源码上添加注解,因此我们的自定义类提倡使用注解配置,但是如果使用到第三方提供的类,则需要通过xml配置形式完成配置

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值