spring的了解以及简单框架的搭建

了解spring:

Spring是一个开源的控制反转(Inversion of Controller)和面向切面(AOP)的框架,目的是为了简化开发。

IOC(控制反转):

public class PersonServiceBean{
    private PersonDao personDao=new PersonDaoBean();
    
    public void save(Person person){
        petsonDao.save(person);
    }
}

PersonDaoBean是在应用内部创建及维护的。所谓的控制反转就是本身不依赖对象的创建以及维护,依赖对象的创建及维护是由外部容器负责的。这样控制权就由应用中转移到了外部容器。

依赖注入(Dependency Injection)

依赖注入,就是我们前面说的控制反转,将bean的创建交给了spring容器,那么在该bean中的一些依赖属性的值的赋值操作,就叫依赖注入。

一些使用spring的优势:

降低组件之间的耦合度,实现软件各层之间的解耦。
使用容器提供的服务,例如:事务传播行为
单例模式支持
AOP技术

spring简单框架的搭建

首先我们需要官网下载spring,至于这个,我也没找到。我的spring是spring2.5.6版本。链接:http://pan.baidu.com/s/1qYgtQtm

使用spring的简单需要jar包:

dist/spring.jar
lib/jakarta-commons/commons-logging.jar
如果需要使用面向切面(AOP)
lib/aspectj/aspectjweaver4.jar和aspectjrt.jar
lib/cglib/cglib-nodep-2.1_3.jar
如果希望使用注解,还需要加入:
lib/j2ee/common-annotations.jar

配置文件模板beans.xml(文件名可随意):

配置文件beans.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">
</beans>

实例化spring容器

方法一:
在类路径下寻找配置文件来实例化容器

ApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"beans.xml"});

方法二:
在文件系统路径下寻找配置文件来实例化容器

ApplicationContext ctx=new FileSystemXmlApplicationContext(new String[]{"d:\beans.xml"});

配置文件可以指定多个,字符串数组表示。

使用spring容器装载bean

 <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"></bean>

id:bean的唯一标识

name:bean的唯一标识

class:bean的源路径

为什么有了id还有name属性?

  因为id是xml已有属性,会检查,是不能包含特殊字符

spring的三种实例化bean的方式:

1、构造方法实例化:

<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"/>

2、静态工厂实例化:
工厂中的生产bean方法为静态的

package cn.itcast.service.impl;

public class PersonServiceBeanFactory {
    
    public static PersonServiceBean createPersonServiceBean(){
        return new PersonServiceBean();
    } 

}


    <bean id="personService2" class="cn.itcast.service.impl.PersonServiceBeanFactory" 
           factory-method="createPersonServiceBean"/>

3、实例化工厂实例化:

package cn.itcast.service.impl;

public class PersonServiceBeanFactory {
    
    public static PersonServiceBean createPersonServiceBean(){
        return new PersonServiceBean();
    } 
    
    public PersonServiceBean createPersonServiceBean2(){
        return new PersonServiceBean();
    }

}
    
     <bean id="personServiceFactory" class="cn.itcast.service.impl.PersonServiceBeanFactory"/>
     <bean id="personService3" factory-bean="personServiceFactory" 
           factory-method="createPersonServiceBean2"/>

bean的作用域

  通过bean中的scope标签可以设置bean的作用域。

<bean id=".." class=".." scope=".." />

  singleton:单例模式,每一次getBean得到的是同一个。  

  prototype:每一次创建一个新实例
  request
  session

bean的生命周期:

1、bean的实例化:

默认情况下的bean作用范围是单实例的,是在容器实例化的时候就会对bean进行实例化的

bean的作用域范围是prototype的时候,是在调用getBean方法的时候进行实例化的
request,session也是这样。

如果希望在singleton中改变bean的实例化时机,可以使用lazy-init属性。
一般不建议在开发阶段使用,因为这样错误不方便查找。

singleton:单例模式,每一次getBean得到的是同一个。
            可以设置延迟加载,使用lazy-init=true
                <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" scope="singleton" lazy-init="true"/>
            如果设置所有的bean延迟加载:
                <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"
                   default-lazy-init="true">

bean的初始化方法和销毁方法:

bean的初始化方法:

public void init(){
System.out.println("初始化");
}

init-method属性

<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" 
init-method="init""/>

bean的销毁前方法:

public void destory(){
System.out.println("我要挂了");
}

destory-method属性:

<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" 
init-method="init" destroy-method="destory"/>

测试代码:

    @Test
    public void instanceSpring(){
        AbstractApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
        PersonService personService=(PersonService) ctx.getBean("personService");
        ctx.close();//正常关闭spring容器
    }

注意:要使用AbstractApplicationContext对象才能正常关闭spring容器。

原文地址:https://www.cnblogs.com/aigeileshei/p/5910867.html