Spring 如何配置 bean (XML 方式)

请直接看原文:Spring 如何配置 bean (XML 方式)_spring 在哪配置bean 文件-CSDN博客

--------------------------------------------------------------------------------------------------------------------------------

Java Bean 如何配置配置到 spring 容器中

基于 XML 的配置

通过生成 applicationContext.xml 文件,声明命名空间来配置 Java Bean,XML 基本格式如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
						http://www.springframework.org/schema/util
						http://www.springframework.org/schema/util/spring-util-4.0.xsd"
						>	
	
</beans>

如何配置 Java Bean ?,这里声明一个 Person 类、Car 类,并且生成 getter,setter 方法。

Person. java

package com.test.helloworld;

public class Person {
	private String name;
	private String age;
    private Car car;
    public Car getCar() {
		return car;
	}
	public void setCar(Car car) {
		this.car = car;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	public Person() {
		System.out.println("无参构造函数");
	}
    public Person(String name, String age, Car car) {
		super();
		this.name = name;
		this.age = age;
		this.car = car;
	}
    
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + "]";
	}
	
}

Car.java

package com.test.helloworld;

public class Car {
	private String brand;
	private Double price;
	public String getBrand() {
		return brand;
	}
	public void setBrand(String brand) {
		this.brand = brand;
	}
	public Double getPrice() {
		return price;
	}
	public void setPrice(Double price) {
		this.price = price;
	}
	@Override
	public String toString() {
		return "Car [brand=" + brand + ", price=" + price + "]";
	}
	
}

依赖注入:属性注入

也就是 setter 注入,通过 setter 方法进行属性注入。

再 xml 文件的 beans 标签中添加 bean 标签,书写格式如下

	<bean id="person" class="com.test.helloworld.Person">
		<property  name="name" value="小明"></property>
		<property  name="age" value="20"></property>
        <property  name="car" ref="car1"></property>
        
	</bean>
    <bean id="car1" class="com.test.helloworld.Car">
            <property name="brand" value="BMW"></property>
            <property name="price" value="350000"></property>
      </bean>

其中:

bean : 表示配置一个 bean,这个 bean 会在 IOC 容器启动的时候,被 IOC 容器加载(实例化),并且在 IOC 容器中管理这个 bean

  • id : bean的名称,是一个唯一的名称

  • class : 这个bean对应的类型的全类名

  • property : 这个标签表示给 IOC 容器中管理的这个 bean 设置属性

    • name : 属性名 ( 对应的是JavaBean风格的属性名 , 也就是 setter 方法 )
    • value 或者 value 标签 : 需要设置的属性值
    • ref 或者 ref 标签 :表示引用其他的bean

依赖注入:构造注入

我们的字段可以采用有参构造函数初始化,可以通过构造函数注入

    <bean id="person" class="com.test.helloworld.Person">
            <constructor-arg value="小王"></constructor-arg>
            <constructor-arg value="18"></constructor-arg>
            <constructor-arg ref="car1"></constructor-arg>
      </bean>
       <bean id="car1" class="com.test.helloworld.Car">
            <property name="brand" value="BMW"></property>
            <property name="price" value="350000"></property>
      </bean>

参数顺序

这里是按照参数的顺序, 我们也可以通过设置添加 index 属性来设置参数顺序。

    <bean id="person" class="com.test.helloworld.Person">
                <constructor-arg value="18" index="0" ></constructor-arg>
                <constructor-arg value="小王" index="1"></constructor-arg>
                <constructor-arg ref="car1" index="3"></constructor-arg>
       </bean>
       <bean id="car1" class="com.test.helloworld.Car">
            <property name="brand" value="BMW"></property>
            <property name="price" value="350000"></property>
      </bean>
  • index : 通过 index 属性可以设置构造注入传入参数的顺序,从 0 开始到参数个数 -1 结束

参数类型

还可以通过设置参数(添加 type 属性)的类型,来匹配相应的构造函数,实例化对象。

		<bean id="person" class="com.test.helloworld.Person">
            <constructor-arg value="18" index="0" type="java.lang.String" ></constructor-arg>
            <constructor-arg value="小王" index="1" type="java.lang.String"></constructor-arg>
            <constructor-arg ref="car1" index="3"></constructor-arg>
      </bean>
       <bean id="car1" class="com.test.helloworld.Car">
            <property name="brand" value="BMW"></property>
            <property name="price" value="350000"></property>
      </bean>

最后

通过生成 ApplicationContext 对象(IOC 容器)来获得 bean 对象。

       1 启动 SpringIOC 容器
       ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
       //2 从 IOC 容器中获取 bean
		Person person = (Person)applicationContext.getBean("person");
		//2 使用 bean
		System.out.println(person);

其中,XML 文件现在我的是放在 src 的一级目录下。

-------------------------------------------------------------------------------------------------------------------------------- 

相关推荐

  1. Spring 如何配置 bean (XML 方式)

    2024-02-10 07:00:01       49 阅读
  2. Spring Boot应用程序如何配置 HTTPS 访问方式

    2024-02-10 07:00:01       50 阅读
  3. spring三种配置方式总结

    2024-02-10 07:00:01       40 阅读
  4. Spring 事务管理配置方法

    2024-02-10 07:00:01       24 阅读
  5. Spring Boot 如何进行多环境配置

    2024-02-10 07:00:01       32 阅读
  6. 深入Spring Boot配置机制:如何高效管理应用配置

    2024-02-10 07:00:01       26 阅读
  7. 三种配置Spring程序的方法

    2024-02-10 07:00:01       56 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-10 07:00:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-10 07:00:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-02-10 07:00:01       82 阅读
  4. Python语言-面向对象

    2024-02-10 07:00:01       91 阅读

热门阅读

  1. http&rpc different

    2024-02-10 07:00:01       49 阅读
  2. 使用深度学习对网络摄像头图像进行分类

    2024-02-10 07:00:01       54 阅读
  3. 5 scala的函数式编程简介

    2024-02-10 07:00:01       48 阅读
  4. 《动手学深度学习(PyTorch版)》笔记8.2

    2024-02-10 07:00:01       51 阅读