Spring框架入门:(非原著,转载)

1.1、      耦合性和控制反转:

       对象之间的耦合性就是对象之间的依赖性。对象之间的耦合越高,维护成本越高。因此,对象的设计应使类和构件之间的耦合最小。

例:

public interface ICustomerDao{      //接口

         public boolean getCustomerByAccount();

}

public class CustomerDao implements ICustomerDao{

         /*实现代码*/

}

public class BeanFactory{         //工厂类

         public static Object getBean(String className){

                   return Class.forName(className).newInstance();         //反射机制

         }

}

Public class LoginAction{

         public void execute(){

                   ICustomerDao icdao =

                            (ICustomerDao)BeanFactory.getBean(“CustomerDao”);

                   boolean b = icdao.getCustomerByAccount();

         }

}

控制反转的原理:

       这种设计方法有一个好处是,BeanFactory类的通用性很强,可以将其框架化。因此,框架化之后,对象的生成由框架参考配置文件进行,和具体实现类的源码无关,将对象生成的控制权由修改不方便的源代码转变为修改相对方便的配置文件与几乎不进行修改的框架进行,这就是控制反转(IOC)的原理。

1.2、      Spring框架简介:

       IOC思想给降低对象间的耦合性带来了巨大的好处,但是,IOC毕竟只是一种思想,不同的程序员写出的基于IOC思想的应用,风格可能不一样,影响程序的标准化。因此,有必要对IOC来进行标准化。

       Spring就是这样的一个框架,在使用的过程中,受到广泛的承认。因此,IOC是Spring框架的基础,或者说,Spring是为了规范IOC开发而发布的一个框架。

2、Spring框架的基本使用方法:

2.1、导入Spring框架:

(1)、新建一个Java Project;

(2)、MyEclipse —> Project Capabilities —> Add Spring Capabilities:

2.2、编写被调用方及其接口:

(1)、

package idao;

publicinterface ICustomerDao {

    publicboolean getCustomerByAccount();

}

(2)、

package dao;

import idao.ICustomerDao;

publicclass CustomerDao implements ICustomerDao{

    publicboolean getCustomerByAccount(){

       System.out.println("CustomerDao查询数据库");

       returntrue;

    }

}

2.3、编写Spring配置文件:

       Spring的配置文件为applicationContext.xml,一般不要修改名称,并放在src目录下,该文件的主要目的是配置需要实例化的对象:

<?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:p="http://www.springframework.org/schema/p"

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean id="icdao" class="dao.CustomerDao"></bean>

</beans>

2.4、编写调用方:

       本例中,调用方为LoginAction,在Action中,可以调用Spring框架,让其根据配置文件实例化相应的对象:

package action;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.FileSystemXmlApplicationContext;

import idao.ICustomerDao;

publicclass LoginAction {

    publicvoid execute(){

       ApplicationContext context =

new FileSystemXmlApplicationContext("/src/applicationContext.xml");

       ICustomerDao icdao = (ICustomerDao)context.getBean("icdao");

       icdao.getCustomerByAccount();

    }

}

       然后,编写一个测试文件,来测试前面的代码:

package main;

import action.LoginAction;

publicclass Main {

    publicstaticvoid main(String[] args) {

       new LoginAction().execute();

    }

}

3、依赖注入:

       Spring配置文件的核心体现在:

       <bean id=”icdao” class=”dao.CustomerDao”></bean>

       表示让框架实例化一个dao.CustomerDao对象,名为icdao,这就是控制反转原理的实现。但是,在Spring中,还有一个重要的功能,那就是依赖注入。

       依赖注入,通俗地说,就是可以由配置文件决定向某个对象中存入值。

3.1、属性注入:

(1)、如果属性是简单数据,属性注入格式如下:

<property name=”属性名”>

       <value>值<value>

</property>

(2)、如果是一个对象,则属性注入格式如下:

<property name=”属性名”>

       <ref local=”对象名” />

</property>

3.2、构造函数注入:

(1)、如果属性是简单数据,则构造函数注入格式如下:

<constructor-arg index=”参数序号” type=”参数类型”>

       <value>参数的值</value>

</constructor>

(2)、如果是一个对象,构造函数注入的格式如下:

<constructor-arg index=”参数序号” type=”参数类型” ref=”对象名”>

</constructor-arg>

注意:属性注入的特点是不需要知道属性类型,但必须知道属性名称;

       构造函数注入的特点是不需要知道参数名称,但必须知道参数的序号和类型。

4、其他问题

4.1、Bean的初始和消亡函数:

       在Spring配置文件的bean标签中,可以设置初始化函数和消亡函数:

<bean id=”testInit” class=”test.TestInit”

         init-method=”init”

         destroy-method=”destroy” >

</bean>

…                              

       表示实例化test.TestInit的对象时,构造函数调用之后,自动调用init函数,该对象消亡时,自动调用destroy函数。

注意:初始化函数和消亡函数不能有参数。

         初始化函数可以在其他

4.2、延迟加载:

       在Spring配置文件的bean标签中,可以设置该bean是否为延迟加载。

例:

<bean id=”testLazy” class=”test.TestLazy” lazy-init=”false”></bean>

       表示实例化testLazy对象不延迟加载。

原文地址:https://www.cnblogs.com/Mindreader/p/5540678.html