Spring基础复习

Spring IOC

使用注解实现Bean管理
注解类型:
@Component:spring定义的通用注解,可用于注解任何bean
@Repository, @Service,@Controller三者属于@Comonent的子注解
@Repository:通常用于注解DAO类,即持久层。
@Service:通常用于Service类,即服务类。
@Controller:通常用于Controller类,即控制层(MVC类)(springMVC)
 
@Required, @Autowired, @Qualifier, @Resource
@Configuration, @Bean, @Import, @DependsOn
 
元注解:注解的注解
 
类的自动检测和Bean的注册
spring能自动检测并注册bean到ApplicationContext中
如果类被被注解了@Repository,@Service,@Controller,Spring将自动检测到并将该Bean注册到ApplicationContext中
 
<context:annotation-config/>:提供了在XML文件中配置Spring注解的方式(请注意包含上下文命名空间)
如:<context:component-scan/>包含<context:annotation-config/>通常在使用前者后,不用再使用后者,前者包含后者的功能,后者只扫描方法字段。
    <context:component-scan base-package="org.example"/>base-package指定了org.example下的所有类(扫描路劲)
 
<beans>
    <context:component-scan base-package="org.example">
        <!--包含过滤器:包含所有以下正则所匹配的注解-->
        <context:include-filter type="regex"   
                expression=".*Stub.*Repositoty"/>
        <!--排除过滤器:排除基于以下ort.springfarmework.stereotype.Repository类的所有注解-->
        <contex:exclude-filter type="annotation"
                expression="ort.springfarmework.stereotype.Repository"/>
    </context:component-scan>
</beans>
注意:只有被注解了@Component.@Repository,@Service,@Controller或者自定义@Component的注解的类才能被扫描。
 
定义一个通过注解自动注册的bean类的方法:
1、在Bean类上添加 @Component.@Repository,@Service,@Controller或者自定义@Component的注解(通过注解的属性可以修改Bean的id,默认为Bean类的首字母小写的类名)
2、通过实现BeanNameGenerator接口并在实现类中添加一个无参构造器来自定义BeanID(在xml配置中,
        必须设置实现类的值如:<context:component-scan base-package="org.example" name-generator="org.example.myNameGenerator"/>)
3、使用注解实现Bean作用域配置:
    使用@Scope注解,拥有一个属性,值为:prototype、singleton(default:表示某一个ioc容器中为单例)...详见xml配置教程。
    (2:实现ScopeMetadataResolver接口并提供无参构造器,来自定义scope策略。
            必须通过xml配置实现类 <context:component-scan base-package="org.example" name-resolver="org.example.myNameGenerator"/>
4、设置Bean类代理方式:通过xml scoped-proxy指定,有no、interfaces、targetClass三个可选值。??
5、@Required:对于被注解的setter方法,必须在xml配置文件中配置。
6、@Autowired:对setter方法是用byType方式自动装配,对属性可以使得setter方法被去除,自动装配。对构造器:自动装配
                @Autowired方法的属性:可设置require=false使得自动装配没有找到对应的类也不会报错。值得注意,拥有多个constructor的Bean中只能有一个constructor的require=true
 
 
Spring AOP
面向切面编程是OOP的补充,关注点在于横向的功能,不关注纵向的业务逻辑,是对通用功能的整合。
    切面:对关注点(如关注日志记录)的抽象,(那么切面就将会应用到需要日志记录的对象中)可以作用于多个对象
    连接点:程序中特定的点,能够连接到切面
    切入点:对应的一类连接点的的集合
    通知:相对于切入点之后执行的方法
    织入:将切面应用到特定对象并生成代理对象的过程
    目标对象:代理的目标对象
AOP的实现由静态织入(AspectJ)和动态代理(jdk动态代理/CGLIB动态代理:spring AOP)两种
Schema-base AOP 配置AOP
所有的切面和通知器都将放入<aop:config>中(可以配置多个该元素)
<aop:config>
    <!-- aspect 切面 将aBean作为切面声明 -->
    <aop:aspect id="myBean" ref="aBean">
    </aop:aspect>
 
    <!-- bussinessService1为切入点的id,匹配com.xyz.myapp.service包中所有类的所有方法 -->
    <aop:pointcut id="bussinessService1"  
        expression="execution(* com.xyz.myapp.service..(..))"/>
 
    <!-- bussinessService2为切入点的id,匹配com.xyz.myapp.service包中exampleclass类的bussinessService()方法 -->
    <aop:pointcut id="bussinessService2"  
        expression="execution(com.xyz.myapp.service.exampleclass.bussinessService(..))"/>
 
</aop:config>
 
<!-- 切面类 切面 将aBean作为切面声明-->
<bean id="aBean" class="org.example.ABean">
</bean>
<!-- 业务类 连接点所在 -->
<bean id="bBean" class="">
</bean>
 
配置切入点:匹配连接点
execution方法用来匹配方法执行的连接点
execution(public * *(..)) 切入点为执行所有的public方法时
execution(* set(..)) 切入点为执行所有的以set为开始的方法时
execution(* com.xyz.service.AccountService.*(..)) 切入点为执行所有com.xyz.service.AccountService类中的所有方法时
execution(* com.xyz.service..(..)) 切入点为执行所有com.xyz.service包中的所有类中的所有方法时
execution(* com.xyz.service...(..)) 切入点为执行所有com.xyz.service包及其子包中的所有方法时
 
//within用于匹配指定类型内的方法执行
within()
 
//this 用于执行匹配当前AOP代理对象类型的执行方法
this()
 
//target
//args
//@args
//within
//@target
......

配置切入点:

<aop:pointcut id="..." expression="...">

</aop:pointcut>

原文地址:https://www.cnblogs.com/xiangleili/p/8657581.html