Spring核心思想(IOC丶AOP)

一、反转思想IOC

1.什么是IOC?

IoC就是Inversion of Control,控制反转。在java开发中,IoC意味着将你设计好的类交给系统去控制,而不是在你的类内部控制。这称为控制反转。

高内聚低耦合:

高内聚就是把功能相关的模块集合在一起,例如:

添加用户模块,就可以把用户的相关内容放在一起,比如添加用户个人信息,为用户绑定角色等。

低耦合就是每个模块之间的关联性降到可控范围的最低,例如:

有个购物车系统和商品是相关的,当加入购物车的时候,势必需要去执行商品相关的操作,这就是耦合,那所谓的低耦合,就是购物车降低对商品这块的联系。

2.IOC的实现方式

方式一:XML方式

<bean id**=”ID名称 class=”需要做控制反转的类的全限定类名**”>

  1. bean标签:控制反转的标签
  2. id属性:对象名称
  3. class属性:需要做控制反转的类的全限定类名 (框架底层默认通过反射机制加载空参构造方法)
  4. scope属性:创建对象的范围
  5. singleton:单例模式(默认)
  6. prototype:多例模式
  7. init-method属性:初始化方法
  8. destroy-method属性:销毁方法
  9. lazy-init:懒加载
  10. true:开启懒加载
  11. false:关闭懒加载 (默认)
  12. 工厂模式
  13. factory-bean:工厂bean对象
  14. factory-method:工厂中创建对象的方法

f0290490d6b04488a3f52cdf46724c8a.png

方式二:注解方式

在配置文件中设置注解的命名空间

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:context="http://www.springframework.org/schema/context"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://www.springframework.org/schema/context
  7. http://www.springframework.org/schema/context/spring-context-4.3.xsd">

开启注解与开启组件扫描

  1. <!--开启注解-->
  2. <context:annotation-config/>
  3. <!--开启组件扫描-->
  4. <context:component-scan base-package="com.yy"/>

在需要做控制反转的类的上边标明控制反转的注解

通用注解

@Component

public class UserController {}

个性化注解

@Controller

@Service

@Repository

3.依赖注入手法(DI)

方式一:XML方式

  • 手动注入

    Set方法注入

  1. <bean id="userController" class="com.yy.controller.UserController">
  2. <property name="userService" ref="userServiceImpl"></property>
  3. </bean>

da3a29cbc356426195568abc15635b6f.png

注意事项:

name属性中的值要求写的是被注入方的类中的属性名称(其实找的是属性相对应的set方法)

ref属性写的是需要注入的bean标签的id值

构造方法注入

  1. <bean id="userController" class="com.yy.controller.UserController">
  2. <constructor-arg name="userService" ref="userServiceImpl"></constructor-arg>
  3. </bean>
  • 自动注入

    构造方法自动注入

前提是需要提供被注入的属性相关的构造方法

  1. <!--自动注入-->
  2. <bean id="userController" class="com.yy.controller.UserController" autowire="constructor"></bean>
  3. <bean id="userServiceImpl" class="com.yy.service.impl.UserServiceImpl" autowire="constructor"></bean>
  4. <bean id="userDaoImpl" class="com.yy.dao.impl.UserDaoImpl"></bean>

byName自动注入

前提要求被注入的类中的属性名称需要和注入的bean标签的id名称保持一致(其实寻找的是相关属性的set方法)

  1. <!--自动注入-->
  2. <bean id="userController" class="com.yy.controller.UserController" autowire="byName"></bean>
  3. <bean id="userService" class="com.yy.service.impl.UserServiceImpl" autowire="byName"></bean>
  4. <bean id="userDao" class="com.yy.dao.impl.UserDaoImpl"></bean>

byType自动注入

前提要求有一个类型能匹配即可,和id名称没有关系,但是只允许提供唯一的一个类型bean

  1. <!--自动注入-->
  2. <bean id="userController" class="com.yy.controller.UserController" autowire="byType"></bean>
  3. <bean id="userService" class="com.yy.service.impl.UserServiceImpl" autowire="byType"></bean>
  4. <bean id="userDao" class="com.yy.dao.impl.UserDaoImpl"></bean>

方式二:注解方式

在配置文件中设置注解的命名空间

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:context="http://www.springframework.org/schema/context"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://www.springframework.org/schema/context
  7. http://www.springframework.org/schema/context/spring-context-4.3.xsd">

