Spring知识点

######【Spring4介绍】####################################
①Spring几大核心:IOC/DI、AOP、声明式事务。
②Spring 框架runtime:

③容器(Container): Spring 当作一个大容器【ApplicationContext接口】。
④IOC概念:
(1)
(2)
(3)

######一【Spring4知识点】####################################
1.Spring环境搭建。
2.spring创建对象的三种方式
3.如何给Bean 的属性赋值(注入):基本类型、list、set、map、数组、Properties
4.DI依赖注入
5.Spring整合MyBatis
6.AOP[面向切面编程]
7.配置异常通知的步骤(AspectJ 方式)
8.异常通知(Schema-based 方式)
9.环绕通知(Schema-based 方式)
10.使用AspectJ方式实现(前置通知、后置通知、异常通知、环绕通知)
11.使用注解(基于Aspect)
15.自动注入
16.Spring中加载properties文件
17.scope属性
18.声明式事务
19.声明式事务中属性解释[<tx:method name=“ins*” read-only=“true” …/>]
20.常用注解

######二【Spring4知识点详解】####################################
1.Spring环境搭建。
①导包jar

②在src 下新建applicationContext.xml

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




③编写测试方法
ApplicationContext ac = newClassPathXmlApplicationContext(“applicationContext.xml”);
People people = ac.getBean(“peo”,People.class);
System.out.println(people);
//getBeanDefinitionNames(),Spring 容器中目前所管理的所对象
String[] names = ac.getBeanDefinitionNames();
for (String string : names) {
System.out.println(string);
}
2.spring创建对象的三种方式
(1)通过构造方法创建
a.无参构造创建:默认情况:
applicationContext.xml:

java代码中:
ApplicationContext ac = newClassPathXmlApplicationContext(“applicationContext.xml”);
People people = ac.getBean(“peo”,People.class);
b.参构造创建:需要明确配置





(2)实例工厂
a实现步骤:
创建实例工厂类:
public class PeopleFactory {public People newInstance(){return new People(1,“测试”);}}
applicationContext.xml:

<bean id=“peo1” factory-bean="factory"factory-method=“newInstance”>

(3)静态工厂
a实现步骤:
创建静态实例工厂类:
public class PeopleFactory {public static People newInstance(){return new People(1,“测试”);}}
applicationContext.xml:

3.如何给Bean 的属性赋值(注入)
(1)通过构造方法设置值.




(2)如果属性是基本数据类型或String 等简单


zhangsan

(3)如果属性是Set<?>

12

(4)如果属性是List<?>

12

如果list 中就只一个值:

(5)如果属性是数组

12

如果数组中就只一个值:

(6)如果属性是map






(7)如果属性Properties 类型


value
value1

4.DI依赖注入
代码体现:






5.Spring整合MyBatis
①导入mybatis所jar和spring基本包,spring-jdbc,spring-tx,spring-aop,spring-web,spring 整合mybatis 的包等
②配置web.xml
<?xml version="1.0" encoding="UTF-8"?>




contextConfigLocation

classpath:applicationContext.xml



org.springframework.web.context.ContextLoaderListener


③编写spring 配置文件applicationContext.xml

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























<property name="airportMapper"ref=“airportMapper”>

④编写代码
4.1 正常编写pojo
4.2 编写mapper 包下时必须使用接口绑定方案或注解方案(必须接口)
4.3 正常编写Service接口和Service实现类[需要在Service实现类中声明Mapper接口对象,并生成get/set方法]
4.4 spring 无法管理Servlet,在service 中取出Servie 对象
@WebServlet("/airport")
public class AirportServlet extends HttpServlet{
private AirportService airportService;
@Override
public void init() throws ServletException {
//对service 实例化
//spring和web整合后所信息都存放在webApplicationContext
ApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
airportService = ac.getBean(“airportService”,AirportServiceImpl.class);
}
@Override
protected void service(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException {
req.setAttribute(“list”, airportService.show());
req.getRequestDispatcher(“index.jsp”).forward(req,resp);
}
}

6.AOP[面向切面编程]
①面向切面编程的概念:
在程序原纵向执行流程中,针对某一个或某一些方法添加通知,形成横切面过程就叫做面向切面编程.
②常用概念:
【切点(pointcut)、前置通知(before advice)、后置通知(after advice)、异常通知(throws advice)】、织入。
③spring 提供了2种AOP实现方式
3.1 Schema-based:
Schema-based 实现步骤:
(1)导入包:
(2)新建通知类:
//前置通知类:arg0: 切点方法对象Method 对象; arg1: 切点方法参数 ; arg2:切点在哪个对象中
public class MyBeforeAdvice implements MethodBeforeAdvice {
@Override
public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
System.out.println(“执行前置通知”);
}
}
//后置通知类:arg0: 切点方法返回值; arg1:切点方法对象; arg2:切点方法参数; arg3:切点方法所在类的对象
public class MyAfterAdvice implements AfterReturningAdvice {
@Override
public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
System.out.println(“执行后置通知”);
}
}
(3)配置spring配置文件:
a.引入aop 命名空间、
b. 配置通知类的 、
c.配置切面、
d.* 通配符,匹配任意方法名,任意类名,任意一级包名
e. 如果希望匹配任意方法参数(…)
<?xml version="1.0" encoding="UTF-8"?>





