为了账号安全,请及时绑定邮箱和手机立即绑定

Spring 一二事(1)

标签:
Java

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

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

https://img1.sycdn.imooc.com//5af3bdd20001686102430071.jpg

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

https://img1.sycdn.imooc.com//5af3bded00017a5e03280358.jpg

内容如下:

<?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!");

    }

}


测试:


package com.lee.spring001.createobject;


import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;


public class HelloWorldTest {


    @Test

    public void testHelloWorld() {

        

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

        

        HelloWorld hello = (HelloWorld)context.getBean("helloWorld");

        hello.hello();

    }

}

这样运行junit,就成功了


点击查看更多内容
“小礼物走一走,来慕课关注我”
赞赏支持
风间影月说 去围观
创业公司技术总监, 10年+开发和技术管理经验。SUN认证SCJP、PMP、MCP认证。主要从事后端技术和架构领域,有丰富的电商平台与物流平台核心系统的架构设计和开发经验。
评论

作者其他优质文章

正在加载中
全栈工程师
手记
粉丝
1.2万
获赞与收藏
904

关注作者,订阅最新文章

阅读免费教程

感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消