Spring bean工厂配置头文件

2023-03-14,,

命名 beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- bean就是java对象的实体类 由spring容器创建和管理 -->
<!-- bean 工厂 -->
<bean name="hello" class="cn.spring.bean.Hello">
<property name="name" value="张三"></property>
</bean> </beans>

导包

Hello.java代码

    public Hello(){
System.out.print("hello 被创建\n");
}
private String name;
public void setName(String name){
this.name=name;
}
public String getName() {
return name;
} public void ShowName(){
System.out.print("holle"+name);
}

演示test类的执行

        //解析 beans.xml文件  生成并管理相应的对象
ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
Hello ho=(Hello)context.getBean("hello");
ho.ShowName();
//1.hello对象是spring容器创建的
//2.对象的属性也是由 spring容器来创建的
//这个过程就叫控制反转 ioc
//控制的内容:指的是 谁来控制对象的创建
//控制:就是指创建对象的过程
//使用了spring以后是由spring来创建的
//ioc 容器实现的ioc,ioc容器就是bean工厂

附上 spring框架版本大全  https://repo.spring.io/list/libs-release-local/org/springframework/spring/

Spring bean工厂配置头文件的相关教程结束。

《Spring bean工厂配置头文件.doc》

下载本文的Word格式文档,以方便收藏与打印。