Spring基础配置----依赖注入&AOP

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/feng9603/article/details/82782404

引言

spring框架本身有四大原则:

  1. 使用POJO进行轻量级和最小侵入式开发
  2. 通过依赖注入和基于接口变成实现松耦合
  3. 通过AOP和默认习惯进行声明式编程
  4. 使用AOP和模板(template)减少模式化代码

Spring所有功能的设计和实现都是基于此四大原则。

一、依赖注入

 我们将常说的控制反转(Inversion of Control-IOC)和依赖注入(dependency injection-DI)在Spring环境下是等同的概念,控制反转是通过依赖注入实现的。所谓的依赖注入是指容器负责创建对象和维护对象间的依赖关系,而不是通过对象本身负责自己的创建和解决自己的依赖。

依赖注入起到的作用就是将对象之间的依赖关系从原先的代码中解耦出来,通过配置文件或注解等方式加上Spring框架的处理让我们对依赖关系灵活集中的进行管理。依赖注入的主要目的是为了解耦,体现的一种“组合”的概念。如果你希望你的类具备某项功能的时候,是继承自一个具有此功能的父类好呢?还是组合另外一个具有这个功能的类好?答案是肯定的,如果说你继承一个父类,子类将于父类耦合,组合另外一个类则是耦合度大大降低。

Spring Ioc 容器(ApplicationContext)负责创建 Bean ,并通过容器将功能类的 bean 注入到你需要的bean 中。Spring 提供使用 xml 、注解、java配置实现 Bean 的创建和注入。

无论是xml配置、注解配置、java配置,都称之为配置元数据,所谓元数据即描述数据的数据。元数据本身不具备任何执行的能力,只能通过外界的代码来对这些元数据解析后进行相应的操作。Spring 容器解析这些配置元数据进行 Bean 初始化、配置和管理依赖。

声明 Bean 的注解:

  • @Component组件,没有明确的角色
  • @Service 在业务逻辑层(service层)使用
  • @Repository 在数据访问层(dao层)使用
  • @Controller 在展现层(MVC--->Spring MVC)使用

注入Bean的注解,一般情况下通用

  • @Autowired:Spring提供的注解
  • @Inject:JSR-330提供的注释
  • @Resource:JSR-250提供的注解

示例

1)编写功能类的bean。

package com.wsf.spring;

import org.springframework.stereotype.Service;

/**
 * @author feng
 * @create 2018-09-20 14:00
 */
@Service
public class TestService {
    public String sayHello (String world) {
        return "Hello " + world ;
    }
}

代码解释:

使用@Service 注解声明当前TestService类是Spring管理的一个类。

2)使用功能类的 Bean。

package com.wsf.spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author feng
 * @create 2018-09-20 14:04
 */
@Service
public class UseTestService {

    @Autowired
    TestService testService;

    public String sayHello (String word) {
        return testService.sayHello(word);
    }
}

代码解释:

使用@Service 注解声明当前UseTestService 类是Spring 管理的一个Bean。

使用@Autowired 将TestService 的实体 Bean注入到 UseTestService 中,让UseTestService 具备 TestService 的功能。

3)配置类

package com.wsf.spring;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * @author feng
 * @create 2018-09-20 14:10
 */
@Configuration
@ComponentScan("com.wsf.spring")
public class MyConfig {
}

代码解释:

@Configuration 声明当前类是一个配置类。

@ComponentScan 自动扫描包名下所有使用@Service、@Component、@Repository和@Controller 的类,并注册为Bean。

4)运行

import com.wsf.spring.MyConfig;
import com.wsf.spring.UseTestService;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @author feng
 * @create 2018-09-20 14:12
 */
public class MyTest {

    AnnotationConfigApplicationContext context =
            new AnnotationConfigApplicationContext(MyConfig.class);

    UseTestService useTestService = context.getBean(UseTestService.class);

    @Test
    public void testSpringIOC(){
        System.out.println(useTestService.sayHello("Spring"));
        context.close();
    }
}

代码解释:

使用AnnotationConfigApplicationContext 作为 Spring 容器 ,接受输入一个配置类作为参数;

