Spring使用大全

  本文主要总结一下spring的所有用法,更多原理篇在后续章节。涵盖内容如下所列:


  • 什么是Ioc/DI、AOP、Spring容器
  • Ioc/DI基于xml、xml和注解混合、以及纯注解的不同实现
  • AOP基于xml、xml和注解混合、以及纯注解的不同实现
  • 声明式事务基于xml、xml和注解混合、以及纯注解的不同实现

  emmm.........That‘s All , let‘s go.


一、spring简介

老规矩,甭管啥,先上图:

Spring使用大全

   Ioc(核心中的核心):Inverse of control,控制反转。对象的创建权利由程序反转给Spring框架。

  DI:Dependency Injection,依赖注入。在Spring框架负责创建对象Bean时,动态的将依赖度的对象注入到Bean组件中!

  AOP:Aspect Oriented Programming,面向切面编程。在不修改目标对象的源代码情况下,增强Ioc容器中Bean的功能。

  Spring容器:指的就是Ioc容器,装载Bean对象的容器。底层就是一个BeanFactory。

二、Ioc、DI

  A、Bean的基础使用方式(无参数版本)

  spring容器默认通过无参构造方法创建Bean对象。

  applicationContext.xml配置(多余的命名空间不必在意o(* ̄︶ ̄*)o):

<?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">
     <!-- id是Bean的名称,class是类全限定名,init-method是初始化对象前执行的方法,destroy-method是销毁对象后执行的方法(容器销毁对象就销毁),还有未罗列的scope代表作用域-->
     <bean id="userService" class="service.UserServiceImpl" init-method="init" destroy-method="destroy" />
</beans>

  Bean类定义如下:

package service;

public class UserServiceImpl implements UserService{
    
    //无参构造
    public UserServiceImpl() {
    }
    
    @Override
    public void saveUser() {
        System.out.println("保存用户成功!");
    }
    public void init() {
        System.out.println("初始化操作");
    }
    public void destroy() {
        System.out.println("销毁操作:释放资源等");
    }
}

测试代码:

@org.junit.Test
    public void xmlTest() {
        //一般使用ApplicationContext,由于需要关闭容器使用了AbstractApplicationContext
//        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        
//        UserService us = (UserService)context.getBean("userService");//也可以通过bean的名称实例化
        UserService us = context.getBean(UserService.class);
    
        us.saveUser();
        context.close();//关闭容器才会销毁对象,测试destroy()方法
    }

  输出结果为: 

初始化操作
保存用户成功!
销毁操作:释放资源等

 B、Bean的基础使用方式(有基础类型参数版本)

配置文件为:

<?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">
     
     <!-- 1、通过构造器注入属性值 -->
     <bean id="userService" class="service.UserServiceImpl" init-method="init" destroy-method="destroy">
         <constructor-arg name="id" value="1" />
         <constructor-arg name="name" value="王麻子"/>
     </bean>
    <!-- 2、通过setter方法注入属性值 
     <bean id="userService" class="service.UserServiceImpl" init-method="init" destroy-method="destroy">
        <property name="id" value="1" />
        <property name="name" value="2" />
     </bean> -->
</beans>

相关推荐