文章目录
1 依赖注入
概念:
一个类里面依赖于另外一个类,然后spring把类的对象创建出来,然后注入到相应的类里面过程
1.1 依赖注入方式
1.1.1 普通set注入
//普通注入
private Long id;
private String name;
private Boolean sex;
private BigDecimal salary;
<property name="id" value="1"></property>
<property name="name" value="xx"></property>
<property name="sex" value="true"></property>
<property name="salary" value="10000"></property>
1.2 数组注入
//数组注入
private String[] arrays;
<!--注入数组-->
<property name="arrays" value="1,2,3"></property>
<property name="arrays">
<array>
<value>a</value>
<value>1</value>
<value>2</value>
<value>3</value>
<value>4</value>
</array>
</property>
1.3 List /Set Map 集合注入
//集合
private List<String> list;
private List<OtherBean> otherBeanList;
private Set<String> set;
private Set<OtherBean> otherBeanSet;
//map注入
private Map<String,String> map = new HashMap<String, String>();
<!--list注入-->
<property name="list">
<list>
<value>111</value>
<value>222</value>
<value>111</value>
</list>
</property>
<property name="otherBeanList">
<list>
<bean class="cn.itsource._01_di.OtherBean"></bean>
<bean class="cn.itsource._01_di.OtherBean"></bean>
<ref bean="otherBean"></ref>
<ref bean="otherBean"></ref>
</list>
</property>
<!--set注入-->
<property name="set">
<list>
<value>111</value>
<value>222</value>
<value>111</value>
</list>
</property>
<property name="otherBeanSet">
<list>
<bean class="cn.itsource._01_di.OtherBean"></bean>
<bean class="cn.itsource._01_di.OtherBean"></bean>
<ref bean="otherBean"></ref>
<ref bean="otherBean"></ref>
</list>
</property>
<!--map注入-->
<property name="map">
<map>
<entry key="key1" value="pp"></entry>
<entry key="key2" value="xx"></entry>
</map>
</property>
1.4 Properties 注入
//Properties
private Properties p1;
private Properties p2;
<!--Properties配置-->
<property name="p1">
<props>
<prop key="key1">pp</prop>
<prop key="key2">测试2</prop>
</props>
</property>
<!--不支持中文-->
<property name="p2">
<value>
key1=value1
key2=测试2
</value>
</property>
1.5 xml注入方式
自动注入 -->ByName ByType
全注解方式注入:
<context:component-scan base-package="。。。"/>
2 AOP
2.1 什么是AOP
概念:
AOP:它是spring的一种实现,面向切面编程
面向切面编程概念:
它的产生并不是去取代面向对象,而是对面向对象进行扩展,可以按照一定的方式,把类切开(切到方法上面),可以往方法前后添加一些逻辑
AOP使用场景
事务管理 日志管理 性能监控 拦截器
2.2 AOP术语
- 连接点(Joinpoint):程序执行的某一个特定位置,如类初始前后,方法的运行前后。而Spring只支持方法的连接点。
- 切点(Pointcut):切点可以定位到相应的连接点,一个切点可以定位多个连接点。
- 增强(Advice):又被称为通知,完成逻辑的增强。
- 目标对象(Target):增强逻辑织入的目标类。
- 引介(Introduction):特殊的增强,为类添加一些属性和方法。
- 织入(Weaving): 将增强添加到目标类的具体连接点上的过程。Spring使用动态代理织入。
- 代理(Proxy):一个类(原类)被织入增强(逻辑)后,就产生一个结果类,称为代理类。
- 切面(Aspect):由切点和增强组成
3 AOP实现
3.1 添加aop命名空间
xmlns:aop="http://www.springframework.org/schema/aop"
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
3.2 前置增强(通知)
<aop:config>
<aop:pointcut expression="execution(* cn.itsource.aop.I*Service.*(..))" id="pointcut" />
<aop:aspect ref="txManager">
<!-- 前置通知 -->
<aop:before method="begin" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
3.3 环绕增强
public class TxManager {
public void begin(){
System.out.println("开启事物");
}
public void commit(){
System.out.println("提交事物");
}
//Throwable 异常对象 所有异常的父类
public void rollback(Throwable e){
System.out.println("回滚事物" + e.getMessage());
}
public void close(){
System.out.println("关闭事物");
}
//环绕通知
public void around(ProceedingJoinPoint joinPoint){
//去执行方法
String name = joinPoint.getSignature().getName();
try {
if (name.equals("find") || "query".equals(name)){
joinPoint.proceed();
}else {
begin();
joinPoint.proceed();
commit();
}
} catch (Throwable e) {
e.printStackTrace();
rollback(e);
}finally {
if (name.equals("find") || "query".equals(name)){
}else {
close();
}
}
}
}
<!--aop配置-->
<aop:config>
<!--pointcut 切点 IUserService IPersonService 接口上面方法前面-->
<aop:pointcut expression="execution(* cn.itsource._04_xmlaop.I*Service.*(..))" id="pointcut" />
<!-- aspect方面或者切面-->
<aop:aspect ref="txManager">
<!--<!– 前置通知 –>
<aop:before method="begin" pointcut-ref="pointcut"/>
<!– 后置通知–>
<aop:after method="close" pointcut-ref="pointcut"/>
<!– 异常通知–>
<aop:after-throwing method="rollback" throwing="e" pointcut-ref="pointcut"/>
<!– 最终通知–>
<aop:after-returning method="commit" pointcut-ref="pointcut"/>-->
<!-- 环绕通知-->
<aop:around method="around" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
<bean id="txManager" class="cn.itsource._04_xmlaop.TxManager"></bean>
3.4 注解版
@Component
@Aspect //AOP的类注解
public class TxManager {
//设置切点
@Pointcut("execution(* cn.itsource.aopanno.I*Service.*(..))")
public void pointcut(){}
//前置通知
@Before("pointcut()")
public void begin(){
System.out.println("开启事务....");
}
//后置通知
@AfterReturning("pointcut()")
public void commit(){
System.out.println("提交事务...");
}
//异常通知
@AfterThrowing(pointcut="pointcut()",throwing="e")
public void rollback(Throwable e){
System.out.println("回滚事务....");
System.out.println(e.getMessage());
}
//最终通知
@After("pointcut()")
public void close(){
System.out.println("关闭资源....");
}
}
环绕通知方法:
//温馨提醒:如果要使用环绕通知的话,把其它几个通知的注解去掉(不然会出现重复)
@Around("pointcut()")
public Object around(ProceedingJoinPoint joinPoint){
Object object = null;
try {
begin();
object = joinPoint.proceed(); //执行相应的代码
commit();
} catch (Throwable e) {
rollback(e);
}finally{
close();
}
return object;
}
<!-- 支持aop注解 -->
<aop:aspectj-autoproxy />
4 AOP代理模式
代理模式的英文叫做Proxy或Surrogate,中文都可译为”代理“,所谓代理,就是一个人或者一个机构代表另一个人或者另一个机构采取行动。在一些情况下,一个客户不想或者不能够直接引用一个对象,而代理对象可以在客户端和目标对象之间起到中介的作用
4.1静态与动态代理
详见:https://blog.csdn.net/Java_pipixia/article/details/97812785