Spring系列(二)配置-MAVEN+IDEA版

一:使用IDEA中的maven创建一个Spring项目

点击new -> project
在这里插入图片描述
左侧选择Maven,直接点击Next。第一次使用IDEA的,顶部还要选择Project SDK路径,就是Java的安装路径。
在这里插入图片描述
untitled改成自己的项目名称,finish,在项目文件夹上右键并选择Add Framework Support
在这里插入图片描述
找到Spring,打钩,点击OK。
create empty spring-config.xml选项框建议勾选上,若不勾选则需要在项目处自行创建配置文件
若是你自己以前有导入过项目的包,点击user library;此处建议点击download选项,直接下载虽然创建慢,但是保证可以直接配置都idea上;
在这里插入图片描述
经过上述步骤后,一个全新得maven spring项目就创建好了,其目录结构如下所示:
在这里插入图片描述
将spring-config.xml文件移动到resource文件目录下,自动创建的会生成在java目录下。

二:hello world

在java目录下创建两个类

public class Demo {
    public void hello(){
        System.out.println("hello world");
    }
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest {
    public static void main(String[] args){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");
        Demo demo = applicationContext.getBean("demo",Demo.class);
        demo.hello();
    }
}

spring-config.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 id="demo" class="Demo"></bean>
</beans>

运行
在这里插入图片描述
在这里插入图片描述
结果
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值