文章目录
官方文档地址
写在前面
个人认为,狂神说某些地方讲的不够清晰,但这种根据官方文档的学习方式值得我们学习!!
以下是几个个人为狂神说讲的不够清晰的地方,列出了一些补充资料,也有自己总结的博客!
理解IOC–控制反转
Spring – 依赖注入DI与控制反转
Spring注解-AutoWired-自动装配详解
代理模式(静态代理/动态代理)
execution表达式详解
Spring AOP的理解(通俗易懂)
配置记录–Spring-Mybatis
狂神说Spring01:概述及IOC理论推导
狂神说Spring02:Spring快速入门
狂神说Spring02包含有参构造函数的三种依赖注入方式!!
狂神说Spring03:依赖注入(DI)
Bean的作用域:单例,原型,request,session
狂神说Spring04:自动装配
自动装配:ByName、ByType
注解装配:@Autowired(required=false) 、@Qualifier指定ID
纠正自动装配的规则:先ByType ,多个符合type,再用ByName
注解自动装配-AutoWired :
- 首先使用自动装配属性的类型进行自动装配(ByType)
– 若没有bean的全限定类名(class)与此属性类型匹配,报错
– 若有且仅有一个bean的全限定类名与此属性类型匹配,自动装配
– 若有多个bean的全限定类名与此属性类型匹配,转2 - 当容器中有多个匹配属性类型的bean时,使用id匹配属性的名字!
– 当有bean的id和属性名匹配时,自动装配
– 当没有bean的id和属性名匹配时,报错 - 如果指定了@Qualifier(value=“xxx”),就会寻找类型匹配,并且id=xxx的
– 只要不满足类型匹配、id=xxx中的一条就会报错!!
狂神说Spring05:使用注解开发
Spring注解,纯java配置
<!--指定注解扫描包-->
<context:component-scan base-package="com.yy.pojo"/>
一、注册注解--放在类上
@Component:放在类上,自动在Spring中注册Bean
下述三个注解都是提供注册到Spring,对应MVC的层
@Respository dao层
@Service service层
@Controller controller层
二、自动装配注解--放在属性上
@Autowired : 自动装备属性。
@Qualifier : 与@Autowired联合使用,指定id装配。
@Nullable : 字段可以为null
@Resource : 自动装配
三、属性注入注解--放在属性上
@Value : 放在属性或属性对应的Set方法上,实现值注入
四、作用域注解--放在类上
@Scope 作用域注解
@Scope("singleton") 单例
@Scope("prototype") 原型
@Scope("request")
@Scope("session")
五、纯java配置注解(见下方)
@Configuration 指明此类为配置类
@Bean 方法名user 就是id
返回值类型,就是class
纯java配置
@Configuration
public class BeansConfig {
@Bean
public User usser(){
return new User();
}
}
等价于
<beans>
<bean id="myService" class="com.acme.services.MyServiceImpl"/>
</beans>
@Bean 方法名user 就是id
返回值类型,就是class
@Configuration 指明此类为配置类
测试代码:
ApplicationContext context = new AnnotationConfigApplicationContext(BeansConfig.class);
User user = (User) context.getBean("usser");
System.out.println(user.getName());
狂神说Spring06:静态/动态代理模式
代理模式:静态/动态代理模式、InvocationHandler、Proxy
狂神说Spring07:AOP就这么简单
MethodBeforeAdvice前置增强、AfterReturningAdvice后置增强
Aop配置:
execution表达式详解
Spring AOP的理解(通俗易懂)
<!--aop的配置-->
<aop:config>
<!--切入点-->
<!--execution(<修饰符模式>? <返回类型模式> <方法名模式>(<参数模式>) <异常模式>?)-->
<aop:pointcut id="pointcut_userService" expression="execution(* com.yy.aop.*.*(..))"/>
<!--通知:功能,比如日志-->
<aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut_userService"></aop:advisor>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut_userService"></aop:advisor>
</aop:config>
第二种方式:
<!--第二种方式:使用AOP的标签实现-->
<aop:aspect ref="diyLog">
<aop:pointcut id="pointcut_user" expression="execution(* yy..*(..))"/>
<aop:before method="before" pointcut-ref="pointcut_user"></aop:before>
<aop:after method="after" pointcut-ref="pointcut_user"></aop:after>
</aop:aspect>
注解方式:@Before、@After、@Arround
<aop:aspectj-autoproxy/> xml中打开支持
狂神说Spring08:整合MyBatis
导入相关jar包、整合spring-mybatis
mybatis-spring
- 准备pojo类、dao层接口、配置mapper(或注解) ---- mybatis
- 在Spring中注册DataSource、SqlSessionFactory(指定mybatis.xml、mapperLocations)
- 实现类:实现dao层接口中的方法,继承SqlSessionDaoSupport类,
- 注册实现类,注入依赖SqlSessionFactory
- 通过Spring得到实现类,调用对应方法!
狂神说Spring09:声明式事务
使用AOP织入事务!!
<!--配置声明式事务-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<constructor-arg ref="datasource"></constructor-arg>
</bean>
<!--结合AOP实现事务的织入-->
<!--配置事务通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!--给哪些方法配置事务-->
<tx:attributes>
<!--二次过滤切面中的方法!支持通配符-->
<tx:method name="*Test"/>
</tx:attributes>
</tx:advice>
<!--织入事务-->
<aop:config>
<aop:pointcut id="txPointcut" expression="execution(* com.yy..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"></aop:advisor>
</aop:config>