Spring

作者: 我不抽烟zzm | 来源:发表于2018-01-31 01:24 被阅读0次

Spring是什么?

Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用。Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson创建。简单来说,Spring是一个分层的JavaSE/EE轻量级开源框架。

为什么要使用Spring?

Spring是一个基于IOC和AOP的结构J2EE系统的框架
IOC 反转控制 是Spring的基础,Inversion Of Control
简单说就是创建对象由以前的程序员自己new 构造方法来调用,变成了交由Spring创建对象
DI 依赖注入 Dependency Inject. 简单地说就是拿到的对象的属性,已经被注入好相关值了,直接使用即可。

Spring的IOC操作

把对象的创建交给了Spring管理
IOC操作两部分:
(1)IOC的配置文件方式
(2)IOC的注解方式

IOC的底层原理

ioc使用的底层原理技术:
1.XML配置文件
2.dom4J解决XML
3.工厂设计模式
4.反射机制


image.png

IOC入门案例

步骤1.导入Jar包
2.创建类,在类里面创建方法
3.创建Spring配置文件,配置创建类
4.写代码测试对象

Ioc/DI

//创建pojo类
public class Category {
    private int id;
    private String name;
    //省略get和set..
}
//配置applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<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-2.0.xsd">

    <bean id="Category" class="pojo.Category">
        <property name="name" value="设置pojo.Category的name"></property>
    </bean>
</beans>
//编写测试方法
public class Test {

    public static void main(String[] args) {
        ApplicationContext Context =
                new ClassPathXmlApplicationContext("applicationContext.xml");
        Category ContextBean = (Category) Context.getBean("Category");
        System.out.println(ContextBean.getname());
    }
}
//输出:  设置pojo.Category的name

注入对象DI

//根据上面的Category.class
public class Product {

    private int id;
    private String name;
    private Category category;
    //get set..
<bean id="Product" class="pojo.Product">
        <property name="name" value="设置name值"/> 
        <property name="category" ref="Category"/> //ref注入另一个对象
</bean>
//编写测试方法
 ApplicationContext Context =
                new ClassPathXmlApplicationContext("applicationContext.xml");
        Product ContextBean = (Product) Context.getBean("Product");
        System.out.println(ContextBean.getName());
        System.out.println(ContextBean.getCategory().getname()); //得到Category的name值

注解方式IOC/DI

使用注解方式需要在applicationContext.xml中添加<context:annotation-config/>

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!--注解驱动-->
    <context:annotation-config/>

    <bean id="Category" class="pojo.Category">
        <property name="name" value="设置pojo.Category的name"/>
    </bean>

    <bean id="Product" class="pojo.Product">
        <property name="name" value="设置name值"/>
        <!--<property name="category" ref="Category"/>-->
    </bean>

</beans>
//在Product上加入自动注入 @Autowired
    @Autowired   //自动注入
    private Category category;

如果有很多<bean>配置 就需要使用扫描包的方式
<context:component-scan base-package="包 名"/>

//applicationContext.xml
    <!--注解驱动-->
    <context:annotation-config/>
    <!--扫描pojo包-->
    <context:component-scan base-package="pojo"/>

其他注解

1.@Component
比如在Category类上加@Component("C")即表明此类是bean id="C"
2.@Resource
@Resource(name = "arg1")定义了name属性的值, 就只按照name值匹配
@Resource根据属性名匹配, 如果匹配不到就进行type匹配

@Resource("name=c") //== <property name="c"/>
private Category category;

什么是AOP?

AOP: (Aspect Oriented Programming) 面向切面编程。是目前软件开发中的一个热点。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。主要的功能是:日志记录,性能统计,安全控制,事务处理,异常处理等等。

·
·

底层原理

原始纵向方式:

image.png
Aop横向方式
image.png

AOP的操作术语

image.png

AspectJ简介

AspectJ是一个面向切面框架。不是Spring的一部分。它和Spring一起使用进行Aop的操作。Spring2.0之后新增对AspectJ切点表达式支持。

使用aspectJ实现AOP的两种操作:

1.基于aspectj的XML配置
2.基于aspectJ的注解方式

使用表达式配置切入点

 execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)
(1)expression="execution(* aop.Book.add(..))
(2)expression="execution(* aop.Book.*(..))
(3)expression="execution(* *.*(..))

AspectJ的aop操作 XML方式

//package aop;

//切入点
public class Book {
    public void add(){
    System.out.println("add....");
    }
}
//  通知\增强
public class Books {
    public void before(){
        System.out.println("前置增强....");
    }
}
//applicationContext.xml

    <bean id="B" class="aop.Book"/>
    <bean id="Bs" class="aop.Books"/>

<!--配置aop操作-->
    <aop:config>
        <!--1 配置切入点-->
        <aop:pointcut id="pointcut1" expression="execution(* aop.Book.add(..))"/>
        <!--2 配置切面-->
        <aop:aspect ref="Bs">
            <!--配置增强类型:
            method:增强类里面使用哪个方法最为前置
            pointcut-ref:配置切入点
            -->
            <aop:before method="before" pointcut-ref="pointcut1"/>
        </aop:aspect>
    </aop:config>
//Test测试类
public class Test {
    public static void main(String[] args) {
        ApplicationContext Context =
                new ClassPathXmlApplicationContext("applicationContext.xml");
        Book ContextBean = (Book) Context.getBean("B");
        ContextBean.add();
    }
}

输出:  前置增强....
        add....

其他通知

//环绕通知
public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("环绕前置");
        proceedingJoinPoint.proceed();
        System.out.println("环绕后置");
    }

//applicationcontext.xml
//添加aop
 <aop:around method="around" pointcut-ref="pointcut1"/>

aop注解方式

//切入点
@Component("B")  //生成Bean
public class Book {
    public void add(){
        System.out.println("add....");
    }
}

@Aspect  //注解表示这是一个切面
@Component
public class Books {
    public void before(){
        System.out.println("前置增强....");
    }

    @Around(value = "execution(* aop.Book.add(..))")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("环绕前置");
        proceedingJoinPoint.proceed();
        System.out.println("环绕后置");
    }
}

//applicationcontext.xml

    <aop:aspectj-autoproxy/>

//Test测试类
public class Test {
    public static void main(String[] args) {
        ApplicationContext Context =
                new ClassPathXmlApplicationContext("applicationContext.xml");
        Book ContextBean = (Book) Context.getBean("B");
        ContextBean.add();
    }
}

Spring 注解方式测试

注解方式用到junit-4.12.jar和hamcrest-all-1.3.jar

  1. @RunWith(SpringJUnit4ClassRunner.class) 表示这是一个Spring的测试类
  2. @ContextConfiguration("classpath:applicationContext.xml")定位Spring的配置文件
  3. @Autowired给这个测试类装配Category对象
  4. @Test测试逻辑
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Test1 {

    @Autowired
    Book book;

    @Test
    public void test() {
        book.add();
    }
}

相关文章

网友评论

    本文标题:Spring

    本文链接:https://www.haomeiwen.com/subject/ouafzxtx.html