Spring Boot实战(一)Spring基础 1.3 Spring基础配置

Spring框架本身有四大原则:
1)使用POJO进行轻量级和最小侵入式开发。
2)通过依赖注入和基于接口编程实现松耦合。
3)通过AOP和默认习惯进行声明式编程。
4)使用AOP和模板(template)减少模式化代码。
Spring所有功能的设计和实现都是基于此四大原则的。
1.3.1 依赖注入
1.点睛
我们经常说的控制翻转(Inversion of Control-IOC)和依赖注入(dependency injection-DI)在Spring环境下是等同的概念,控制翻转是通过依赖注入实现的。所谓依赖注入指的是容器负责创建对象和维护对象间的依赖关系,而不是通过对象本身负责自己的创建和解决自己的依赖。
依赖注入的主要目的是为了解耦,体现了一种“组合”的理念。如果你希望你的类具备某项功能的时候,是继承自一个具有此功能的父类好呢?还是组合别外一个具有这个功能的类好呢?答案是不言而喻的,继承一个父类,子类将与父类耦合,组合别外一个类则使耦合度大大降低。
Spring IoC容器(ApplicationContext)负责创建Bean,并通过容器将功能类Bean注入到你需要的Bean中。Spring提供使用xml、注解、Java配置、groovy配置实现Bean的创建和注入。
无论是xml配置、注解配置还是Java配置,都被称为配置元数据,所谓元数据即描述数据的数据。元数据本身不具备任何可执行的能力,只能通过外界代码还对这些元数据行解析后进行一些有意义的操作。Spring容器解析这些配置元数据进行Bean初始化、配置和管理依赖。
声明Bean的注解:
@Component组件,没有明确的角色。
@Service在业务逻辑层(service层)使用。
@Repository在数据访问层(dao层)使用。
@Controller在展现层(MVC-Spring MVC)使用。
注入Bean的注解,一般情况下通过
@Autowired:Spring提供的注解。
@Inject:JSR-330提供的注解。
@Resource:JSR-250提供的注解。

@Autowired、@Inject、@Resource可注解在set方法上或者属性上,笔者习惯注解在属性上,优点是代码更少、层次更清晰。
在本节演示基于注解的Bean的初始化和依赖注入,Spring容器类选用AnnotationConfigApplicationContext
2.示例
(1)编写功能类的Bean
在这里插入图片描述
代码解释
①使用@Service注解声明当前FunctionService类是Spring管理的一个Bean。其中,使用@Component、@Service、@Repository和@Controller是等效的,可根据需要选用。
(2)使用功能类的Bean。

package com.wisely.highlight_spring4.ch1.di;

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

@Service  //使用@Service注解声明当前UseFunctionService类是Spring管理的一个Bean。
public class UseFunctionService {
	@Autowired   //使用@Autowired将FunctionService的实体Bean注入到UseFunctionService中,让UserFunctionService具备FunctionService的功能,此处使用JSR-330的@Inject注解或者JSR-250的@Resources注解是等效的
	FunctionService functionService;
	
	public String SayHello(String word){
		return functionService.sayHello(word);
	}
}

(3)配置类。

package com.wisely.highlight_spring4.ch1.di;

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

@Configuration  //@Configuration声明当前类是一个配置类,在后面1.3.2节的Java配置中有更详细的说明
@ComponentScan("com.wisely.highlight_spring4.ch1.di") //使用@ComponentScan,自动扫描包下所有使用@Service、@Component、@Repository和@Controller的类,并注册为Bean。
public class DiConfig {

}

(4)运行

package com.wisely.highlight_spring4.ch1.di;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
	public static void main(String[] args) {
		AnnotationConfigApplicationContext context = 
				new AnnotationConfigApplicationContext(DiConfig.class);//接受输入一个配置类作为参数
		UseFunctionService useFunctionService = 
				context.getBean(UseFunctionService.class);//获得声明配置的UseFunctionService的Bean
		
		System.out.println(useFunctionService.SayHello("helloworld"));
		
		context.close();
	}

}

结果如图
在这里插入图片描述
1.3.2 Java配置
1.点睛
Java配置是Spring 4.x推荐的配置方式,可以完全替代xml配置;Java配置也是Spring Boot推荐的配置方式。
Java配置是通过@Configuration和@Bean来实现的。
@Configuration声明当前类是一个配置类,相当于一个Spring配置的xml文件。
@Bean注解在方法上,声明当前方法的返回值为一个Bean。
本文通篇使用Java配置和注解混合配置。何时使用Java配置或者注解配置呢?我们主要的原则上:全局配置使用Java配置(如数据库相关配置、MVC相关配置),业务Bean的配置使用注解配置(@Service、@Component、@Repository、@Controller)
2.示例
(1)编写功能类的Bean

package com.wisely.highlight_spring4.ch1.javaconfig;
//此处没有使用@Service声明Bean
public class FunctionService {
	public String saayHello(String word){
		return "Hello " + word + " !";
	}

}

(2)使用功能类的Bean

package com.wisely.highlight_spring4.ch1.javaconfig;
//此处没有使用@Service声明Bean
public class UseFunctionService {
	//此处没有使用@Autowired注解注入Bean
	FunctionService functionService;
	
	public void setFunctionService(FunctionService functionService){
		this.functionService=functionService;
	}
	
	public String SayHello(String word){
		return functionService.saayHello(word);
	}

}

(3)配置类。