aop:config

<aop:pointcut expression=“execution(* com.bjsxt.test.Demo.demo2())” id=“mypoint”/>

<aop:advisor advice-ref=“mybefore” pointcut-ref=“mypoint”/>
<aop:advisor advice-ref=“myafter” pointcut-ref=“mypoint”/>
</aop:config>



(4)编写测试代码:
class Demo {
public void demo1(){ System.out.println(“demo1()”)}
public void demo2(){ System.out.println(“demo2()”)}
public void demo3(){ System.out.println(“demo3()”)}
}
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext(“applicationContext.xml”);
Demo demo = ac.getBean(“demo”,Demo.class);
demo.demo1();
demo.demo2();
demo.demo3();
}
}
3.2 AspectJ:

7.配置异常通知的步骤(AspectJ 方式)
①新建类,在类写任意名称的方法:
public class MyThrowAdvice{
public void myexception(Exception e1){
System.out.println(“执行异常通知”+e1.getMessage());
}
}

②在spring 配置文件中配置:
说明:
aop:aspect的ref 属性表示:方法在哪个类中.
<aop: xxxx/> 表示什么通知
method: 当触发这个通知时,调用哪个方法
throwing: 异常对象名,必须和通知中方法参数名相同(可以不在通知中声明异常对象)


aop:config
<aop:aspect ref=“mythrow”>
<aop:pointcut expression=“execution(* com.bjsxt.test.Demo.demo1())” id=“mypoint”/>
<aop:after-throwing method=“myexception” pointcut-ref=“mypoint” throwing=“e1”/>
</aop:aspect>
</aop:config>

8.异常通知(Schema-based 方式)
①新建一个类实现throwsAdvice 接口
说明:
必须自己写方法,且必须叫afterThrowing
两种参数方式: 必须是1个或4 个
异常类型要与切点报的异常类型一致

public class MyThrow implements ThrowsAdvice{
public void afterThrowing(Method m, Object[] args,Object target, Exception ex) {
System.out.println(“执行异常通知”);
}
public void afterThrowing(Exception ex) throwsThrowable {
System.out.println(“执行异常通过-schema-base 方式”);
}
}

②在spring 配置文件中配置:

aop:config
<aop:pointcut expression=“execution(* com.bjsxt.test.Demo.demo1())” id=“mypoint”/>
<aop:advisor advice-ref=“mythrow” pointcut-ref=“mypoint” />
</aop:config>

9.环绕通知(Schema-based 方式)
①新建一个类实现MethodInterceptor
public class MyArround implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation arg0) throws Throwable {
System.out.println(“环绕-前置”);
Object result = arg0.proceed();//放行,调用切点方式
System.out.println(“环绕-后置”);
return result;
}
}

②配置applicationContext.xml

aop:config
<aop:pointcut expression=“execution(* com.bjsxt.test.Demo.demo1())” id=“mypoint”/>
<aop:advisor advice-ref=“myarround” pointcut-ref=“mypoint” />
</aop:config>

10.使用AspectJ方式实现(前置通知、后置通知、异常通知、环绕通知)
①新建类,不用实现【类中方法名任意】
public class MyAdvice {
public void mybefore(String name1,int age1){System.out.println(“前置”+name1 );}
public void mybefore1(String name1){System.out.println(“前置:”+name1);}

 public void myaftering(){System.out.println("后置2");}
 public void myafter(){System.out.println("后置1");}

 public void mythrow(){System.out.println("异常");}

 public Object myarround(ProceedingJoinPoint p) throws Throwable{
	System.out.println("执行环绕");
	System.out.println("环绕-前置");
	Object result = p.proceed();
	System.out.println("环绕后置");
	return result;
  }
 }

②配置spring 配置文件
说明:
aop:after/后置通知,是否出现异常 都执行
aop:after-returing/后置通知,只当切点正确执行时执行
aop:after/和aop:after-returing/和aop:after-throwing/执行顺序和配置顺序关
AspectJ中参数跟配置的通知类中方法一致(顺序、类型、名称)

