Spring最简洁清晰总结-ioc-aop

Spring最简洁清晰总结

装配Bean

通过xml装配

格式:

<bean id= class=></bean>
bean的作用域
作用域定义
singleton单例
prototype多例
requestweb单例
sessionweb单例
global-sessionportlet单例
构造器注入(从上到下按照构造方法参数顺序):
<bean id= class=>
	<constractor-arg value= ></constractor-arg>
    <constractor-arg ref= ></constractor-arg>  
</bean>

value是基本类型,ref是类

属性注入
<bean id= class= >
	<property name= value=></property>
    <property name= ref=></property>
</bean>

value是基本类型,ref是类

通过spEL属性注入
<bean id= class= >
	<property name= value=1e5></property>
</bean>

最小化XML配置

自动装配Bean

通过xml自动装配:

​ 通过no的方式:

<bean id=>
	<property name= ref=></property>
</bean>

​ 通过byname、bytype方式:

<bean id= autowire="byName"/"byType" >
</bean>

​ 但是bytype模式下如果存在多个bean,抛出异常,解决办法是:

<bean id= autowire="byType" autowire-candidate="false">
</bean>

​ 装配构造器:

<bean id= autowire="constructor" >
</bean>

和bytype有相同局限性

​ 最佳自动装配

<bean id= autowire="autodetect" >
</bean>

相当于先constructor再bytype

通过注解自动装配
@Autowired
Student stu;
@Autowired
void setStudent(Student stu){
    this.stu=stu;
}

​ 但是,@Audowired相当于byType,以下方式相当于byName:

@Autowired
@Qualifier("student1")
Student stu;
	@Autowired
@Inject
Student stu;

@Inject相当于@Audowired,只是如果值为null抛出异常。

@Value("hello")
String str;

自动检测Bean

注解说明
@Component通用
@ControllermvcController
@Repository数据仓库
@Service服务

面向切面编程

声明前置和后置通知

切面:

public class Audience{
    public void takeSeats(){...};
    public void applaud(){...};
}

注册切面Bean:

<bean id=audience class=Audience></bean>

切点:

<aop:config>
	<aop aspect ref="audience">
    	<aop:before
         pointcut="execution(* com.xxx.Perform.perform(..) "
         method="takeSeats"/>
        <aop:after
         pointcut="execution(* com.xxx.Perform.perform(..) "
         method="applaud"/>
    </aop>
</aop:config>

或者:

<aop:config>
	<aop aspect ref="audience">
        <aop:pointcut id="p" expression=
            "execution(* com.xxx.Perform.perform(..)"/>
    	<aop:before pointcut-ref="p" method="takeSeats"/>
        <aop:after pointcut-ref="p"   method="applaud"/>
    </aop>
</aop:config>

声明环绕通知

切面:

public class Audience{
    public void takeSeats(){...};
    public void applaud(){...};
    public void watchShow(ProceedingJoinPoint joinpoint){
        Time t1=new Time();
        joinpoint.proceed();//切点执行
        Time t2=new Time();
        Sys.out.print(t2-t1);
	} 
}

注册切面Bean:

<bean id=audience class=Audience></bean>

切点:

<aop:config>
	<aop aspect ref="audience">
        <aop:pointcut id="p" expression=
            "execution(* com.xxx.Perform.perform(..)"/>
    	<aop:around pointcut-ref="p" method="takeSeats"/>
    </aop>
</aop:config>

通知传参:

<aop:config>
	<aop aspect ref="magician">
        <aop:pointcut id="thingking" expression=
            "execution(* com.xxx.Think.think(String) and
             args(thoughts)"/>
    	<aop:before pointcut-ref="thinking" 		     method="interceptThoughts" arg-names="thoughts"/>
    </aop>
</aop:config>

注解切面:

@Aspect
public class Audience{
    @Pointcut("execution(* com.xx.Perform.perform(..))")
    public void performance(){}
    //performance不用去实现,其实就是相当于pointcut id="performance"
    
    @Before("performance()")
    public void takeSeats(){...};
    @After("Performance()")
    public void applaud(){...};
    @Around("performance()")
     public void watchShow(ProceedingJoinPoint joinpoint){
        Time t1=new Time();
        joinpoint.proceed();//切点执行
        Time t2=new Time();
        Sys.out.print(t2-t1);
	} 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值