首页 > 其他分享 >Spring 示例

Spring 示例

时间:2023-01-16 22:56:13浏览次数:32  
标签:xml name 示例 spring jar Student Spring org

在这里,我们将学习创建第一个spring应用程序的简单步骤。要运行此应用程序,我们不使用任何IDE。

我们只是在使用命令提示符。

让我们看看创建spring应用程序的简单步骤

创建Java类


 

1)创建Java类

这是仅包含name属性的简单Java bean类。

package com.nhooo;
public class Student {
private String name;
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public void displayInfo(){
    System.out.println("Hello: "+name);
}
}

这是简单的bean类,仅包含一个带有其getter和setters方法的属性名称。
此类包含一个名为displayInfo()的附加方法,该方法通过问候消息打印学生姓名。

 

 

创建xml文件以提供值

如果使用myeclipse IDE, ,您无需创建xml文件,因为myeclipse可以自己完成此操作。打开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"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="studentbean" class="com.nhooo.Student">
<property name="name" value="Vimal Jaiswal"></property>
</bean>
</beans>

bean 元素用于为给定类定义bean。
bean的 property 子元素指定名为name的Student类的属性。
属性元素中指定的值将由IOC容器在Student类对象中设置。

 

创建测试类

-------

3)创建测试类

创建Java类,例如测试。在这里,我们使用BeanFactory的getBean()方法从IOC容器中获取Student类的对象。

让我们看一下测试类的代码。

package com.nhooo;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class Test {
public static void main(String[] args) {
    Resource resource=new ClassPathResource("applicationContext.xml");
    BeanFactory factory=new XmlBeanFactory(resource);
    
    Student student=(Student)factory.getBean("studentbean");
    student.displayInfo();
}
}
资源对象表示applicationContext.xml文件的信息。
Resource是接口,而 ClassPathResource 是Reource接口的实现类。
BeanFactory 负责返回Bean。 
XmlBeanFactory 是BeanFactory的实现类。
BeanFactory接口中有很多方法。
一种方法是 getBean(),该方法返回关联类的对象。

 

----------

加载spring jar文件

4)加载spring框架所需的jar文件

运行该应用程序主要需要三个jar文件。

org.springframework.core-3.0.1.RELEASE-A

com.springsource.org.apache.commons.logging-1.1.1

org.springframework.beans-3.0.1.RELEASE-A

为了将来使用,您可以下载spring核心应用程序所需的jar文件。

下载Spring的核心jar文件

全部下载spring的jar文件,包括core,web,aop,mvc,j2ee,remoting,oxm,jdbc,orm等。

要运行此示例,您只需要加载spring core jar文件。

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

运行测试类

5)运行测试类

现在运行Test类。您将得到输出Hello: Vimal Jaiswal。

 

 

标签:xml,name,示例,spring,jar,Student,Spring,org
From: https://www.cnblogs.com/cnetsa/p/17056506.html

相关文章

  • Spring Framework的优势
    SpringFramework有很多优点。它们如下:1、预定义模板Spring框架提供了JDBC,Hibernate,JPA等技术的模板。 因此,无需编写过多的代码。它隐藏了这些技术的基本步骤。让我......
  • spring cloud 错误:namingService subscribe failed
    ERROR67472---[错误:main]c.a.cloud.nacos.discovery.NacosWatch:namingServicesubscribefailed解决方案:修改成xuguo就行。......
  • Spring 模块
    Spring框架包含许多模块,例如Core,bean,Context,ExpressionLanguage,AOP,Aspects,Instrumentation,JDBC,ORM,OXM,JMS,Transaction,Web,Servlet,Struts等。这些模块如下图所示,它们分为Tes......
  • 230116_50_SpringBoot入门
    指定自定义的配置文件bill.propertiesbill.properitesname=billage=11happy=falsebirth=2021/12/2通过@PropertySource注解指定自定义的配置文件@PropertyS......
  • Spring 教程
    Spring教程Spring框架是由于软件开发的复杂性而创建的。Spring使用的是基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅仅限于服务器端的开发。......
  • day03-Spring管理Bean-IOC-01
    Spring管理Bean-IOC1.Spring配置/管理bean介绍Bean管理包括两方面:创建bean对象给bean注入属性Bean的配置方式:基于xml文件配置方式基于注解配置方式2.基于X......
  • 手写笔记15:谈谈Spring MVC的工作流程?
    ......
  • 分布式请求链路跟踪(SpringCloud Sleuth + zipkin)
    前言在微服务框架中,一个由客户端发起的请求在后端系统中会经过多个不同的的服务节点调用来协同产生最后的请求结果,每一个前段请求都会形成一条复杂的分布式服务调用链路,链路......
  • 处理分布式事务(SpringCloud Alibaba Seata)
    前言一次业务操作需要跨多个数据源或需要跨多个系统进行远程调用,就会产生分布式事务问题Seata是一款开源的分布式事务解决方案,致力于在微服务架构下提供高性能和简单易用的......
  • 消息总线(Spring Cloud Bus)
    前言SpringCloudBus是什么?SpringCloudBus配合SpringCloudConfig使用可以实现配置的动态刷新。SpringCloudBus是用来将分布式系统的节点与轻量级消息系统链接起来......