<aop:config>
   <aop:aspect ref="myadvice">
      <!--切点-->
	<aop:pointcut expression="execution(* com.bjsxt.test.Demo.demo1(String,int)) and args(name1,age1)" id="mypoint"/>
	<aop:pointcut expression="execution(* com.bjsxt.test.Demo.demo1(String)) and args(name1)" id="mypoint1"/>
      <!--前置通知-->
	<aop:before method="mybefore" pointcut-ref="mypoint" arg-names="name1,age1"/>
	<aop:before method="mybefore1" pointcut-ref="mypoint1" arg-names="name1"/>
      <!--后置通知-->
	<aop:after method="myafter" pointcut-ref="mypoint"/>
	<aop:after-returning method="myaftering" pointcut-ref="mypoint"/>
      <--异常通知-->
	<aop:after-throwing method="mythrow" pointcut-ref="mypoint"/>
      <--环绕通知-->
	<aop:around method="myarround" pointcut-ref="mypoint"/>
  </aop:aspect>
</aop:config>

11.使用注解(基于Aspect)
①配置spring 配置文件[ 1.引入xmlns:context 2.配置文件中设置注解在哪些包中 3.ture:配置cglib动态代理 false:jdk动态代理 ]
<context:component-scan base-package=“com.bjsxt.advice,com.bjsxt.test”></context:component-scan>
<aop:aspectj-autoproxy proxy-target-class=“true”></aop:aspectj-autoproxy>

②在切点类中添加@Componet注解,在方法上添加@Pointcut(“”) 定义切点。
说明:
@Component相当于 如果没参数,把类名首字母变小写,相当于[@Component(“自定义名称”)]
----------------------------
@Component
public class Demo {
@Pointcut(“execution(* com.bjsxt.test.Demo.demo1())”)
public void demo1() throws Exception{
int i = 5/0;
System.out.println(“demo1”);
}
}

③在通知类中配置
说明: @Aspect 相当于aop:aspect/表示通知方法在当前类中
------------------------------
@Component
@Aspect
public class MyAdvice {
@Before(“com.bjsxt.test.Demo.demo1()”)
public void mybefore(){System.out.println(“前置”);}
@After(“com.bjsxt.test.Demo.demo1()”)
public void myafter(){ System.out.println(“后置通知”);}
@AfterThrowing(“com.bjsxt.test.Demo.demo1()”)
public void mythrow(){System.out.println(“异常通知”);}
@Around(“com.bjsxt.test.Demo.demo1()”)
public Object myarround(ProceedingJoinPoint p) throws Throwable{
System.out.println(“环绕-前置”);
Object result = p.proceed();
System.out.println(“环绕-后置”);
return result;
}
}

12.静态代理设计
静态代理设计: 真实对象、抽象对象(接口)、代理对象
静态代理设计模式缺点: 当代理功能比较多时,代理类中方法需要写很多.
静态代理设计模式优点: 保护真实对象、让真实对象职责更明显、扩展、
13.jdk动态代理(为了解决静态代理频繁编写代理功能缺点)
14.cglib动态代理

15.自动注入
1.在Spring 配置文件中对象名和ref=”id”id 名相同使用自动注入,可以不配置

2.两种配置办法:
  2.1 在<bean>中通过autowire=”” 配置,只对这个<bean>生效
  2.2 在<beans>中通过default-autowire=””配置,表当当前文件中所<bean>都是全局配置内容

3.autowire=””可取值:
3.1 default: 默认值,根据全局default-autowire=””值.默认全局和局部都没配置情况下,相当于no
3.2 no: 不自动注入
3.3 byName: 通过名称自动注入.在Spring 容器中找类的Id
3.4 byType: 根据类型注入.(spring 容器中不可以出现两个相同类型的)
3.5 constructor: 根据构造方法注入.(底层使用byName,构造方法参数名和其他的id相同)

16.Spring中加载properties文件
Spring中加载properties文件步骤:
1.在src 下新建xxx.properties 文件

2.在spring 配置文件中先引入xmlns:context,在下面添加(如果需要记载多个配置文件逗号分割)
<context:property-placeholder location=“classpath:db.properties”/>

3.添加了属性文件记载,并且在中开启自动注入注意的地方



4.在被Spring 管理的类中通过@Value(“${key}”)取出properties 中内容
  4.1 添加注解扫描 <context:component-scan base-package="com.bjsxt.service.impl"></context:component-scan>
  4.2 在类中添加
     @Value("${my.demo}")
    private String test;

17.scope属性
1.的属性,作用:控制对象效范围(单例,多例等)。标签对应的对象默认是单例的。
2.scope 可取值
singleton: 默认值,单例
prototype: 多例,每次获取重新实例化
request: 每次请求重新实例化
session: 每个会话对象内,对象是单例的.
application: 在application 对象内是单例
globalsession: spring 推出的一个对象, 依赖于spring-webmvc-portlet ,类似于session

18.声明式事务
①声明式事务:都是针对于ServiceImpl类下方法的.事务管理器基于通知(advice)的.
②在spring 配置文件中配置声明式事务
<context:property-placeholder location=“classpath:db.properties,classpath:second.properties”/>