package com.wisely.highlight_spring4.ch1.javaconfig;

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

@Configuration  //使用#Configuration注解表明当前类是一个配置类,这意味着这个类里可能有0个或者多个@Bean注解,此处没有使用包扫描,是因为所有的Bean都在此类中定义了。
public class JavaConfig {
	@Bean //使用@Bean注解声明当前方法FunctionServcie的返回值是一个Bean,Bean的名称是方法名
	public FunctionService FunctionService(){
		return new FunctionService();
	}
	@Bean
	public UseFunctionService useFunctionService(){
		UseFunctionService useFunctionService = new UseFunctionService();
		useFunctionService.setFunctionService(FunctionService());//注入FunctionService的Bean时候直接调用functionService();
		return useFunctionService;
		
	}
	

}

(4)运行

package com.wisely.highlight_spring4.ch1.javaconfig;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
	public static void main(String[] args) {
		AnnotationConfigApplicationContext context = 
				new AnnotationConfigApplicationContext(JavaConfig.class);
		
		UseFunctionService useFunctionService = 
				context.getBean(UseFunctionService.class);
		System.out.println(useFunctionService.SayHello("java config"));
		
		context.close();
	}
}

结果如图
在这里插入图片描述

1.3.3 AOP
1.点睛

AOP:面向切面编程,相对于OOP面向对象编程。
Spring的AOP的存在目的是为了解耦。AOP可以让一组类共享相同的行为。在OOP中只能通过继承类和实现接口,来使代码的耦合度增强,且类继承只能为单继承,阻碍更多行为添加到一组类上,AOP弥补了OOP的不足。
Spring支持AspectJ的注解式切面编程。
(1)使用@AspectJ声明是一个切面
(2)使用@After、@Before、@Around定义建言(advice),可直接将拦截规则(切点)作为参数。
(3)其中@After、@Before、@Around参数的拦截规则为切点(PointCut),为了使切点复用,可使用@PointCut专门定义拦截规则,然后在@After、@Before、@Around的参数中调用。
(4)其中符合条件的每一个被拦截处为连接点(JoinPoint)。
本节示例将演示基于注解拦截和基于方法规则拦截的两种方式,演示一种模拟记录操作的日志系统的实现。其中注解式拦截能够很好地控制要拦截的粒度和获得更丰富的信息,Spring本身在事务处理(@Transcational)和数据缓存(@Cacheable等)上面都使用此种形式的拦截。
2.示例
(1)添加spring aop支持及AspectJ依赖

    <!-- spring aop支持 -->
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-aop</artifactId>
    	<version>4.1.6.RELEASE</version>
    </dependency>
    <!-- aspectj 支持 -->
    <dependency>
    	<groupId>org.aspectj</groupId>
    	<artifactId>aspectjrt</artifactId>
    	<version>1.8.5</version>
    </dependency>
    <dependency>
    	<groupId>org.aspectj</groupId>
    	<artifactId>aspectjweaver</artifactId>
    	<version>1.8.5</version>
    </dependency>

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

package com.wisely.highlight_spring4.ch1.aop;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Action {
	String name();
}

代码解释
这里讲下注解,注解本身是没有功能的,就和xml一样。注解和xml都是一种元数据,元数据即解释数据的数据,这就是所谓配置。
注解的功能来自用这个注解的地方。
(3)编写使用注解的被拦截类。

package com.wisely.highlight_spring4.ch1.aop;

import org.springframework.stereotype.Service;

@Service
public class DemoAnnotationService {
	@Action(name="注解式拦截的add操作")
	public void add(){
		
	}
}

(4)编写使用方法规则被拦截类

package com.wisely.highlight_spring4.ch1.aop;

import org.springframework.stereotype.Service;

@Service
public class DemoMethodService {
	public void add(){}
}

(5)编写切面

package com.wisely.highlight_spring4.ch1.aop;

import java.lang.reflect.Method;

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;

@Aspect //声明一个切面
@Component //让此切面成为Spring容器管理的Bean
public class LogAspect {
	
	@Pointcut("@annotation(com.wisely.highlight_spring4.ch1.aop.Action)")//声明切点
	public void annotationPointCut(){}
	
	@After("annotationPointCut()")//声明一个建言,并使用@PointCut定义的切点
	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.wisely.highlight_spring4.ch1.aop.DemoMethodService.*(..))")//声明一个建言,此建言直接使用拦截规则做为参数
	public void before(JoinPoint joinPoint){
		MethodSignature signature = (MethodSignature) joinPoint.getSignature();
		Method method = signature.getMethod();
		System.out.println("方法规则拦截,"+method.getName());
	}
}

(6)配置类

package com.wisely.highlight_spring4.ch1.aop;

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

@Configuration
@ComponentScan("com.wisely.highlight_spring4.ch1.aop")
@EnableAspectJAutoProxy //开户Spring对AdpectJ的支持。
public class AopConfig {

}

(7)运行

package com.wisely.highlight_spring4.ch1.aop;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
	public static void main(String[] args) {
		AnnotationConfigApplicationContext context = 
				new AnnotationConfigApplicationContext(AopConfig.class);
		
		DemoAnnotationService demoAnnotationService = 
				context.getBean(DemoAnnotationService.class);
		
		DemoMethodService demoMethodService =
				context.getBean(DemoMethodService.class);
		
		demoAnnotationService.add();
		demoMethodService.add();
		
		context.close();
	}
}

结果如图
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值