Spring核心思想(IOC丶AOP)
一、反转思想IOC
1.什么是IOC?
IoC就是Inversion of Control,控制反转。在java开发中,IoC意味着将你设计好的类交给系统去控制,而不是在你的类内部控制。这称为控制反转。
高内聚低耦合:
高内聚就是把功能相关的模块集合在一起,例如:
添加用户模块,就可以把用户的相关内容放在一起,比如添加用户个人信息,为用户绑定角色等。
低耦合就是每个模块之间的关联性降到可控范围的最低,例如:
有个购物车系统和商品是相关的,当加入购物车的时候,势必需要去执行商品相关的操作,这就是耦合,那所谓的低耦合,就是购物车降低对商品这块的联系。
2.IOC的实现方式
方式一:XML方式
<bean id**=”ID名称“ class=”需要做控制反转的类的全限定类名**”>
bean标签:控制反转的标签
id属性:对象名称
class属性:需要做控制反转的类的全限定类名 (框架底层默认通过反射机制加载空参构造方法)
scope属性:创建对象的范围
singleton:单例模式(默认)
prototype:多例模式
init-method属性:初始化方法
destroy-method属性:销毁方法
lazy-init:懒加载
true:开启懒加载
false:关闭懒加载 (默认)
工厂模式
factory-bean:工厂bean对象
factory-method:工厂中创建对象的方法
方式二:注解方式
在配置文件中设置注解的命名空间
<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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
开启注解与开启组件扫描
<!--开启注解-->
<context:annotation-config/>
<!--开启组件扫描-->
<context:component-scan base-package="com.yy"/>
在需要做控制反转的类的上边标明控制反转的注解
通用注解
@Component
public class UserController {}
个性化注解
@Controller
@Service
@Repository
3.依赖注入手法(DI)
方式一:XML方式
手动注入
Set方法注入
<bean id="userController" class="com.yy.controller.UserController">
<property name="userService" ref="userServiceImpl"></property>
</bean>
注意事项:
name属性中的值要求写的是被注入方的类中的属性名称(其实找的是属性相对应的set方法)
ref属性写的是需要注入的bean标签的id值
构造方法注入
<bean id="userController" class="com.yy.controller.UserController">
<constructor-arg name="userService" ref="userServiceImpl"></constructor-arg>
</bean>
自动注入
构造方法自动注入
前提是需要提供被注入的属性相关的构造方法
<!--自动注入-->
<bean id="userController" class="com.yy.controller.UserController" autowire="constructor"></bean>
<bean id="userServiceImpl" class="com.yy.service.impl.UserServiceImpl" autowire="constructor"></bean>
<bean id="userDaoImpl" class="com.yy.dao.impl.UserDaoImpl"></bean>
byName自动注入
前提要求被注入的类中的属性名称需要和注入的bean标签的id名称保持一致(其实寻找的是相关属性的set方法)
<!--自动注入-->
<bean id="userController" class="com.yy.controller.UserController" autowire="byName"></bean>
<bean id="userService" class="com.yy.service.impl.UserServiceImpl" autowire="byName"></bean>
<bean id="userDao" class="com.yy.dao.impl.UserDaoImpl"></bean>
byType自动注入
前提要求有一个类型能匹配即可,和id名称没有关系,但是只允许提供唯一的一个类型bean
<!--自动注入-->
<bean id="userController" class="com.yy.controller.UserController" autowire="byType"></bean>
<bean id="userService" class="com.yy.service.impl.UserServiceImpl" autowire="byType"></bean>
<bean id="userDao" class="com.yy.dao.impl.UserDaoImpl"></bean>
方式二:注解方式
在配置文件中设置注解的命名空间
<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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
开启注解与开启组件扫描
<!--开启注解-->
<context:annotation-config/>
<!--开启组件扫描-->
<context:component-scan base-package="com.yy"/>
在需要注入的属性或者set方法上标明依赖注入的注解
@Autowired
public void setUserService(UserService userService) {
this.userService = userService;
}
注意:
@Autowired 默认按照类型注入,如果出现多个类型都可以匹配,那么需要指定名字来注入
@Qualifier(value = “userServiceImpl”)
或者
@Resource
public void setUserService(UserService userService) { this.userService = userService; }
注意:
默认按照名字注入,如果出现名字不匹配,那么会根据类型来注入
@Resource(name = “userServiceImpl”)
二、面向切面AOP
1.切面概述
通过预编译的方式和运行期间动态代理实现程序功能的统一维护的一种技术。
2.搭建第一个切面
第一步:导入切面坐标
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
第二步:在spring的核心配置文件spring.xml中添加头部信息,以及开启注解
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/context/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--开启切面注解-->
<aop:aspectj-autoproxy/>
第三步:编写配置类
@Component
@Aspect
public class MyAspect {
@Before("execution(public * com.xs.service..*.*(..) )")
public void before(){
System.out.println("我是前置通知");
}
}
3.声明式事务管理
第一步:导入事务管理的坐标
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
第二步:在spring核心配置文件spring.xml中配置事务的命名空间
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
第三步:配置事务管理器
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
XML版本
第四步:配置aop的切面(要切入的方法的位置),配置事务管理器,配置方法的通知类型
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:advice id="txadvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="upd*" propagation="REQUIRED"/>
<tx:method name="sel*" read-only="true" propagation="SUPPORTS"/>
</tx:attributes>
</tx:advice>
<!--配置切面-->
<aop:config>
<aop:pointcut id="pointcut" expression="execution(public * com.xs.service..*.*(..))"/>
<aop:advisor advice-ref="txadvice" pointcut-ref="pointcut"></aop:advisor>
</aop:config>
注解版本
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
在需要做事务管理的业务层方法上边标明注解 @Transactional
还没有评论,来说两句吧...