开启注解与开启组件扫描

  1. <!--开启注解-->
  2. <context:annotation-config/>
  3. <!--开启组件扫描-->
  4. <context:component-scan base-package="com.yy"/>

在需要注入的属性或者set方法上标明依赖注入的注解

  1. @Autowired
  2. public void setUserService(UserService userService) {
  3. this.userService = userService;
  4. }

注意:

@Autowired 默认按照类型注入,如果出现多个类型都可以匹配,那么需要指定名字来注入

@Qualifier(value = “userServiceImpl”)

或者

@Resource

public void setUserService(UserService userService) { this.userService = userService; }

注意:

默认按照名字注入,如果出现名字不匹配,那么会根据类型来注入

@Resource(name = “userServiceImpl”)

二、面向切面AOP

1.切面概述

通过预编译的方式和运行期间动态代理实现程序功能的统一维护的一种技术。

2.搭建第一个切面

第一步:导入切面坐标

  1. <dependency>
  2. <groupId>org.aspectj</groupId>
  3. <artifactId>aspectjweaver</artifactId>
  4. <version>1.8.7</version>
  5. </dependency>

第二步:在spring的核心配置文件spring.xml中添加头部信息,以及开启注解

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:aop="http://www.springframework.org/schema/aop"
  3. xmlns:tx="http://www.springframework.org/schema/tx"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/aop
  9. http://www.springframework.org/schema/aop/spring-aop.xsd
  10. http://www.springframework.org/schema/tx
  11. http://www.springframework.org/schema/context/spring-tx.xsd
  12. http://www.springframework.org/schema/context
  13. http://www.springframework.org/schema/context/spring-context.xsd">
  14. <!--开启切面注解-->
  15. <aop:aspectj-autoproxy/>

第三步:编写配置类

  1. @Component
  2. @Aspect
  3. public class MyAspect {
  4. @Before("execution(public * com.xs.service..*.*(..) )")
  5. public void before(){
  6. System.out.println("我是前置通知");
  7. }
  8. }

3.声明式事务管理

第一步:导入事务管理的坐标

  1. <dependency>
  2. <groupId>org.aspectj</groupId>
  3. <artifactId>aspectjweaver</artifactId>
  4. <version>1.8.7</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework</groupId>
  8. <artifactId>spring-tx</artifactId>
  9. <version>5.0.2.RELEASE</version>
  10. </dependency>

第二步:在spring核心配置文件spring.xml中配置事务的命名空间

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:context="http://www.springframework.org/schema/context"
  3. xmlns:aop="http://www.springframework.org/schema/aop"
  4. xmlns:tx="http://www.springframework.org/schema/tx"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context.xsd
  10. http://www.springframework.org/schema/aop
  11. http://www.springframework.org/schema/aop/spring-aop.xsd
  12. http://www.springframework.org/schema/tx
  13. http://www.springframework.org/schema/tx/spring-tx.xsd">

第三步:配置事务管理器

  1. <!--配置事务管理器-->
  2. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  3. <property name="dataSource" ref="dataSource"></property>
  4. </bean>
XML版本

第四步:配置aop的切面(要切入的方法的位置),配置事务管理器,配置方法的通知类型

  1. <!--配置事务管理器-->
  2. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  3. <property name="dataSource" ref="dataSource"></property>
  4. </bean>
  5. <tx:advice id="txadvice" transaction-manager="transactionManager">
  6. <tx:attributes>
  7. <tx:method name="add*" propagation="REQUIRED"/>
  8. <tx:method name="del*" propagation="REQUIRED"/>
  9. <tx:method name="upd*" propagation="REQUIRED"/>
  10. <tx:method name="sel*" read-only="true" propagation="SUPPORTS"/>
  11. </tx:attributes>
  12. </tx:advice>
  13. <!--配置切面-->
  14. <aop:config>
  15. <aop:pointcut id="pointcut" expression="execution(public * com.xs.service..*.*(..))"/>
  16. <aop:advisor advice-ref="txadvice" pointcut-ref="pointcut"></aop:advisor>
  17. </aop:config>
注解版本
  1. <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>

在需要做事务管理的业务层方法上边标明注解 @Transactional

发表评论

表情:
评论列表 (有 0 条评论,80人围观)

还没有评论,来说两句吧...

相关阅读

    相关 spring Ioc Aop

    一、 IoC(Inversion of control): 控制反转 1、IoC: 概念:控制权由对象本身转向容器;由容器根据配置文件去创建实例并创建各个实例之间的依赖