简易Spring框架学习

Spring框架学习

控制反转IOC 依赖注入DI 面向切面AOP 声明式事务管理TX

搭建第一个Spring工程:

第一步:导入jar包
org.springframework.asm-3.1.1.RELEASE.jar
org.springframework.beans-3.1.1.RELEASE.jar
org.springframework.context-3.1.1.RELEASE.jar
org.springframework.core-3.1.1.RELEASE.jar
org.springframework.expression-3.1.1.RELEASE.jar

第二步:创建配置文件 beans.xml applicationContext.xml

第三步:读取配置文件

控制反转(Inversion of Control 简称IOC):应用本身不负责对象的创建和维护,由外部容器创建和维护对象,然后这样控制权就由应用本身转移到外部容器中。
依赖注入(Dependency Injection 简称DI):在运行期间,由外部容器动态的将对象注入到其它对象中

注入方式:
set方法注入 (一般使用)

构造器注入

简单属性注入:


集合属性注入:


list01
list02
list03




set01
set02
set03

	<property name="map">
		<map>
			<entry key="map1" value="value1"></entry>
			<entry key="map2" value="value2"></entry>
			<entry key="map3" value="value3"></entry>
		</map>
	</property>
	
	<property name="users">
		<list>
			<ref bean="user1"/>
			<ref bean="user2"/>
			<ref bean="user3"/>
		</list>
	</property>

自动注入:(XML)

只要提供相对应的构造方法即可


最少提供一个名字一样的id多对应的bean


有且仅有一个类型相匹配的bean

自动注入(注解)
第一步:
在beans.xml中配置注解的命名空间:

<?xml version="1.0" encoding="UTF-8"?>


第二步:
开启beans.xml中的注解
开启组件扫描

<context:annotation-config></context:annotation-config>
<!-- 开启组件扫描 -->
<context:component-scan base-package="com.sram"></context:component-scan>

第三步:
@Autowired
public void setUserService(UserService userService) {
this.userService = userService;
}

@Autowired
默认按照byType来注入,如果类型有多个,那么会找到名字相匹配的类型类注入,否则报错

@Resource(name = “”)
默认按照byName来注入,如果没有名字相对应,那么按照类型来注入,

用来控制反转的注解:@Component
等价于:
@Controller
@Service
@Repository

面向切面AOP
面向对象的补充

切面(Aspect):
连接点(join point):切面与程序的交叉点
切入点(point cut): 连接点的集合
通知(Advice)
目标对象

配置Spring中的切面:
第一步:导入切面的jar包
aopalliance.jar
aspectjrt.jar
aspectjweaver.jar
org.springframework.aop-3.1.1.RELEASE.jar
org.springframework.aspects-3.1.1.RELEASE.jar
cglib-nodep-2.1_3.jar 动态代理的依赖包
第二步:配置命名空间

<?xml version="1.0" encoding="UTF-8"?>

第三步:在beans.xml中或实体类中写切面的代码

aop:config
<aop:aspect id=“aspect” ref=“aop”>
<aop:pointcut id=“pointCut” expression=“execution(public void com.sram.action.UserAction.add())” />





<aop:after-throwing method=“after_throwing” pointcut-ref=“pointCut”/>

<aop:around method=“around” pointcut-ref=“pointCut”/>

<aop:after method=“after” pointcut-ref=“pointCut”/>
</aop:aspect>
</aop:config>

<aop:aspectj-autoproxy/>

@Component
@Aspect
public class MyAop {
@Before(“execution(public void com.sram.action.UserAction.add())”)
public void before(){
System.out.println(“前置通知…”);
}
@AfterReturning(“execution(public void com.sram.action.UserAction.add())”)
public void after_returning(){
System.out.println(“后置通知…”);
}
@AfterThrowing(“execution(public void com.sram.action.UserAction.add())”)
public void after_throwing(){
System.out.println(“抛异常通知…”);
}
@Around(“execution(public void com.sram.action.UserAction.add())”)
public void around(ProceedingJoinPoint p) throws Throwable{
System.out.println(“环绕之前”);
p.proceed();
System.out.println(“环绕之后…”);
}
@After(“execution(public void com.sram.action.UserAction.add())”)
public void after(){
System.out.println(“最终通知…”);
}
}

切入点表达式:

整合阶段:
Spring+JDBC:(数据库连接池机制) C3P0 DBCP
导入数据库连接池的jar包:
commons-dbcp.jar
commons-pool.jar
mysql-connector-java-5.1.15-bin.jar
然后在beans.xml中配置连接池
Spring+Hibernate
导入Spring依赖包,Hibernate依赖包,还有:
org.springframework.jdbc-3.1.1.RELEASE.jar
org.springframework.orm-3.1.1.RELEASE.jar
org.springframework.transaction-3.1.1.RELEASE.jar

Hibernate+SpringMVC+Spring Hibernate+SpringBoot+Spring maven
Hibernate+Struts2+Spring SSH
事务的传播性:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值