zoukankan      html  css  js  c++  java
  • Spring 一二事(1)

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

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

    在src下新建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: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

  • 相关阅读:
    每日日报110
    每日日报109
    每日日报108
    每日日报107
    每日日报106
    每日日报105
    每日日报104
    eclipse新建Maven Project项目后出现The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path问题的解决办法
    0proxy/reflect
    toRefs/toRef/unref/customRef
  • 原文地址:https://www.cnblogs.com/leechenxiang/p/5305154.html
Copyright © 2011-2022 走看看