Spring Bean 容器

来源:互联网 发布:汤珈铖 知乎 编辑:程序博客网 时间:2024/05/16 08:48

Spring 对bean的使用 有两种方式
方式一:基于xml的配置方式

<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"       xmlns:jdbc="http://www.springframework.org/schema/jdbc"        xmlns:jee="http://www.springframework.org/schema/jee"       xmlns:tx="http://www.springframework.org/schema/tx"        xmlns:aop="http://www.springframework.org/schema/aop"    xmlns:jpa="http://www.springframework.org/schema/data/jpa"      xsi:schemaLocation="          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd          http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd          http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"      default-lazy-init="true">  <!--相当于Api api = new ImplOne();-->  <bean name="test" class="cn.javass.spring3.hello.ImplOne"></bean></beans>

方式二:注解

Spring Bean容器初始化用到的两个基础包

org.springframework.beans包
BeanFactory类和子类 提供配置结构和基本功能,加载并初始化Bean

org.springframework.context包
ApplicationContext类和子类 保存Bean对象

初始化ApplicationContext的三种方式
方式1:加载本地文件

import org.springframework.context.support.ClassPathXmlApplicationContext;FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("F:/workspace/appcontext.xml");

方式2:Classpath(相对于工程的位置)

import org.springframework.context.support.FileSystemXmlApplicationContext;ClassPathXmlApplicationContext context2 = new ClassPathXmlApplicationContext("classpath:spring-context.xml");

方式3:Web应用中 依赖Servlet或Listener初始化ApplicationContext

 <listener>        <description>spring监听器</description>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
  <servlet>        <servlet-name>context</servlet-name>        <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>         <load-on-startup>1</load-on-startup>    </servlet>

Spring 配置文件

    <!-- Spring配置文件开始  -->    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>            classpath:applicationContext.xml                 </param-value>    </context-param>    <listener>        <description>spring监听器</description>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>     <!-- 防止spring内存溢出监听器 -->    <listener>        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>    </listener>     <servlet>        <servlet-name>spring</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>classpath:spring-mvc.xml</param-value>        </init-param>        <load-on-startup>1</load-on-startup>        <async-supported>true</async-supported>    </servlet>    <servlet-mapping>        <servlet-name>spring</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping>      <!-- Spring配置文件结束 -->
原创粉丝点击