这篇文章不介绍spring的相关概念,只记录一下springbean的创建方式和使用方式。

一、bean的创建和调用

1、创建演示需要用到的类:Student、Teacher、Person

package test.spring.school;

public class Student {
private String name;
private Integer age; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} @Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
} }
2、spring的xml配置
<?xml version="1.0" encoding="UTF-8"?>
<!-- beans里的属性是spring的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:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
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/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> <bean id="student" class="test.spring.school.Student"/>
</beans>

文件结构:

3、测试代码

package test.spring;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import test.spring.school.Student; public class TestApp {
@Test
public void testXml() {
@SuppressWarnings("resource")
ApplicationContext ctx = new ClassPathXmlApplicationContext("resources/config.xml");
//根据bean的id查找
Student student = (Student)ctx.getBean("student");
System.out.println(student);
}
}

输出结果:

这种配置bean的方法,是通过类的构造器生成的,所有必须要保证该类有构造器。因为java默认存在无参构造器,可以直接实例化。但是,如果手动添加有参数构造器,必须给属性注入参数值,否则会报错

修改Student类,添加有参构造器:

    public Student(String name, Integer age) {
super();
this.name = name;
this.age = age;
}

测试输出:

二、bean的其他属性的相关配置

1、作用域的配置

  可以通过<bean>定义的scope属性,该属性值得含义:

  singleton,在每个spring loc容器中,一个bean对应一个对象实例,默认项。

  prototype,一个bean对应多个实例。

  request,在一次HTTP请求中,一个bean对应一个实例,仅限Web环境。

  session,在一个HTTP Session中 ,一个bean定义对应一个实例,仅限于Web环境。

  global Session,在一个全局的HttpSession中,一个bean定义对应一个实例;仅在基于portlet的Web应用中才有意义,portlet规范定义了全局Session的概念。

2、初始和销毁方法

  在<bean>的init-method属性和destroy-method属性定义。

  在<beans>元素中的default-init-method和default-destroy-method中给所有的bean定义全局初始和销毁方法

3、延迟实例化

  在ApplicationContext启动时,默认会将所有singleton bean提前进行实例化,如果不想让一个singleton在启动时实例化,可以使用<bean>元素的lazy-init属性改变。

  同样可以在<beans>元素中使用default-lazy-init属性,实现全局<bean>延迟实例化。

配置:

<bean id="student" class="test.spring.school.Student"
scope="prototype" init-method="init" destroy-method="destroy" lazy-init="true"/>

测试:

@Test
public void testXml() {
@SuppressWarnings("resource")
ApplicationContext ctx = new ClassPathXmlApplicationContext("resources/config.xml");
//根据bean的id查找
Student student = (Student)ctx.getBean("student"); System.out.println(student);
student.destroy();
}

输出结果:

spring----bean的使用的更多相关文章

  1. Spring8:一些常用的Spring Bean扩展接口

    前言 Spring是一款非常强大的框架,可以说是几乎所有的企业级Java项目使用了Spring,而Bean又是Spring框架的核心. Spring框架运用了非常多的设计模式,从整体上看,它的设计严格 ...

  2. Spring Bean详细讲解

    什么是Bean? Spring Bean是被实例的,组装的及被Spring 容器管理的Java对象. Spring 容器会自动完成@bean对象的实例化. 创建应用对象之间的协作关系的行为称为:装配( ...

  3. Spring Bean的生命周期(非常详细)

    Spring作为当前Java最流行.最强大的轻量级框架,受到了程序员的热烈欢迎.准确的了解Spring Bean的生命周期是非常必要的.我们通常使用ApplicationContext作为Spring ...

  4. spring bean的生命周期

    掌握好spring bean的生命周期,对spring的扩展大有帮助.  spring bean的生命周期(推荐看)  spring bean的生命周期

  5. spring bean的重新加载

    架构体系 在谈spring bean的重新加载前,首先我们来看看spring ioc容器. spring ioc容器主要功能是完成对bean的创建.依赖注入和管理等功能,而这些功能的实现是有下面几个组 ...

  6. Spring Bean

    一.Spring的几大模块:Data access & Integration.Transcation.Instrumentation.Core Spring Container.Testin ...

  7. 【转】Spring bean处理——回调函数

    Spring bean处理——回调函数 Spring中定义了三个可以用来对Spring bean或生成bean的BeanFactory进行处理的接口,InitializingBean.BeanPost ...

  8. 在非spring组件中注入spring bean

    1.在spring中配置如下<context:spring-configured/>     <context:load-time-weaver aspectj-weaving=&q ...

  9. spring bean生命周期管理--转

    Life Cycle Management of a Spring Bean 原文地址:http://javabeat.net/life-cycle-management-of-a-spring-be ...

  10. Spring Bean配置默认为单实例 pring Bean生命周期

    Bean默认的是单例的. 如果不想单例需要如下配置:<bean id="user" class="..." scope="singleton&q ...

随机推荐

  1. Win10系统下在国内访问Tensorflow官网

    1.修改hosts文件 目录:     C:\Windows\System32\drivers\etc 添加: #TensorFlow start64.233.188.121 www.tensorfl ...

  2. 依赖注入[2]: 基于IoC的设计模式

    正如我们在<控制反转>提到过的,很多人将IoC理解为一种"面向对象的设计模式",实际上IoC自身不仅与面向对象没有必然的联系,它也算不上是一种设计模式.一般来讲,设计模 ...

  3. 安卓开发学习笔记(二):如何用Android Stuidio在res资源下创建xml视图文件

    笔者在看了相关的教程之后发现教程当中的资源已经过时了.当我们在创建了一个新的空白的工程之后,会发现其文件夹下面的分文件夹目录和官方的教程文件结构完全不同,因此会引起很多误解.笔者使用的是最新版的And ...

  4. 动态规划----最长公共子序列(LCS)问题

    题目: 求解两个字符串的最长公共子序列.如 AB34C 和 A1BC2   则最长公共子序列为 ABC. 思路分析:可以用dfs深搜,这里使用到了前面没有见到过的双重循环递归.也可以使用动态规划,在建 ...

  5. 分享13道上海尚学堂拿回来的Java面试真题,这些都是Java核心常见问题,想拿OFFER必看!

    上海尚学堂Java培训学员参加面试带回来的真题,分享出来与大家,希望大家能认真地看看做一遍.后面有详细题解答案,对照下,看看自己做得怎么样,把这些面试遇到的真题全部掌握,做好面试笔试前的准备. 一.1 ...

  6. Redis 设计与实现 (九)--Lua

    EVAL script numkeys key [key ...] arg [arg ...] script:     你的lua脚本 numkeys:  key的个数 key:           ...

  7. 设置radio选中

    选中: $('.viewradio:input[name="istop"][value="' + getSelected().istop + '"]').pro ...

  8. BBS论坛(十八)

    18.首页轮播图实现 (1)front/css/front_base.css .main-container{ width: 990px; margin: 0 auto; overflow: hidd ...

  9. Python爬虫入门教程 16-100 500px摄影师社区抓取摄影师数据

    写在前面 今天要抓取的网站为 https://500px.me/ ,这是一个摄影社区,在一个摄影社区里面本来应该爬取的是图片信息,可是我发现好像也没啥有意思的,忽然觉得爬取一下这个网站的摄影师更好玩一 ...

  10. 将Maple输出的LaTex导出到txt文件

    将Maple输出的LaTex导出到txt文件 1. 生成LATEX Maple可以把它的表达式转换成LATEX, 使用latex命令即可: > latex(x^2+y^2=z^2); {x}^{ ...