1、下载和配置maven:略
2、建立项目,参考:点击打开链接
对于建立项目失败,多半是因为连不上中央库,可以搜索“maven 阿里云”,配置中央库镜像
3、将示例代码考入相应的包中后:首先配置pom.xml,添加下面的代码,用以执行mvn exec:exec
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<classpath>
</classpath>
<argument>com.ch2.Main</argument>
</arguments>
</configuration>
</plugin>
插入下面代码,作为spring的依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
4、执行 mvn clean compile命令会导致target目录删除并重新生成,这样配置文件也会被删除
注:配置文件不会自动添加到运行目录,手动加入之后,不要执行mvn clean compile 而只进行 mvn compile
5、拷贝过来的代码不要忘记修改 package
6、Spring依赖注入配置文件用相对路径的表示方法,是从包名开始的,前面还有一个‘/’
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("/com/ch2/ch2-beans.xml");
AccountService accountService = applicationContext.getBean("accountServiceImpl", AccountService.class);