简单介绍一下spring,一方面带新手入入门,一方面自己也重温一下
第一个小工厂先暂时不用maven,下一个会用maven来来配置

jar包只需要一个,spring版本为2.5(暂时为2.5,后续更新,基本核心都是一样的),引入spring.jar到lib,如下:

Spring 一二事(1)

在src下新建applicationContext.xml文件,结果如下:

Spring 一二事(1)

内容如下:

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">
           
    <!-- 把一个类放入到spring容器中,该类就称为bean -->
    <!-- 描述一个类 id 唯一标示 class 类名 -->
    <bean id="helloWorld" class="com.lee.spring001.createobject.HelloWorld"></bean>

</beans>

在com.lee.spring001.createobject包下新建类HelloWorld.java :

package com.lee.spring001.createobject;

public class HelloWorld {

    public HelloWorld() {
        System.out.println("HelloWorld...");
    }

    /**
     * 
     */
    public void hello() {
        System.out.println("Hello world!");
    }
}

测试:

 1 package com.lee.spring001.createobject;
 2 
 3 import org.junit.Test;
 4 import org.springframework.context.ApplicationContext;
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 
 7 public class HelloWorldTest {
 8 
 9     @Test
10     public void testHelloWorld() {
11         
12         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
13         
15         HelloWorld hello = (HelloWorld)context.getBean("helloWorld");
16         hello.hello();
17     }
18 }

这样运行junit,成功

 

github地址:https://github.com/leechenxiang/maven-spring001-helloworld

相关文章:

  • 2021-11-09
  • 2022-12-23
  • 2022-12-23
  • 2021-06-28
  • 2021-10-23
  • 2021-07-01
  • 2021-11-05
猜你喜欢
  • 2021-11-24
  • 2022-01-25
  • 2021-05-26
  • 2021-07-19
  • 2021-11-30
  • 2021-07-13
相关资源
相似解决方案