获得声明配置的 UseTestService 的Bean。

5)运行结果

Hello Spring

 二、AOP

AOP:面向切面编程。Spring的AOP的存在是为了解耦。AOP可以让一组类共享相同的行为。

Spring支持AspectJ的注解式切面编程。

1)使用@Aspect声明是一个切面

2)  使用@After、@Before、@Around 定义建言(advice),可直接将拦截规则(切点)作为参数。

3)其中@After、@Before、@Around参数的拦截规则为切点(PointCut),为了使切点服用,可使用@PointCut专门定义拦截规则,然后再@After、@Before、@Around 的参数中调用。

4)其中符合条件的每一个被拦截处为连接点(JoinPoint)。

示例

1)添加Spring aop支持和AspectJ依赖

2)编写拦截规则的注解。

package com.wsf.spring.aop;

import java.lang.annotation.*;

/**
 * @author feng
 * @Description:TODO
 * @create 2018-09-20 16:36
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Action {
    String name();
}

代码解释:

注解本身是没有功能的,和xml一样。注解和xml都是一种元数据,元数据即解释数据的数据,这就是所谓配置。

注解的功能来自用这个注解的地方。

3)编写使用注解的被拦截类

package com.wsf.spring.aop;

import org.springframework.stereotype.Service;

/**
 * @author feng
 * @Description:TODO
 * @create 2018-09-20 16:38
 */
@Service
public class DemoAnnotationService {
    @Action(name="注解式拦截的add操作")
    public void add(){}
}

4)编写使用方法的被拦截类

package com.wsf.spring.aop;

import org.springframework.stereotype.Service;

/**
 * @author feng
 * @Description:编写使用方法规则被拦截类
 * @create 2018-09-20 16:38
 */
@Service
public class DemoMethodService {
    public void add(){}
}

5)编写切面

package com.wsf.spring.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @author feng
 * @Description:TODO
 * @create 2018-09-20 16:41
 */
@Aspect
@Component
public class LogAscept {

    @Pointcut("@annotation(com.wsf.spring.aop.Action)")
    public void annotationPointCut(){};

    @After("annotationPointCut()")
    public void after (JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        Action action = method.getAnnotation(Action.class);
        System.out.println("注解式拦截 " + action.name());
    }

    @Before("execution(* com.wsf.spring.aop.DemoMethodService.*(..))")
    public void before (JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        System.out.println("方法规则式拦截 " + method.getName());
    }

}

代码解释:

通过@Aspect注解声明一个切面。

通过@Component让此切面成为Spring容器管理的Bean。

通过@PointCut注解声明切点。

通过@After注解声明一个建言,并使用@PointCut定义的切点。

通过反射可获得注解上的属性,然后做日志记录相关的操作。

通过@Before 直接声明一个建言,此建言直接使用拦截规则作为参数。

6)配置类

package com.wsf.spring.aop;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

/**
 * @author feng
 * @Description:TODO
 * @create 2018-09-20 16:54
 */
@Configuration
@ComponentScan("com.wsf.spring.aop")
@EnableAspectJAutoProxy
public class AopConfig {
}

代码解释:使用@EnableAspectJAutoProxy注解开启Spring对AspectJ代理的支持。

7)运行

import com.wsf.spring.aop.AopConfig;
import com.wsf.spring.aop.DemoAnnotationService;
import com.wsf.spring.aop.DemoMethodService;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @author feng
 * @create 2018-09-20 14:12
 */
public class AOPTest {

    AnnotationConfigApplicationContext context =
            new AnnotationConfigApplicationContext(AopConfig.class);
    DemoAnnotationService demoAnnotationService = context.getBean(DemoAnnotationService.class);
    DemoMethodService demoMethodService = context.getBean(DemoMethodService.class);

    /**
     * 测试AOP
     */
    @Test
    public void testSpringAOP(){
        demoAnnotationService.add();
        demoMethodService.add();
        context.close();
    }
    
}

运行结果:

注解式拦截 注解式拦截的add操作
方法规则式拦截 add

猜你喜欢

转载自blog.csdn.net/feng9603/article/details/82782404