Spring总结

来源:互联网 发布:国产纪录片 知乎 编辑:程序博客网 时间:2024/06/05 07:24

1.Spring 简介

1.1 spring概况

(1)spring是一个开源的框架
(2)是一个轻量级的控制反转(IOC)面向切面(AOP)的容器框架。
(3)包含并管理应用对象的配置和生命周期,是一个容器
(4)面向接口编程

2.Spring IOC(配置、注解) 容器

2.1 IOC及Bean容器

2.1.1面向接口编程

2.1.2 控制反转

控制权转移,应用程序本身不负责依赖对象的创建和维护,外部容器负责

2.1.3依赖注入(DI)

实现控制反转的一种方式,创建对象并且组装对象之间的关系

2.1.4 Bean的配置

<?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.xsd" > <bean id="oneInterface" class="com.imooc.ioc.interfaces.OneInterfaceImpl"></bean> </beans>

2.1.3 Bean容器的初始化

(1)文件:FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("F:/workspace/appcontext.xml");

 (2) Classpath: ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-context.xml")

 ( 3)web应用:Servlet进行初始化

2.1 Spring 注入方式

启动spring容器加载bean配置时,完成对变量的赋值行为

2.1.1 设值注入

xml配置方法:
        <bean id="injectionService" class="com.imooc.ioc.injection.service.InjectionServiceImpl">         <property name="injectionDAO" ref="injectionDAO"></property>         </bean>         <bean id="injectionDAO" class="com.imooc.ioc.injection.dao.InjectionDAOImpl"></bean>

class:
public class InjectionServiceImpl implements InjectionService {private InjectionDAO injectionDAO;//设值注入public void setInjectionDAO(InjectionDAO injectionDAO) {this.injectionDAO = injectionDAO;}}



2.1.3 构造注入


xml配置方法:
<bean id="injectionService" class="com.imooc.ioc.injection.service.InjectionServiceImpl">        <constructor-arg name="injectionDAO" ref="injectionDAO"></constructor-arg>        </bean>                <bean id="injectionDAO" class="com.imooc.ioc.injection.dao.InjectionDAOImpl"></bean>




class:
public class InjectionServiceImpl implements InjectionService {private InjectionDAO injectionDAO;//构造器注入public InjectionServiceImpl(InjectionDAO injectionDAO1) {this.injectionDAO = injectionDAO1;}}



3.Spring Bean装配(配置、注解)

3.1 Bean的配置项及作用域

3.1.1  Bean的配置项:

Id ,
class,
scope,
Constructor arguments,
properties,
Autowiring mode,
lazy-initiation mode 
Initiation/destruction method

3.1.2 Bean的作用域

singleton(默认)
prototype
request
session
global session

 <bean id="beanScope" class="com.bean.BeanScope" scope="singleton"></bean>

3.2 Bean的生命周期

定义--初始化--使用--销毁

3.3 Bean的Aware接口

可以对Spring的资源进行操作(通过实现接口进行操作)

3.4 Bean的自动装配


No:不做任何操作

byName:根据属性名 自动装配(需要加上set方法)
<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.xsd"         default-autowire="byName">                <bean id="autoWiringService" class="com.imooc.autowiring.AutoWiringService" ></bean>                <bean class="com.imooc.autowiring.AutoWiringDAO" ></bean> </beans>


byType:更具类型装配  (需要写构造方法)
<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.xsd"         default-autowire="byType">                <bean id="autoWiringService" class="com.imooc.autowiring.AutoWiringService" ></bean>                <bean class="com.imooc.autowiring.AutoWiringDAO" ></bean> </beans>



Constructor 


3.5 Resouces


实现接口,获取资源



3.6 Bean的定义及作用域注解的实现

3.6.1 类的自动检测及Bean的注册

<?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.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context.xsd" >                <context:component-scan base-package="com.imooc.beanannotation"></context:component-scan>         </beans>


3.6.2 使用过滤器进行自定义扫描

3.6.3 定义Bean

@Coponent("myStudent")public calss  Student()

3.6.3 定义Bean的作用域

@Scope("prototype")
@Service("myStudent")public calss  Student()


3.7 Autowired 注解


3.7.1 @Required 

bean的属性必须在配置时候就被填充

3.7.2 @Autowired

可以用在 setter、构造器、成员变量上。

@Servicepublic class InjectionServiceImpl implements InjectionService {//@Autowiredprivate InjectionDAO injectionDAO;@Autowiredpublic InjectionServiceImpl(InjectionDAO injectionDAO) {this.injectionDAO = injectionDAO;}//@Autowiredpublic void setInjectionDAO(InjectionDAO injectionDAO) {this.injectionDAO = injectionDAO;}public void save(String arg) {//模拟业务操作System.out.println("Service接收参数:" + arg);arg = arg + ":" + this.hashCode();injectionDAO.save(arg);}}


3.7.2 数组及Map的自动注入


@Order(1)//装载顺序@Componentpublic class BeanImplTwo implements BeanInterface {}
@Order(2)@Componentpublic class BeanImplOne implements BeanInterface {}
public interface BeanInterface {}
@Componentpublic class BeanInvoker {@Autowiredprivate List<BeanInterface> list;@Autowiredprivate Map<String, BeanInterface> map;@Autowired@Qualifier("beanImplTwo")private BeanInterface beanInterface;}


3.8 @Qualifier

3,9 基于Java容器的注解

4.AOP(配置、注解、AspectJ、API)

0 0
原创粉丝点击