Spring Bean Xml映射方式
Spring Bean和普通的javaBean没有什么区别,Spring 是个容器,只是为了通过bean.xml来管理(或者说映射)javaBean。
1、定义Spring Bean
1 public class HelloBean {
2 private String helloworld;
3
4 public String getHelloworld() {
5 return helloworld;
6 }
7
8 public void setHelloworld(String helloworld) {
9 this.helloworld = helloworld;
10 }
11
12 public void sayHello() {
13 System.out.println(helloworld);
14 }
15 }
2、定义 beans.xml
2 private String helloworld;
3
4 public String getHelloworld() {
5 return helloworld;
6 }
7
8 public void setHelloworld(String helloworld) {
9 this.helloworld = helloworld;
10 }
11
12 public void sayHello() {
13 System.out.println(helloworld);
14 }
15 }
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
3 "http://www.springframework.org/dtd/spring-beans.dtd">
4
5 <beans>
6 <bean id="helloBean" class="com.spring.ioc.demo.HelloBean">
7 <property name="helloworld">
8 <value>Hello!World!</value>
9 </property>
10 </bean>
11 </beans>
3、Bean 调用
2 <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
3 "http://www.springframework.org/dtd/spring-beans.dtd">
4
5 <beans>
6 <bean id="helloBean" class="com.spring.ioc.demo.HelloBean">
7 <property name="helloworld">
8 <value>Hello!World!</value>
9 </property>
10 </bean>
11 </beans>
Bean 调用我们最常用的是 new 对象,而Spring 则是通过 xml 来映射(反射、亦即Ioc)实现 Bean 的调用的,并且Spring 有多种映射的方法,如下:
1 import org.springframework.beans.factory.BeanFactory;
2 import org.springframework.beans.factory.xml.XmlBeanFactory;
3 import org.springframework.context.ApplicationContext;
4 import org.springframework.context.support.ClassPathXmlApplicationContext;
5 import org.springframework.context.support.FileSystemXmlApplicationContext;
6 import org.springframework.core.io.ClassPathResource;
7 import org.springframework.core.io.FileSystemResource;
8 import org.springframework.core.io.Resource;
9
10 public class HelloBeanTest {
11 public static void main(String[] args) {
12 /**
13 * 当普通 javaBean 使用
14 * */
15 HelloBean hellobean = new HelloBean();
16 hellobean.setHelloworld("Hello World!");
17 System.out.println("new对象直接调用");
18 hellobean.sayHello();
19
20 /**
21 * 通过Spring访问JavaBean组件
22 * 1、BeanFactory 方式
23 * */
24 //1.1用 ClassPathResource 时 beans.xml 可以从classpath中读取XML文件
25 Resource resource = new ClassPathResource("beans.xml");
26 BeanFactory factory = new XmlBeanFactory(resource);
27 hellobean = (HelloBean) factory.getBean("helloBean");
28 System.out.println("ClassPathResource + XmlBeanFactory 调用");
29 hellobean.sayHello();
30
31 //1.2用FileSystemResources 时 beans.xml 可以指定XML定义文件的路径来读取定义文件
32 resource = new FileSystemResource("beans.xml");
33 factory = new XmlBeanFactory(resource);
34 hellobean = (HelloBean) factory.getBean("helloBean");
35 System.out.println("FileSystemResource + XmlBeanFactory 调用");
36 hellobean.sayHello();
37
38 /**
39 * 2、ApplicationContext 方式
40 * 采用BeanFactory对简单的应用程序来说就够了,可以获得对象管理上的便利性。
41 * 但是要获取一些特色和高级的容器功能,可以采用ApplicationContext。
42 * ApplicationContext提供了一些高级的功能,比如:
43 * 2.提供文字消息解析的方法
44 * 3.支持国际化(i18n)消息
45 * 4.可以发布事件,对事件感兴趣的Bean可以接收这些事件
46 *
47 * Spring创始者建议采用ApplicationContext取代BeanFactory。
48 * */
49 //2.1 FileSystemXmlApplicationContext 可以指定XML定义文件的路径来读取定义文件
50 ApplicationContext context = new FileSystemXmlApplicationContext("beans.xml");
51 hellobean = (HelloBean)context.getBean("helloBean");
52 System.out.println("FileSystemXmlApplicationContext 调用");
53 hellobean.sayHello();
54 //2.2 ClassPathXmlApplicationContext 可以从classpath中读取XML文件
55 context = new ClassPathXmlApplicationContext("beans.xml");
56 hellobean = (HelloBean)context.getBean("helloBean");
57 System.out.println("ClassPathXmlApplicationContext 调用");
58 hellobean.sayHello();
59 //2.3 XmlWebApplicationContext 从Web应用程序的文件架构中,指定相对位置来读取定义文件
60 //略
61 }
62 }
2 import org.springframework.beans.factory.xml.XmlBeanFactory;
3 import org.springframework.context.ApplicationContext;
4 import org.springframework.context.support.ClassPathXmlApplicationContext;
5 import org.springframework.context.support.FileSystemXmlApplicationContext;
6 import org.springframework.core.io.ClassPathResource;
7 import org.springframework.core.io.FileSystemResource;
8 import org.springframework.core.io.Resource;
9
10 public class HelloBeanTest {
11 public static void main(String[] args) {
12 /**
13 * 当普通 javaBean 使用
14 * */
15 HelloBean hellobean = new HelloBean();
16 hellobean.setHelloworld("Hello World!");
17 System.out.println("new对象直接调用");
18 hellobean.sayHello();
19
20 /**
21 * 通过Spring访问JavaBean组件
22 * 1、BeanFactory 方式
23 * */
24 //1.1用 ClassPathResource 时 beans.xml 可以从classpath中读取XML文件
25 Resource resource = new ClassPathResource("beans.xml");
26 BeanFactory factory = new XmlBeanFactory(resource);
27 hellobean = (HelloBean) factory.getBean("helloBean");
28 System.out.println("ClassPathResource + XmlBeanFactory 调用");
29 hellobean.sayHello();
30
31 //1.2用FileSystemResources 时 beans.xml 可以指定XML定义文件的路径来读取定义文件
32 resource = new FileSystemResource("beans.xml");
33 factory = new XmlBeanFactory(resource);
34 hellobean = (HelloBean) factory.getBean("helloBean");
35 System.out.println("FileSystemResource + XmlBeanFactory 调用");
36 hellobean.sayHello();
37
38 /**
39 * 2、ApplicationContext 方式
40 * 采用BeanFactory对简单的应用程序来说就够了,可以获得对象管理上的便利性。
41 * 但是要获取一些特色和高级的容器功能,可以采用ApplicationContext。
42 * ApplicationContext提供了一些高级的功能,比如:
43 * 2.提供文字消息解析的方法
44 * 3.支持国际化(i18n)消息
45 * 4.可以发布事件,对事件感兴趣的Bean可以接收这些事件
46 *
47 * Spring创始者建议采用ApplicationContext取代BeanFactory。
48 * */
49 //2.1 FileSystemXmlApplicationContext 可以指定XML定义文件的路径来读取定义文件
50 ApplicationContext context = new FileSystemXmlApplicationContext("beans.xml");
51 hellobean = (HelloBean)context.getBean("helloBean");
52 System.out.println("FileSystemXmlApplicationContext 调用");
53 hellobean.sayHello();
54 //2.2 ClassPathXmlApplicationContext 可以从classpath中读取XML文件
55 context = new ClassPathXmlApplicationContext("beans.xml");
56 hellobean = (HelloBean)context.getBean("helloBean");
57 System.out.println("ClassPathXmlApplicationContext 调用");
58 hellobean.sayHello();
59 //2.3 XmlWebApplicationContext 从Web应用程序的文件架构中,指定相对位置来读取定义文件
60 //略
61 }
62 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述