版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/SunnyYoona/article/details/50611593
了解Spring的朋友都知道,创建一个Spring Framework项目都需要依赖什么样的Jar包。如果不使用Maven,那么在项目中就需要手动下载相关的依赖。由于Spring Framework又会依赖与其他开源类库,因此实际中往往会下载Spring Framework的jar包,还的下载所有它依赖的其他jar包。这么做往往就引入了很多不必要的依赖。另一种做法是只下载Spring Framework的jar包,不包含其他的相关依赖,到实际使用的时候,再根据报错信息,或者查询相关文档,加入需要的其他依赖。
1. 创建Maven项目
如果对Maven不太了解的可以参考:[Maven实战](4)eclipse创建Maven项目
使用IDE创建一个Maven项目非常简单,选择菜单项File->New->Other,在弹出的对话框中选择Maven下的Maven Project,然后点击Next按钮:
在弹出的New Maven Project对话框中,使用默认的选项,然后点击Next按钮:
此时m2eclipse会提示我们选择一个Archetype。这里选择maven-archetype-quickstart,再点击Next按钮:
此时输入groupId,artifactId,version,package:
点击Finish即可完成。
2. 添加依赖配置
pom.xml
2.1 Spring2.xxx
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sjf.springdemo</groupId>
<artifactId>springdemo-helloworld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springdemo-helloworld</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6</version>
</dependency>
</dependencies>
</project>
2.2 Spring3.xxx
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sjf.springdemo</groupId>
<artifactId>springdemo-helloworld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springdemo-helloworld</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
</dependencies>
</project>
3. 编译查看Maven是否创建成功
运行mvn clean compile命令:
Run:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building springdemo-helloworld 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ springdemo-helloworld ---
[INFO] Deleting D:\WorkSpace\EclipseCode\springdemo-helloworld\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ springdemo-helloworld ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\WorkSpace\EclipseCode\springdemo-helloworld\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ springdemo-helloworld ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to D:\WorkSpace\EclipseCode\springdemo-helloworld\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.292 s
[INFO] Finished at: 2016-01-30T15:24:11+08:00
[INFO] Final Memory: 9M/23M
[INFO] ------------------------------------------------------------------------
BUILD SUCCESS则表示创建Maven成功。
4. Spring bean主代码
在src/main/java/com/sjf/bean/下
创建HellloWorld类,
Spring的bean仅仅是一个普通的Java类,稍后在Spring bean配置文件中声明。
package com.sjf.bean;
/**
* hello world 类
* @author sjf0115
*
*/
public class HelloWorld {
private String name;
public void setName(String name) {
this.name = name;
}
public void sayHello(){
System.out.println("welcome "+ name +" to spring world...");
}
}
5. Spring bean配置文件
在src/main/resources创建applicationContext.xml文件,
这是的Spring bean的配置文件,该文件告知所有可用的Spring bean。
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.xsd">
<!-- 配置bean -->
<bean id = "helloworld" class = "com.sjf.bean.HelloWorld">
<property name="name" value="sjf0115"></property>
</bean>
</beans>
6.项目结构
Spring2.xxx:
Spring3.xxx:
7.运行
src/main/java下创建Test.java:
package com.sjf.bean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 测试类
* @author sjf0115
*
*/
public class Test {
private static ApplicationContext context;
private static HelloWorld helloWorld;
public static void main(String[] args) {
context = new ClassPathXmlApplicationContext("applicationContext.xml");
// 2. 从IOC容器中获取Bean实例
helloWorld = (HelloWorld)context.getBean("helloworld");
// 3.调用sayHello方法
helloWorld.sayHello();
}
}
运行结果:
一月 30, 2016 3:53:19 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@164b9b6: display name [org.springframework.context.support.ClassPathXmlApplicationContext@164b9b6]; startup date [Sat Jan 30 15:53:19 CST 2016]; root of context hierarchy
一月 30, 2016 3:53:19 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
一月 30, 2016 3:53:19 下午 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@164b9b6]: org.springframework.beans.factory.support.DefaultListableBeanFactory@18b6281
一月 30, 2016 3:53:19 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@18b6281: defining beans [helloworld]; root of factory hierarchy
welcome sjf0115 to spring world...
|