Spring 容器的基本用法】的更多相关文章

容器的基本用法 bean 是 Spring 中最核心的东西,因为 Spring 就像是个大水桶,而 bean 就像是容器中的水,水桶脱离了水也没什么用处了,来看看 bean 的定义. public class MyTestBean { private String testStr = "testStr"; public String getTestStr() { return testStr; } public void setTestStr(String testStr) { thi…
7.5 Spring容器中的Bean 7.5.1 Bean的基本定义和Bean别名 <beans.../>元素是Spring配置文件的根元素,该元素可以指定如下属性: default-lazy-init : 指定该<beans.../> 元素下配置的所有Bean默认的延迟初始化行为. default-merge : 指定该<beans.../> 元素下配置的所有Bean默认的merge行为. default-autowire : 指定该<beans.../>…
7.4 使用 Spring 容器 Spring 有两个核心接口:BeanFactory 和 ApplicationContext,其中ApplicationContext 是 BeanFactory 的子接口.它们都可代表 Spring 容器,Spring 容器是生成 Bean 实例的工厂,并管理容器中的Bean. Java 程序面向接口编程,无须关心 Bean 实例的实现类:但 Spring 容器负责创建 Bean 实例,因此必须精确知道每个 Bean 实例的实现类,故Spring 配置文件必…
Spring定时器Quartz的用法也很简单,需要引入quartz-all-1.5.2.jar java代码如下: package com.coalmine.desktop; import java.text.SimpleDateFormat; import java.util.Date; public class QuartzJob {    public void work() {      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-…
有时在Spring(3.2.5)项目中,如果使用到Servlet,可能希望Servlet实例作为bean受Spring容器管理,这样也能自动注入其他需要的bean,查了下,发现只针对过滤器提供了代理类org.springframework.web.filter.DelegatingFilterProxy,并没有提供针对Servlet的代理类,于是模仿着写了下面的代理类:  package org.springframework.web.servlet; import java.io.IOExce…
如果我们需要对我们的Service方法作单元测试,恰好又是用Spring作为IOC容器的,我们可以这么配置Junit加载Spring容器,方便做单元测试. > 基本的搭建 (1)引入所需的包 <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test<…
方法一:在初始化时保存ApplicationContext对象方法二:通过Spring提供的工具类获取ApplicationContext对象方法三:继承自抽象类ApplicationObjectSupport方法四:继承自抽象类WebApplicationObjectSupport方法五:实现接口ApplicationContextAware 常用的5种获取spring 中bean的方式总结: 方法一:在初始化时保存ApplicationContext对象 ApplicationContext…
好久没有写博客了,今天闲下来将之前未完成的表达出来. 在之前的文章自己动手写spring容器(2)中完成了对spring的依赖注入的实现,这篇将会介绍spring基于注解的依赖注入的实现. 在一般的Java开发中,最常接触到的可能就是@Override和@SupressWarnings这两个注解了.使用@Override的时候只需要一个简单的声明即可.这种称为标记注解(marker annotation ),它的出现就代表了某种配置语义.而其它的注解是可以有自己的配置参数的.配置参数以名值对的方…
之前都是说了怎么配置bean以及用法之类的,这篇博文来介绍下spring容器内幕. 内部容器工作机制 Spring中AbstractApplicationContext抽象类的refresh()方法是用来刷新Spring的应用上下文的. @Override public void refresh() throws BeansException, IllegalStateException { //初始化BeanFactory prepareBeanFactory(beanFactory); //…
在开发中,总是能碰到用注解注入不了Spring容器里面bean对象的问题.为了解决这个问题,我们需要一个工具类来直接获取Spring容器中的bean.因此就写了这个工具类,在此记录一下,方便后续查阅.废话不多说,直接上代码. 一.代码 package com.zxy.demo.spring; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext;…