<property name=“driverClassName value=” j d b c . d r i v e r " > < / p r o p e r t y > < p r o p e r t y n a m e = " u r l " v a l u e = " {jdbc.driver}"></property> <property name="url" value=" jdbc.driver"></property><propertyname="url"value="{jdbc.url}">


19.声明式事务中属性解释[<tx:method name=“ins*” read-only=“true” …/>]
(1).name=”” 哪些方法需要事务控制。支持*通配符。
2.read-only=”boolean” 是否是只读事务.
2.1如果为true,告诉数据库此事务为只读事务.数据化优化,会对性能一定提升,所以只要是查询的方法,建议使用.
2.2如果为false(默认值),事务需要提交的事务.建议新增,删除,修改.
3.propagation 控制事务传播行为.
3.1当一个具事务控制的方法被另一个事务控制的方法调用后,需要如何管理事务(新建事务?在事务中执行?把事务挂起?报异常?)
3.2 REQUIRED (默认值): 如果当前事务,就在事务中执行,如果当前没事务,新建一个事务.
3.3 SUPPORTS:如果当前事务就在事务中执行,如果当前没事务,就在非事务状态下执行.
3.4 MANDATORY:必须在事务内部执行,如果当前事务,就在事务中执行,如果没事务,报错.
3.5 REQUIRES_NEW:必须在事务中执行,如果当前没事务,新建事务,如果当前事务,把当前事务挂起.
3.6 NOT_SUPPORTED:必须在非事务下执行,如果当前没事务,正常执行,如果当前事务,把当前事务挂起.
3.7 NEVER:必须在非事务状态下执行,如果当前没事务,正常执行,如果当前事务,报错.
3.8 NESTED:必须在事务状态下执行.如果没事务,新建事务,如果当前事务,创建一个嵌套事务.
4.isolation=”” 事务隔离级别
4.1在多线程或并发访问下如何保证访问到的数据具完整性的.
4.2 脏读:
一个事务(A)读取到另一个事务(B)中未提交的数据,另一
个事务中数据可能进行了改变,此时A 事务读取的数据可能和数据
库中数据是不一致的,此时认为数据是脏数据,读取脏数据过程叫做脏读.
4.3 不可重复读:主要针对的是某行数据.(或行中某列)。主要针对的操作是修改操作。两次读取在同一个事务内。
当事务A 第一次读取事务后,事务B 对事务A 读取的淑君
进行修改,事务A 中再次读取的数据和之前读取的数据不一致,过程不可重复读.
4.4 幻读:主要针对的操作是新增或删除 两次事务的结果.
事务A 照特定条件查询出结果,事务B 新增了一条符合
条件的数据.事务A 中查询的数据和数据库中的数据不一致的,事务A 好像出现了幻觉,这种情况称为幻读.
4.5 DEFAULT: 默认值,由底层数据库自动判断应该使用什么隔离界别
4.6 READ_UNCOMMITTED: 可以读取未提交数据,可能出现脏读,不重复读,幻读. 效率最高.
4.7 READ_COMMITTED:只能读取其他事务已提交数据.可以防止脏读,可能出现不可重复读和幻读.
4.8 REPEATABLE_READ: 读取的数据被添加锁,防止其他事务修改此数据,可以防止不可重复读.脏读,可能出现幻读.
4.9 SERIALIZABLE: 排队操作,对整个表添加锁.一个事务在操作数据时,另一个事务等待事务操作完成后才能操作这个表. 最安全的 效率最低的.
5.rollback-for=”异常类型全限定路径”
5.1 当出现什么异常时需要进行回滚
5.2 建议:给定该属性值. 手动抛异常一定要给该属性值.
6.no-rollback-for=””
6.1 当出现什么异常时不滚回事务.

20.常用注解
1.@Component 创建类对象,相当于配置
2.@Service 与@Component 功能相同. 写在ServiceImpl 类上.
3. @Repository 与@Component 功能相同. 写在数据访问层类上.
4. @Controller 与@Component 功能相同. 写在控制器类上.
5. @Resource(不需要写对象的get/set)
5.1 java 中的注解
5.2 默认照byName 注入,如果没名称对象,照byType 注入
5.2.1 建议把对象名称和spring 容器中对象名相同
6. @Autowired(不需要写对象的get/set)
6.1 spring 的注解
6.2 默认照byType 注入.
7.@Value() 获取properties 文件中内容
8.@Pointcut() 定义切点
9.@Aspect() 定义切面类
10.@Before() 前置通知
11.@After 后置通知
12.@AfterReturning 后置通知,必须切点正确执行
13.@AfterThrowing 异常通知
14.@Arround 环绕通知

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

java之书

会持续更新实用好的文章谢谢关注

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值