Spring最简洁清晰总结
装配Bean
通过xml装配
格式:
<bean id= class=></bean>
bean的作用域
作用域 | 定义 |
---|---|
singleton | 单例 |
prototype | 多例 |
request | web单例 |
session | web单例 |
global-session | portlet单例 |
构造器注入(从上到下按照构造方法参数顺序):
<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 | 通用 |
@Controller | mvcController |
@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);
}
}