官网
https://spring.io/projects/spring-framework#learn
下载
https://repo.spring.io/ui/native/release/org/springframework/spring
控制反转IOC,面向切面AOP
默认使用无参构造创建对象
合并多个xml文件,多用于合作开发
自动装配
autowire
注解配置
<?xml version="1.0" encoding="UTF-8"?><context:annotation-config/>
使用注解开发,配置文件加上
<context:component-scan base-package=“com.hl…”/>
用来扫描包下的带注解的实体类
实体类加上
@Component
等价于
衍生的注解
dao层 @Repository
service层 @Service
controller层 @Controller
静态代理,AOP底层
动态代理InvocationHandler能自动生成代理类,本质是反射机制
万能动态代理工具
public class ProxyInvocationHandler implements InvocationHandler {
//被代理的接口
private Object target;
public void setTarget(Object target) {
this.target = target;
}
//生成得到代理类
public Object getProxy(){
return Proxy.newProxyInstance(this.getClass().getClassLoader(),target.getClass().getInterfaces(),this);
}
//处理代理实例并返回结果
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object result=method.invoke(target,args);
return result;
}
}
注意:工具要以接口为对象,一个动态代理可以实现代理多个类,只要它们实现了同一个接口
使用AOP要导入的包
org.aspectj aspectjweaver 1.9.7 runtime自定义类实现AOP(HelloPointcut 为自定义类)
aop:config
<aop:aspect ref=“hellopoint”>
<aop:pointcut id=“pointcut” expression=“execution(* *(…))”/>
<aop:before method=“after” pointcut-ref=“pointcut”/>
<aop:before method=“before” pointcut-ref=“pointcut”/>
</aop:aspect>
</aop:config>
mybatis整合spring
需要导入mybatis-spring包,
官网 http://mybatis.org/spring/zh/index.html
spring要操作数据库需导入spring-jdbc包(版本号最好一样)
spring中的事务
1、声明式事务
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<constructor-arg ref="dataSource"/>
</bean>
<tx:advice id="transactionInterceptor" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="txPoint" expression="execution(* *.*(..))"/>
<aop:advisor advice-ref="transactionInterceptor" pointcut-ref="txPoint"/>
</aop:config>
2、编程式事务(要改变原代码,放弃!)