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配置文件结束 -->
阅读全文
0 0
- Bean获取Spring容器
- Spring容器注入bean
- Spring Bean容器
- Spring Bean 容器
- Bean感知Spring容器
- Bean获取Spring容器
- Spring容器中的Bean
- 【Spring】Spring容器获取Bean
- spring容器中的bean创建
- Spring容器-Bean的生命周期
- Spring容器-Bean的生命周期
- Spring容器动态注入bean
- 让bean获取spring容器
- Spring之核心容器bean
- Spring之核心容器bean
- spring容器和装配Bean:
- 让Bean获取Spring容器
- Spring基础—Bean容器
- springmvc(3)--非注解的处理器映射器和适配器
- [Linux] DEVICE_ATTR
- Android学习之shape属性与用法
- 自定义绘制圆形和弧形进度条
- 说说如何使用 HTML5 实现拖放功能(使用原生 API)
- Spring Bean 容器
- docker的五种网络模式总结
- Eclipse Debug 界面应用详解——Eclipse Debug不为人知的秘密
- MoviePy 数据动态可视化 图像转动态图或者视频
- NCO3.0连接SAP报错处理Could not load file or assembly 'sapnco' or one of its dependencies
- 在 MVVM 中使用 RACSubject 可以非常方便地实现统一的错误处理逻辑
- POJ3522 Slim Span(kruskal)
- 7-spring源码3.2.18解读+spring技术内幕(关于BeanDefinition的注册)
- Java项目跨域问题解决方案总结