Spring环境及写一个HelloWord

Spring环境

 

使用Spring 框架需要用做好的准备:

  1. Apache Commons logging
  2. spring-framework(Spring框架库)
  3. JDK

Commons logging下载地址:http://commons.apache.org/proper/commons-logging/download_logging.cgi

spring-framework 下载地址:

http://repo.spring.io/release/org/springframework/spring/

 

下载好之后先创建一个java Project,然后在创建的工程下创建一个lib文件夹

      

在lib目录下引入需要的jar包

相关的jar包在刚刚下载的Commons logging的目录下和spring-framework-4.1.6.RELEASE\libs 目录下

 

引入之后选中lib下所有的jar包右键Build Path→Configure Build path

 

使用Spring写一个HelloWord

 

创建一个com.user包在包下面创建一个User类

代码如下:

public class User {

   private String message;

  

   public void setMessage(String message) {

      this.message = message;

   }

   public void show()

   {

      System.out.println("message=  "+message);

   }

 

}

创建一个Test类 com.test.Test

代码:

public static void main(String[] args) {

      //

      ApplicationContext context=new ClassPathXmlApplicationContext("Beans.xml");

     

      User user=(User)context.getBean("user");

      user.show();

   }

 

在Src下创建Bean的配置文件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

        http://www.springframework.org/schema/beans/spring-beans.xsd">

  

   <bean name="user" class="com.user.User">

      <property name="message" value="Hello,Word"></property>

   </bean>       

      

</beans>

 

关于Beans标签的配置文件头可以在下载的Spring框架的spring-framework-4.1.6.RELEASE\docs\spring-framework-reference\html目录下打开相关页面可以找到

如果以上步骤都没有出错的话运行Test.java就可以看到如下结果

猜你喜欢

转载自blog.csdn.net/u012777599/article/details/88226040
今日推荐