上QQ阅读APP看书,第一时间看更新
How to do it...
After creating the project, certain Maven errors will be encountered just like in Chapter 1, Getting Started with Spring. Bug-fix the Maven issues in our ch02-xml project in order to use the XML-based Spring 5.0 container by performing the following steps:
- Open pom.xml for the project and add the following properties, which contain the Spring build version and servlet container to utilize.
<properties> <spring.version>5.0.0.BUILD-SNAPSHOT</spring.version> <servlet.api.version>3.1.0</servlet.api.version> </properties>
- Add also the following Spring 5 dependencies in pom.xml, which are essential in providing us with the interfaces and classes to build our Spring container:
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> </dependencies>
- It is required to add the following repositories in pom.xml where all the Spring 5.0 dependencies in Step 2 will be downloaded through Maven:
<repositories> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/libs-snapshot</url> <snapshots><enabled>true</enabled></snapshots> </repository> </repositories>
- Then add the Maven plugin for deployment in pom.xml but be sure to recognize web.xml as the deployment descriptor. This can be done by enabling <failOnMissingWebXml> or just deleting the <configuration> tag as follows:
<plugin> <artifactId>maven-war-plugin</artifactId> <version>2.3</version> </plugin> <plugin>
- Add the rest of the Tomcat Maven plugin for deployment in pom.xml, as explained in Chapter 1, Getting Started with Spring.
- After the Maven configuration details, check if there is a WEB-INF folder inside src\main\webapp. If there isn't, create one. This is mandatory for this project since we will be using a deployment descriptor (or web.xml).
- Inside the WEB-INF folder, create a deployment descriptor or drop a web.xml template inside the src\main\webapp\WEB-INF directory.
- Then create an XML-based Spring container named ch02-beans.xml inside the ch02-xml\src\main\java\ directory. The configuration file must contain the following namespaces and tags:
<?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"> </beans>
You can generate this file using the STS Eclipse Wizard ( Ctrl- N) and under the module SpringàSpring Bean Configuration File option.
- Save all the files. Then clean and build the Maven project. Do not deploy yet because this is just a standalone project at the moment.