spring----bean的使用

时间:2023-03-10 03:55:19
spring----bean的使用

这篇文章不介绍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>

文件结构:

spring----bean的使用

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);
}
}

输出结果:

spring----bean的使用

这种配置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的使用