您的位置:首页 > 编程语言 > Java开发

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

2017-02-03 01:43 585 查看
之前的博客里说以后学习在项目中使用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 运行了


完美。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: