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

Maven配置spring(只配置spring)

2015-10-27 15:14 399 查看
下载eclipsej2ee版本,如果不是的话去下一个m2e:http://download.csdn.net/detail/xia744510124/9215555,重启普通eclipse

--到maven官网下载maven包解压到一个指定文件夹下

--到maven解压包下打开conf/settings.xml文件:

加入:<localRepository>根据你自己指定一个本地仓库路径(会存放以后在maven中心仓库下载下来的jar)</localRepository>

--注:也可以不加这句话的,它有一个默认路径(C:\用户\计算机名\.m2\repository)

--在eclipse配置maven路径window-->Preference-->输入maven-->点开有个Installations-->点击填写maven解压路径

--新建一个maven project,一直点下一步,它会让你填一个groupId:也就是我们所说的包名,还有一个artifactId:也就是我们所说的项目名,点击完成finish。

--之后在eclipse中会出现一个maven项目,右键单击-->new-->Soure Folder,文件名为src/main/resources(存放外部资源文件,比如.xml文件)

--在src/main/resources文件夹下新建一个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"

xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean class="com.xll.Student" id="student">

<property name="name" value="xialonglei"/>

<property name="age" value="12"/>

</bean>

</beans>

--打开pom.xml配置文件,里面加入如下内容:

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>4.2.2.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

<version>4.2.2.RELEASE</version>

</dependency>

这些依赖可以百度:maven repository-->点击进入-->搜索spring-->spring context-->选择一个版本-->进入后下面就有maven依赖配置-->然后粘贴过来就行

--右键项目-->Run As-->选择第一个Maven Build-->Goals里面填写complile-->点击运行-->如何你是第一次配置,那么它会先去maven的中央仓库下载所需要的jar文件到本地仓库中,需要一些时间

--编写一个Student类,内容为:

package com.xll;

public class Student {

private String name;

private int age;

public void setName(String name){

this.name = name;

}

public void setAge(int age){

this.age = age;

}

public void sayInfo(){

System.out.println("name:" + name + " age:" + age);

}

}

--set方法是必须要的

--编写一个运行类,内容如下:

package com.xll;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

public static void main(String[] args){

ApplicationContext acc = new ClassPathXmlApplicationContext("ApplicationContext.xml");

Student stu = (Student)acc.getBean("student");

stu.sayInfo();

}

}

--运行运行类

--注eclipse改一下java版本
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: