首页 > 代码库 > Spring入门,使用Maven进行管理

Spring入门,使用Maven进行管理

一,使用maven创建项目原型

mvn archetype:generate

进入交互模式创建项目原型,根据网速不同,跳出设置选项的时间不定

第一个选项

直接Enter即可,表示使用默认值502,后面的如法炮制,直到设置我们需要的groupId,artifactId,version为止。

我的设置为:

groupId:com.yxl.springdemo

artifactId:springdemo

version:1.0-SNAPSHOT(也就是默认值)

使用tree展示原型目录结构:

二,编辑源代码

  首先创建一个bean类,在com/yxl/springdemo目录下新建HelloBean.java。

 1 package com.yxl.springdemo; 2  3 public class HelloBean 4 { 5     private String helloword; 6  7     public void setHelloword(String helloword) 8     { 9         this.helloword = helloword;10     }11     public String getHelloword()12     {13         return helloword;14     }15 }
View Code

   在com/yxl/springdemo目录下新建SpringDemo.java。

 1 package com.yxl.springdemo; 2  3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 import org.springframework.context.support.FileSystemXmlApplicationContext; 6  7 public class SpringDemo 8 { 9     public static void main(String[] args)10     {11         //ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-config.xml");12         ApplicationContext ctx = new FileSystemXmlApplicationContext("beans-config.xml");13         HelloBean hello = (HelloBean)ctx.getBean("helloBean");14         System.out.println(hello.getHelloword());15     }16 }
View Code

   重新编辑pom.xml,解决依赖关系,并生成可执行jar包。

 1 <?xml version = "1.0" encoding = "UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0"  3      xmlns:xsi="http://www.w3.org/4.1.2.RELEASE1/XMLSchema-instance" 4     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5   <modelVersion>4.0.0</modelVersion> 6  7   <groupId>com.yxl.springdemo</groupId> 8   <artifactId>springdemo</artifactId> 9   <version>1.0-SNAPSHOT</version>10   <packaging>jar</packaging>11 12   <name>springdemo</name>13   <url>http://maven.apache.org</url>14 15   <properties>16     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>17   </properties>18 19   <dependencies>20     <dependency>21         <groupId>org.springframework</groupId>22         <artifactId>spring-core</artifactId>23         <version>4.1.2.RELEASE</version>24     </dependency>25     <dependency>26         <groupId>org.springframework</groupId>27         <artifactId>spring-context</artifactId>28         <version>4.1.2.RELEASE</version>29     </dependency>30     <dependency>31         <groupId>org.springframework</groupId>32         <artifactId>spring-context-support</artifactId>33         <version>4.1.2.RELEASE</version>34     </dependency>35     <dependency>36         <groupId>org.springframework</groupId>37         <artifactId>spring-beans</artifactId>38         <version>4.1.2.RELEASE</version>39     </dependency>40     <dependency>41         <groupId>org.springframework</groupId>42         <artifactId>spring-aop</artifactId>43         <version>4.1.2.RELEASE</version>44     </dependency>45     <dependency>46       <groupId>junit</groupId>47       <artifactId>junit</artifactId>48       <version>4.7</version>49       <scope>test</scope>50     </dependency>51   </dependencies>52 53 <build>54     <plugins>55       <plugin>56         <groupId>org.apache.maven.plugins</groupId>57         <artifactId>maven-shade-plugin</artifactId>58         <version>2.3</version>59         <executions>60           <execution>61             <phase>package</phase>62             <goals>63               <goal>shade</goal>64             </goals>65             <configuration>66               <transformers>67                 <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">68                   <manifestEntries>69                     <Main-Class>com.yxl.springdemo.SpringDemo</Main-Class>70                     <Build-Number>123</Build-Number>71                   </manifestEntries>72                 </transformer>73               </transformers>74             </configuration>75           </execution>76         </executions>77       </plugin>78     </plugins>79   </build>80 81 </project>
View Code

  在项目主目录springdemo(不是包目录)下新建beans-config.xml,配置bean属性。

 1 <?xml version = "1.0" encoding = "UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4     xsi:schemaLocation="http://www.springframework.org/schema/beans 5         http://www.springframework.org/schema/beans/spring-beans.xsd"> 6  7     <bean id = "helloBean" 8         class = "com.yxl.springdemo.HelloBean"> 9         <property name = "helloword">10             <value>Hello, World</value>11         </property>12     </bean>13 </beans>
View Code

 在这里我犯过的一个错误就是xml文件第一行的?与xml之间插入了一个空格,然后运行程序就会抛出异常。

三,编译并运行结果

mvn clean compilemvn clean packagejava -jar target/sprintdemo-1.0-SNAPSHOT.jar

结果:

注意,执行java -jar target/springdemo-1.0-SNAPSHOT.jar时,配置文件必须在当前目录下。比如我是在项目根目录下执行这条语句,所以beans-config.xml也被放置在项目根目录springdemo中,如果在target中执行该包文件,执行时会抛出异常。

Spring入门,使用Maven进行管理