如何为Springframework开发设置一个小型maven项目

时间:2020-12-20 00:00:08

I created a simple maven project with Eclipse(no archetype), and I have wound up with an empty skeleton. Whenever I create a Spring project, I create from a template, and it contains a number of XML configurations and files. I am not a great Spring developer, and I do not know what ll is required to set up Spring. What necessary files and folders do I need to get started writing a Spring MVC web application, and what content should I put in them?

我用Eclipse创建了一个简单的maven项目(没有原型),我已经完成了一个空的骨架。每当我创建一个Spring项目时,我都是从模板创建的,它包含许多XML配置和文件。我不是一个出色的Spring开发人员,我不知道设置Spring需要什么。开始编写Spring MVC Web应用程序需要哪些必要的文件和文件夹,以及我应该在其中添加哪些内容?

Here is my application structure so far:

这是我到目前为止的应用程序结构:

如何为Springframework开发设置一个小型maven项目

If it is any helpful, here is my pom.xml:

如果它有用,这是我的pom.xml:

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.reimaginenewton</groupId>
  <artifactId>site</artifactId>
  <version>1.0.0.0</version>
  <packaging>war</packaging>
  <name>Re-Imagine Newton</name>
  <description>The site at reimaginenewton.org</description>
  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.1.6.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-mongodb</artifactId>
      <version>1.7.0.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.mobile</groupId>
      <artifactId>spring-mobile-device</artifactId>
      <version>1.1.3.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.social</groupId>
      <artifactId>spring-social-core</artifactId>
      <version>1.1.0.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-web</artifactId>
      <version>4.0.1.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.social</groupId>
      <artifactId>spring-social-facebook</artifactId>
      <version>2.0.1.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.social</groupId>
      <artifactId>spring-social-twitter</artifactId>
      <version>1.1.0.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.0</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>
  <repositories>
    <repository>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Central Repository</name>
      <url>http://repo.maven.apache.org/maven2</url>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Central Repository</name>
      <url>http://repo.maven.apache.org/maven2</url>
    </pluginRepository>
  </pluginRepositories>
  <build>
    <sourceDirectory>C:\Users\James\Documents\workspace-sts-3.6.4.RELEASE\site\src\main\java</sourceDirectory>
    <scriptSourceDirectory>C:\Users\James\Documents\workspace-sts-3.6.4.RELEASE\site\src\main\scripts</scriptSourceDirectory>
    <testSourceDirectory>C:\Users\James\Documents\workspace-sts-3.6.4.RELEASE\site\src\test\java</testSourceDirectory>
    <outputDirectory>C:\Users\James\Documents\workspace-sts-3.6.4.RELEASE\site\target\classes</outputDirectory>
    <testOutputDirectory>C:\Users\James\Documents\workspace-sts-3.6.4.RELEASE\site\target\test-classes</testOutputDirectory>
    <resources>
      <resource>
        <directory>C:\Users\James\Documents\workspace-sts-3.6.4.RELEASE\site\src\main\resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>C:\Users\James\Documents\workspace-sts-3.6.4.RELEASE\site\src\test\resources</directory>
      </testResource>
    </testResources>
    <directory>C:\Users\James\Documents\workspace-sts-3.6.4.RELEASE\site\target</directory>
    <finalName>site-1.0.0.0</finalName>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.2-beta-5</version>
        </plugin>
        <plugin>
          <artifactId>maven-dependency-plugin</artifactId>
          <version>2.8</version>
        </plugin>
        <plugin>
          <artifactId>maven-release-plugin</artifactId>
          <version>2.3.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
    <plugins>
      <plugin>
        <artifactId>maven-clean-plugin</artifactId>
        <version>2.5</version>
        <executions>
          <execution>
            <id>default-clean</id>
            <phase>clean</phase>
            <goals>
              <goal>clean</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.6</version>
        <executions>
          <execution>
            <id>default-testResources</id>
            <phase>process-test-resources</phase>
            <goals>
              <goal>testResources</goal>
            </goals>
          </execution>
          <execution>
            <id>default-resources</id>
            <phase>process-resources</phase>
            <goals>
              <goal>resources</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.2</version>
        <executions>
          <execution>
            <id>default-war</id>
            <phase>package</phase>
            <goals>
              <goal>war</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.5.1</version>
        <executions>
          <execution>
            <id>default-compile</id>
            <phase>compile</phase>
            <goals>
              <goal>compile</goal>
            </goals>
          </execution>
          <execution>
            <id>default-testCompile</id>
            <phase>test-compile</phase>
            <goals>
              <goal>testCompile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.12.4</version>
        <executions>
          <execution>
            <id>default-test</id>
            <phase>test</phase>
            <goals>
              <goal>test</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-install-plugin</artifactId>
        <version>2.4</version>
        <executions>
          <execution>
            <id>default-install</id>
            <phase>install</phase>
            <goals>
              <goal>install</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-deploy-plugin</artifactId>
        <version>2.7</version>
        <executions>
          <execution>
            <id>default-deploy</id>
            <phase>deploy</phase>
            <goals>
              <goal>deploy</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-site-plugin</artifactId>
        <version>3.3</version>
        <executions>
          <execution>
            <id>default-site</id>
            <phase>site</phase>
            <goals>
              <goal>site</goal>
            </goals>
            <configuration>
              <outputDirectory>C:\Users\James\Documents\workspace-sts-3.6.4.RELEASE\site\target\site</outputDirectory>
              <reportPlugins>
                <reportPlugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-project-info-reports-plugin</artifactId>
                </reportPlugin>
              </reportPlugins>
            </configuration>
          </execution>
          <execution>
            <id>default-deploy</id>
            <phase>site-deploy</phase>
            <goals>
              <goal>deploy</goal>
            </goals>
            <configuration>
              <outputDirectory>C:\Users\James\Documents\workspace-sts-3.6.4.RELEASE\site\target\site</outputDirectory>
              <reportPlugins>
                <reportPlugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-project-info-reports-plugin</artifactId>
                </reportPlugin>
              </reportPlugins>
            </configuration>
          </execution>
        </executions>
        <configuration>
          <outputDirectory>C:\Users\James\Documents\workspace-sts-3.6.4.RELEASE\site\target\site</outputDirectory>
          <reportPlugins>
            <reportPlugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-project-info-reports-plugin</artifactId>
            </reportPlugin>
          </reportPlugins>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <reporting>
    <outputDirectory>C:\Users\James\Documents\workspace-sts-3.6.4.RELEASE\site\target\site</outputDirectory>
  </reporting>
