初学Spring必须弄懂的几个知识点

来源:互联网 发布:兰州市政府网络留言板 编辑:程序博客网 时间:2024/06/03 18:12

 一. spring的关键在于它的AOPIOC,所以首先要弄懂AOP和IOC的原理,深入理解了这两方面才算是入门了。

1.IoC(Inversion of control): 控制反转  (将以前我们在代码中new的工作交给了spring容器)
概念:控制权由对象本身转向容器;由容器根据配置文件去创建实例并创建各个实例之间的依赖关系 
核心:bean工厂;在Spring中,bean工厂创建的各个实例称作bean

动态注入,让一个对象的创建不用new了,可以自动的生产,这其实就是利用java里的反射 
反射其实就是在运行时动态的去创建、调用对象,Spring就是在运行时,跟xml  Spring的配置 文件来动态的创建对象,和调用对象里的方法的 。

 

2.AOP(Aspect-Oriented Programming): 面向方面编程 
2.1 代理的两种方式: 
静态代理: 
 针对每个具体类分别编写代理类; 
 针对一个接口编写一个代理类; 
动态代理: 
针对一个方面编写一个InvocationHandler,然后借用JDK反射包中的Proxy类为各种接口动态生成相应的代理类 
2.2 AOP的主要原理:动态代理 

 

这个就是面向切面编程,可以为某一类对象 进行监督和控制(也就是在调用这类对象的具体方法的前后去调用你指定的 模块)从而达到对一个模块扩充的功能。这些都是通过配置类达到的。 
Spring目的:就是让对象与对象(模块与模块)之间的关系没有通过代码来关联,都是通过配置类说明 
管理的(Spring根据这些配置 内部通过反射去动态的组装对象) 
要记住:Spring是一个容器,凡是在容器里的对象才会有Spring所提供的这些服务和功能。 
Spring里用的最经典的一个设计模式就是:模板方法模式。  Spring里的配置是很多的,很难都记住,但是Spring里的精华也无非就是以上的两点,把以上两点跟理解了 
也就基本上掌握了Spring. 

 

二.Spring MVC 工作原理及其作用

    1.springmvc请所有的请求都提交给DispatcherServlet,它会委托应用系统的其他模块负责负责对请求进行真正的处理工作。

  2.DispatcherServlet查询一个或多个HandlerMapping,找到处理请求的Controller.

  3.DispatcherServlet请请求提交到目标Controller

  4.Controller进行业务逻辑处理后,会返回一个ModelAndView

  5.Dispathcher查询一个或多个ViewResolver视图解析器,找到ModelAndView对象指定的视图对象

  6.视图对象负责渲染返回给客户端。

三.

经常会有听到Spring 上下文:Spring 上下文是一个配置文件,向 Spring框架提供上下文信息。(作用承上启下:spring框架是上自己写的代码是下,即将spring与项目融合的桥梁)

 

四.Spring的自动注入(目前spring3.0开始有了3种配置方式,xml、annotation、javaconfig形式,其中javaconfig形式是spring3.0才加入的,就是用java代码来写配置,感觉效果不是很大),看下面例子中有三个类 Office类有一个属性 officeNO; Car类中有两个属性brand和price;boss类中有两个属性office和car:

1.仅xml配置:

<?xml version="1.0" encoding="UTF-8" ?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean id="boss" class="Boss类路径"><property name="car" ref="car" /><property name="office" ref="office" /></bean><bean id="office" class="Office类路径"><property name="officeNo" value="002" /></bean><bean id="car" class="Car类路径" scope="singleton"><property name="brand" value=" 红旗 CA72" /><property name="price" value="20000" /></bean></beans>

在java中获得bean

         String[] locations = { "beans.xml" };         ApplicationContext ctx = new ClassPathXmlApplicationContext(locations);Boss boss = (Boss) ctx.getBean("boss");System.out.println(boss);

 

2.使用Annotation方式配置

Spring 2.5 引入了 @Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。

Spring 通过一个 BeanPostProcessor 对 @Autowired 进行解析,所以要让 @Autowired 起作用必须事先在 Spring 容器中声AutowiredAnnotationBeanPostProcessor Bean。

public class Boss { @Autowired private Car car;@Autowired private Office office; }

当不能确定 Spring 容器中一定拥有某个类的 Bean 时,可以在需要自动注入该类 Bean 的地方可以使用@Autowired(required = false),这等于告诉 Spring:在找不到匹配 Bean 时也不报错。(下面的xml文件中缺少一个Car的bean)

xml中

<?xml version="1.0" encoding="UTF-8" ?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- 该 BeanPostProcessor 将自动起作用,对标注 @Autowired 的 Bean 进行自动注入 --><bean class="org.springframework.beans.factory.annotation. AutowiredAnnotationBeanPostProcessor" /> <!-- 移除 boss Bean 的属性注入配置的信息 --><bean id="boss" class="Boss类的路径" /><bean id="office" class="Office类的路径"><property name="officeNo" value="001" /></bean><bean id="car" class="Car类的路径" scope="singleton"><property name="brand" value=" 红旗 CA72" /><property name="price" value="2000" /></bean></beans>

补充:

1.Spring 不但支持自己定义的 @Autowired 的注释,还支持几个由 JSR-250 规范定义的注释,它们分别是@Resource、@PostConstruct 以及 @PreDestroy。

@Resource 的作用相当于 @Autowired,只不过 @Autowired 按 byType 自动注入,面 @Resource 默认按 byName 自动注入罢了。@Resource 有两个属性是比较重要的,分别是 name 和 type,Spring 将 @Resource 注释的 name 属性解析为 Bean 的名字,而 type 属性则解析为 Bean 的类型。所以如果使用 name 属性,则使用 byName 的自动注入策略,而使用 type 属性时则使用 byType 自动注入策略。如果既不指定 name 也不指定 type 属性,这时将通过反射机制使用 byName 自动注入策略。Resource 注释类位于 Spring 发布包的 lib/j2ee/common-annotations.jar 类包中,因此在使用之前必须将其加入到项目的类库中。一般情况下,我们无需使用类似于 @Resource(type=Car.class) 的注释方式,因为 Bean 的类型信息可以通过 Java 反射从代码中获取。要让 JSR-250 的注释生效,除了在 Bean 类中标注这些注释外,还需要在 Spring 容器中注册一个负责处理这些注释的BeanPostProcessor:<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>

2.<context:annotation-config />
<context:annotation-config />
将隐式地向 Spring容器注册AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor
以及 equiredAnnotationBeanPostProcessor 这 4 个 BeanPostProcessor。
所以可以将xml配置改成如下(在配置文件中使用 context 命名空间之前,必须在 <beans> 元素中声明 context 命名空间)

<?xml version="1.0" encoding="UTF-8" ?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"><context:annotation-config /><bean id="boss" class="Boss类的路径" /><bean id="office" class="Office类的路径"><property name="officeNo" value="001" /></bean><bean id="car" class="Car类的路径" scope="singleton"><property name="brand" value=" 红旗 CA72" /><property name="price" value="2000" /></bean></beans>

<context:component-scan>中包含了<context:annotation-config/>所以可以不要用后面的那个,组件扫描功能强大

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

原创粉丝点击