spring4 之 helloworld

1、从官网下载相关JAR包

spring-framework-4.2.1.RELEASE-dist(下载地址:http://maven.springframework.org/release/org/springframework/spring/

commons-logging-1.2-bin(下载地址:http://commons.apache.org/proper/commons-logging/download_logging.cgi)

 

2、新建JAVA PROJECT项目

 

3、从项目中引入(1)内的JAR包

 

4、SRC内新建2个类文件Helloworld.java & Main.java代码如下。

复制代码
package com.bai.spring.beans;
public class Helloworld {
    private String name;
    public void setName(String name){
        this.name=name;
    }
    public void hello(){
        System.out.println("hello:"+name);
    }
}
复制代码

 

复制代码
package com.bai.spring.beans;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
    public static void main(String []args){
        /*    不使用springde 情况
         * //创建一个HelloWorld对象
        HelloWorld helloworld=new HelloWorld();
        //为对象赋值
        helloworld.setName("baixl");
        //调用hello()方法
        */
        ApplicationContext ctx=new ClassPathXmlApplicationContext("SPRING.xml");
        Helloworld helloworld=(Helloworld) ctx.getBean("helloworld");
        helloworld.hello();
    }
}
复制代码

 

5、新建SRPING.XML文件

在SRC根目录建立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 -->
    <bean id="helloworld" class="com.bai.spring.beans.Helloworld">
        <property name="name" value="Spring"></property>
    </bean>
</beans>
复制代码

 

6、RUN AS -> JAVA APPLICATION 运行。

 

7、完成。正确结果显示如下

 

十月 13, 2015 3:51:16 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@19a5dff: startup date [Tue Oct 13 15:51:16 CST 2015]; root of context hierarchy
十月 13, 2015 3:51:17 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationcontext.xml]
hello:Spring

 

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

重点提示:

1、在我们运行的时候很多时候会提示”Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogF“这个信息,很多时候是因为我们没有引用commons-logging-1.2-bin的JAR包,引用上即可。

2、如上为SRPING的基本入门。

 

运行流程:

首先会运行main()语句,Spring框架使用ClassPathXmlApplicationContext()首先创建一个容器。
这个容器从Beans.xml中读取配置信息,并根据配置信息来创建bean(也就是对象),每个bean有唯一的id。
然后通过context.getBean()找到这个id的bean,获取对象的引用。
通过对象的引用调用printMessage()方法来打印信息。
好了,这是你的第一个Spring应用。你已经学会用Eclipse新建Java项目,导入Spring和commons-logging库,编写Java源代码和XML配置文件,并且成功运行了。如果要更改输出,只需要修改XML文件中的value值,而不需要更改Java源文件。

posted on   范兵  阅读(669)  评论(0编辑  收藏  举报

编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
历史上的今天:
2014-10-13 ANDROID 开发,安装离线安装包的下载地址及安装方法。

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示