</project>

1 个解决方案

#1


the only necessary things now are your code and the spring application context.

现在唯一需要的东西是你的代码和spring应用程序上下文。

The application config can be setup using a class annotated with @Configuration as described here. A simple implemenation looks like this:

可以使用@Configuration注释的类来设置应用程序配置,如此处所述。一个简单的实现看起来像这样:

@Configuration
@ComponentScan("org.example.springapp")
public class SpringConfig {

    @Bean
    public Foo foo() {
       return new Foo();
    }

}

and since you have a war file packaging then you will need something to fire up the spring container in the servlet engine.

因为你有一个war文件打包,所以你需要一些东西来启动servlet引擎中的spring容器。

public class WebAppInitializer implements WebApplicationInitializer {

    public void onStartup(ServletContext container) throws ServletException {
        AnnotationConfigWebApplicationContext rootContext = new   AnnotationConfigWebApplicationContext();

        rootContext.register(SpringConfig.class);

        container.addListener(new ContextLoaderListener(rootContext));

        // Register and map the dispatcher servlet
        ServletRegistration.Dynamic dispatcher = container.addServlet(
            "dispatcher", new DispatcherServlet(rootContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    }
}

#1


the only necessary things now are your code and the spring application context.

现在唯一需要的东西是你的代码和spring应用程序上下文。

The application config can be setup using a class annotated with @Configuration as described here. A simple implemenation looks like this:

可以使用@Configuration注释的类来设置应用程序配置,如此处所述。一个简单的实现看起来像这样:

@Configuration
@ComponentScan("org.example.springapp")
public class SpringConfig {

    @Bean
    public Foo foo() {
       return new Foo();
    }

}

and since you have a war file packaging then you will need something to fire up the spring container in the servlet engine.

因为你有一个war文件打包,所以你需要一些东西来启动servlet引擎中的spring容器。

public class WebAppInitializer implements WebApplicationInitializer {

    public void onStartup(ServletContext container) throws ServletException {
        AnnotationConfigWebApplicationContext rootContext = new   AnnotationConfigWebApplicationContext();

        rootContext.register(SpringConfig.class);

        container.addListener(new ContextLoaderListener(rootContext));

        // Register and map the dispatcher servlet
        ServletRegistration.Dynamic dispatcher = container.addServlet(
            "dispatcher", new DispatcherServlet(rootContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    }
}