Spring基础

来源:互联网 发布:淘宝买家黑名单网站 编辑:程序博客网 时间:2024/06/05 04:20

1,概述

Spring是一个依赖注入,控制反转的轻量级容器。

通常我们的程序都是三层,应用层,Service层,Dao层。

由应用层持有Service层的对象,调用Service层的方法实现应用;而Service层持有Dao层的对象,调用Dao层的方法实现Service。这些对象需要自己维护自己持有的对象。

而Spring这个容器只需将这些层的类注册到配置文件中,将需要持有那些对象声明,在调用时,Spring会自动维护这些对象,帮助我们new对象和维护对象。

 

Spring还具有面向切面编程特性,可以为某个类配置拦截器,在调用某个方法前调用拦截器方法。

 

Spring有七大模块:

Core:主要实现控制反转

AOP:主要实现面向切面特性

ORM:封装了主流ORM框架

DAO:统一管理JDBC资源和事务

Web:管理Struts,JSF,WebWork等Web框架

Context:提供类似JNDI机制访问各个类

Web MVC:Spring自己实现的Web MVC框架,比Struts简单

 

2,控制反转

控制反转中有几个重要角色:

Bean:交给Spring管理的类;

ApplicationContext:配置文件;

BeanDefinition:Bean在配置文件中的定义;

BeanFactory:创建Bean的工厂

1>例子

Dao层

public interface IDao {public void curd();}
public class DaoImpl implements IDao{public void curd() {System.out.println("数据库增删改查完毕");}}

Service层

public interface IService {public void service();}
public class ServiceImpl implements IService{private IDao dao;public IDao getDao() {return dao;}public void setDao(IDao dao) {this.dao = dao;}public void service() {dao.curd();}}

应用层

import org.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.core.io.ClassPathResource;import com.hellobbboy.service.IService;public class Main {public static void main(String[] args) {XmlBeanFactory xmlBeanFactory=new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));IService service=(IService)xmlBeanFactory.getBean("serviceImpl");service.service();}}

配置

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="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-3.0.xsd"><bean id="daoImpl" class="com.hellobbboy.dao.DaoImpl"></bean><bean id="serviceImpl" class="com.hellobbboy.service.ServiceImpl"><property name="dao" ref="daoImpl"></property></bean></beans>


2><bean>配置详解

a>属性

id和class是必须有的;

单例模式:singleton="false";//Spring默认为单例

工厂模式:factory-method="newInstanceMethod"

初始化方法:init-method="initMethod"

销毁方法:destory-method="destoryMethod"

依赖对象:depends-on="anotherBean"(Spring默认按配置顺序加载Bean,如配置依赖对象,则在实例化前,先实例化依赖的对象)

b>子元素

1,注入其他bean:(最常用)

<property name="dao">
 <ref bean="dao"/>
</property>


2,注入集合:

List集合:
<property name="list">
 <list>
  <value></value>
 </list>
</property>
Set集合:

<property name="set">
 <set>
  <value></value>
 </set>
</property>
Map集合:

<property name="map">
 <map>
  <entry key="key">
   <value>value</value>
  </entry>
 </map>
</property>


3,注入Properties:

<property name="props">
 <props>
  <prop key="key">value</prop>
 </props>
</property>


PS:可以发现,除了注入Bean比较方便,其他类型的注入并不方便,企业通常使用配置文件方式,特别是数据库,但需导入一个Spring的Bean:

<!-- 导入并配置读取属性的Bean -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 

<property name="location" value="classpath:bean.properties"/>

</bean>

这时配置其他类型是就可:

<bean id="oneBean" class="OneBean">

 <property name="key" value="${value}"/>

</bean>

而数据写在放在classpath下的bean.properties中:

value=param


4,构造函数参数:

<bean id="oneBean" class="OneBean">
 <constructor-arg><ref bean="anotherBean"/></constructor-arg>
 <constructor-arg><value>1<value/></constructor-arg>
</bean>

 

3,面向切面编程(AOP)

AOP把一个业务流程分成几个部分,例如权限检查、业务处理、日志记录,然后组装成完整的业务流程。

 

4,Best Practise

1>随web容器自动加载Bean:

在web.xml配置:

<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>
   classpath*:daoContext.xml
   classpath*:repositoryContext.xml
   classpath*:presentationContext.xml
   classpath*:serviceContext.xml
    </param-value>
 </context-param>