spring学习6-使用xml方式实现spring基本应用

简介: spring学习6-使用xml方式实现spring基本应用

image.png

image.png

applicationconText.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 http://www.springframework.org/schema/beans/spring-beans.xsd">
         <!--
         bean元素:描述当前的对象需要由spring容器管理
         id属性:标识对象 未来在应用程序中可以根据id获取对象
         class对象:被管理的对象的全名
        -->
         <bean id="service" class="hello.MesasageService"></bean>
         <bean id="printer" class="hello.MessagePrinter">
             <property name="service" ref="service"></property>
         </bean>
     </beans>MesasageService 类
    package hello;
     //去掉注解
     public class MesasageService {
         public MesasageService() {
             super();
             System.out.println("MessageService...");
         }
         /**
          * 执行打印功能
          * @return 返回要打印的字符串
          */
         public String getMessage(){
             return "Hello World";
         }
     }MessagePrinter类
    package hello;
     //去掉注解
     public class MessagePrinter {
         //无参构造方法的加入
         public MessagePrinter() {
             super();
             System.out.println("MessagePrinter...");
         }
         private MesasageService service;
         /*
         * 简历和MessageService的关系
         * */
         //设置service的值
         //创建关联关系
         public void setService(MesasageService service){
             this.service =service;
         }
         public void printMessage(){
             System.out.println(this.service.getMessage());
         }
     }ApplicationSpring类
    package hello;
     import org.springframework.context.ApplicationContext;
     import org.springframework.context.annotation.AnnotationConfigApplicationContext;
     import org.springframework.context.support.ClassPathXmlApplicationContext;
     //加入扫描
     public class ApplicationSpring {
         public static void main(String[] args){
             System.out.println("appliaction....");
             //初始化spring
             ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml");
             //获取messagePrinter对象
             MessagePrinter printer=context.getBean(MessagePrinter.class);
             printer.printMessage();
         }
     }

运行结果

   appliaction....

   10月 27, 2019 7:37:17 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh

   信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1175e2db: startup date [Sun Oct 27 19:37:17 CST 2019]; root of context hierarchy

   10月 27, 2019 7:37:17 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

   信息: Loading XML bean definitions from class path resource [applicationContext.xml]

   MessageService...

   MessagePrinter...

   Hello World


相关文章
|
4天前
|
XML SQL 数据处理
学习 XQuery:XML数据查询的关键
XQuery是用于查询XML数据的语言,类似SQL对数据库的操作。它基于XPath构建,用于从XML文档中提取和排序元素。FLWOR表达式(For, Let, Where, Order by, Return)是其核心,用于处理和过滤数据。例如,示例代码展示了如何选取&quot;books.xml&quot;中价格大于30的书籍并按标题排序。XQuery还可用于Web服务、生成报告、XML到XHTML转换及搜索文档等场景。了解XPath能帮助更好地掌握XQuery,两者有相似的数据模型和函数。
26 0
|
1月前
|
监控 Java 数据处理
【Spring云原生】Spring Batch:海量数据高并发任务处理!数据处理纵享新丝滑!事务管理机制+并行处理+实例应用讲解
【Spring云原生】Spring Batch:海量数据高并发任务处理!数据处理纵享新丝滑!事务管理机制+并行处理+实例应用讲解
|
25天前
|
安全 Java 数据安全/隐私保护
【深入浅出Spring原理及实战】「EL表达式开发系列」深入解析SpringEL表达式理论详解与实际应用
【深入浅出Spring原理及实战】「EL表达式开发系列」深入解析SpringEL表达式理论详解与实际应用
54 1
|
15天前
|
XML JSON JavaScript
Java中XML和JSON的比较与应用指南
本文对比了Java中XML和JSON的使用,XML以自我描述性和可扩展性著称,适合结构复杂、需验证的场景,但语法冗长。JSON结构简洁,适用于轻量级数据交换,但不支持命名空间。在Java中,处理XML可使用DOM、SAX解析器或XPath,而JSON可借助GSON、Jackson库。根据需求选择合适格式,注意安全、性能和可读性。
25 0
|
20天前
|
存储 安全 Java
Spring Security应用讲解(Java案列演示)
Spring Security应用讲解(Java案列演示)
|
20天前
|
XML JSON JavaScript
使用JSON和XML:数据交换格式在Java Web开发中的应用
【4月更文挑战第3天】本文比较了JSON和XML在Java Web开发中的应用。JSON是一种轻量级、易读的数据交换格式,适合快速解析和节省空间,常用于API和Web服务。XML则提供更强的灵活性和数据描述能力,适合复杂数据结构。Java有Jackson和Gson等库处理JSON,JAXB和DOM/SAX处理XML。选择格式需根据应用场景和需求。
|
25天前
|
Prometheus 监控 Cloud Native
Spring Boot 应用可视化监控
Spring Boot 应用可视化监控
15 0
|
1月前
|
网络安全
ssh(Spring+Spring mvc+hibernate)——applicationContext.xml
ssh(Spring+Spring mvc+hibernate)——applicationContext.xml
7 0
|
1月前
|
XML 存储 JavaScript
深入学习 XML 解析器及 DOM 操作技术
所有主要的浏览器都内置了一个XML解析器,用于访问和操作XML XML 解析器 在访问XML文档之前,必须将其加载到XML DOM对象中 所有现代浏览器都有一个内置的XML解析器,可以将文本转换为XML DOM对象
72 0
|
1月前
|
Cloud Native Java 开发者
Spring Boot 4.0:构建云原生Java应用的前沿工具
Spring Boot 4.0:构建云原生Java应用的前沿工具