使用Maven管理Java项目中的依赖

之前的博客里说以后学习在项目中使用Maven,说干就干。

MyEclipse集成Maven

可以参见http://huihai.iteye.com/blog/1993972,这里不再赘述。

创建Maven项目

过程如下:

  • 创建Maven项目

  • 填写项目相关信息

  • 填写项目名称、包名、打包类型

  • 项目创建成功的目录列表

使用添加Spring框架的依赖项

  • 修改pom.xml文件,添加Spring依赖

  • 保存后,MyEclipse会自动build项目

  • 等到bulid过程结束,依赖项就可以用了

测试代码

TestSpring.java

package com.mrbcy.maven.firstmavenspring;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class TestSpring {
    public TestSpring(){
        System.out.println("test spring 运行了");
    }
    public static void main(String[] args) {
        new ClassPathXmlApplicationContext("applicationContext.xml");
    }
}

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.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.mrbcy.maven.firstmavenspring"/>


</beans>

运行结果:

二月 03, 2017 1:27:42 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3a6bf80b: startup date [Fri Feb 03 01:27:42 CST 2017]; root of context hierarchy
二月 03, 2017 1:27:42 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
test spring 运行了

完美。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值