Spring核心机制IoC与AoP梳理
文章目录
Spring核心机制IoC与AoP梳理IoC介绍IoC案例介绍pom文件中IoC环境引入自己new对象方法举例(正转)IoC创建对象基于XML方法基于注解方式IoC依赖注入AoP介绍AoP术语:AoP实现IoC介绍
控制反转(Inversion of Control),IOC 不是一种技术,而是一种设计思想,IoC 是 Spring 全家桶各个功能模块的基础,创建对象的容
器。
控制反转,将对象的创建进行反转,常规情况下,对象都是
开发者手动创建的,使用 IoC 开发者不再需要创建对象,而
是由 IoC 容器根据需求自动创建项目所需要的对象。
IoC案例介绍
pom文件中IoC环境引入
<dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.3.15</version></dependency
自己new对象方法举例(正转)
创建一个类:
@Datapublic class DataConfig {private String url;private String driverName;private String username;private String password;
测试使用常规方法:
IoC创建对象
基于XML方法
开发者把需要的对象在 XML 中进行配置,Spring框架读取这个配置文件,根据配置文件的内容来创建对象。(不常用)
配置文件在resources里进行创建。
配置文件取名:applicationcontext,springioc,或者其他 无所谓
首先在配置文件中进行配置:
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="/schema/beans"xmlns:xsi="/2001/XMLSchema-instance"xmlns:context="/schema/context"xmlns:aop="/schema/aop"xmlns:p="/schema/p"xsi:schemaLocation="/schema/beans/schema/beans/spring-beans.xsd/schema/context/schema/context/spring-context.xsd/schema/aop/schema/aop/spring-aop-4.3.xsd"><bean class="com.southwind.ioc.DataConfig"id="config"><property name="driverName" value="Driver"></property><property name="url" value="localhost:8080"></property><property name="username" value="root"></property><property name="password" value="root"></property></bean></beans>
测试使用:
public class Test {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");//ioc容器System.out.println(context.getBean("config"));//和配置文件里面id对应}}
基于注解方式
配置类方法用一个 Java 类来替代 XML 文件,把在 XML 中配置的内容放到配置类中。
首先创建一个配置类:
@Configuration//加这个注解证明是配置类public class BeanConfiguration {@Beanpublic DataConfig dataConfig(){DataConfig dataConfig = new DataConfig();dataConfig.setDriverName("Driver");dataConfig.setUrl("localhost:3306/dbname");dataConfig.setUsername("root");dataConfig.setPassword("root");return dataConfig;}}
测试使用:
ApplicationContext context = newAnnotationConfigApplicationContext(BeanConfiguration.class);//实现类和上面不一样//通过包名也可以加载它System.out.println(context.getBean("dataConfig"));//通过方法名获取bean
扫包+注解
更简单的方式,不再需要依赖于 XML 或者配置类,而是直接将 bean 的创建交给目标类,在目标类添加注解来创建。
首先:
@Data@Component//告诉spring框架,这个类需要被注入到Ioc的public class DataConfig {@Value("localhost:3306")private String url;@Value("Driver")private String driverName;@Value("root")private String username;@Value("root")private String password;}
测试使用:
ApplicationContext context = newAnnotationConfigApplicationContext("com.southwind.ioc");//告诉包名System.out.println(context.getBean(DataConfig.class));//通过类型
IoC依赖注入
a里面有个b的对象,创建a b两个对象,自动的把b装到a里面
@Data@Componentpublic class GlobalConfig {@Value("8080")private String port;@Value("/")private String path;@Autowiredprivate DataConfig dataConfig;}
@Autowired 通过类型进行注入,如果需要通过名称取值,
通过 @Qualifier 注解完成名称的映射
Data@Componentpublic class GlobalConfig {@Value("8080")private String port;@Value("/")private String path;@Autowired@Qualifier("config")private DataConfig config;}
@Data@Component("config")public class DataConfig {@Value("localhost:3306")private String url;@Value("Driver")private String driverName;@Value("root")private String username;@Value("root")private String password;}
测试使用:
ApplicationContext context = newAnnotationConfigApplicationContext(com.southwind.ioc);//实现类和上面不一样//通过包名也可以加载它System.out.println(context.getBean("GlobalConfig.class"));
AoP介绍
面向切面编程,是一种抽象化的面向对象编程,对面向对象编程的一种补充,底层使用动态代理机制来实现。
1、打印日志(常用)
2、事务
3、权限处理
一句话介绍:做到核心业务和非业务代码的解耦合,提高程序的可重用性,同时提高了开发的效率。
AoP术语:
连接点
类里面哪些方法可以被增强,这些方法称为连接点
切入点
实际被真正增强的方法,称为切入点
增强(通知)
实际增加的逻辑部分称为通知(增强)
通知有多种类型:
前置通知
后置通知
环绕通知
异常通知
最终通知
切面(过程)
把通知应用到切入点的过程,就叫切面
AoP实现
底层通过动态代理
1.先引入切面依赖
2.创建切面类
@Component@Aspectpublic class LoggerAspect {@Before("execution(public int com.southwind.aop.CalImpl.*(..))")public void before(JoinPoint joinPoint){String name =joinPoint.getSignature().getName();System.out.println(name+"方法的参数是"+Arrays.toString(joinPoint.getArgs()));}@AfterReturning(value = "execution(public int com.southwind.aop.CalImpl.*(..))",returning = "result")public void afterReturning(JoinPoint joinPoint,Object result){String name =joinPoint.getSignature().getName();System.out.println(name+"方法的结果是"+result);}}//after 注解拿不到结果 ,要想拿结果用afterreturning
3.配置自动扫包+开启代理
配置文件里:
<context:component-scan base-package="com.southwind.aop"></context:component-scan><!-- 开启自动生成代理 --><aop:aspectj-autoproxy></aop:aspectj-autoproxy></beans>
4.使用:
public class Test {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");Cal bean = context.getBean(Cal.class);//用接口去取System.out.println(bean.add(9, 8));System.out.println(bean.sub(9, 8));System.out.println(bean.mul(9, 8));System.out.println(bean.div(9, 8));}}