将Spring应用转换为Spring Boot。

huangapple go评论83阅读模式
英文:

Convert Spring application to SpringBoot

问题

我正在根据以下教程构建一个玩具应用程序:
https://www.baeldung.com/spring-security-login

我已经有一个可运行的 web 应用程序,并希望将其转换为 Spring Boot。

我想知道如何完成这个过程?

当前的应用程序初始化器如下:

public class AppInitializer implements WebApplicationInitializer {

  @Override
  public void onStartup(final ServletContext sc) throws ServletException {

    AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();

    root.scan("com.baeldung");
    sc.addListener(new ContextLoaderListener(root));

    ServletRegistration.Dynamic appServlet = sc.addServlet("mvc", new DispatcherServlet(new GenericWebApplicationContext()));
    appServlet.setLoadOnStartup(1);
    appServlet.addMapping("/");

    sc.addFilter("securityFilter", new DelegatingFilterProxy("springSecurityFilterChain"))
    .addMappingForUrlPatterns(null, false, "/*");

   }

}

对于(最好是快速且简单的)迁移到 Spring Boot,我欢迎任何提示。

这是一个 Gradle 应用程序(我将其从 Maven 迁移过来)。

英文:

I'm building a toy application based on the following tutorial:
https://www.baeldung.com/spring-security-login

I've got a working web app, and would like to convert it to SpringBoot.

I wonder how that could be done?

The current application initialiser looks like that:

public class AppInitializer implements WebApplicationInitializer {

  @Override
  public void onStartup(final ServletContext sc) throws ServletException {

    AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();

    root.scan("com.baeldung");
    sc.addListener(new ContextLoaderListener(root));

    ServletRegistration.Dynamic appServlet = sc.addServlet("mvc", new DispatcherServlet(new GenericWebApplicationContext()));
    appServlet.setLoadOnStartup(1);
    appServlet.addMapping("/");

    sc.addFilter("securityFilter", new DelegatingFilterProxy("springSecurityFilterChain"))
    .addMappingForUrlPatterns(null, false, "/*");

   }

}

Any tips for (preferably quick and easy) migration to SpringBoot are appreciated.

It's a gradle application (I migrated it from maven).

答案1

得分: 1

Springboot初始化和入口点是使用@SpringBootApplication注解标记的主类。例如,参考下面的DemoApplication类。

@SpringBootApplication
public class DemoApplication {
  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }
}

您还需要查看spring-boot-starter插件。

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.4.RELEASE</version>
    <relativePath/> <!-- 从存储库查找父级 -->
</parent>

您可以使用Springboot初始化器生成项目并开始迁移您的代码。

英文:

Springboot initialization and entry point is the main class annotated with @SpringBootApplication. For example, see below DemoApplication class.

@SpringBootApplication
public class DemoApplication {
  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }
}

You will need to look at the spring-boot-starter plugins as well.

&lt;parent&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
    &lt;version&gt;2.3.4.RELEASE&lt;/version&gt;
    &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
&lt;/parent&gt;

You can generate a project using Springboot initializer and start moving your code.

答案2

得分: 0

好的,以下是翻译好的部分:

Well, my question was caused by a confusion as to how the web context gets started and knows which controllers to use, so I was a bit stuck trying to wire up the WebApplicationInitializer and AnnotationConfigWebApplicationContext but then...

since it's a web-based application that uses controllers, all I had to do was to replace the main class with the following:

@SpringBootApplication
public class AppInitializer {
   
    public static void main(String[] args) {
        SpringApplication.run(AppInitializer.class, args);
    }
}

Then, since I've had controllers, as in:

@Controller
public class SecuredResourceController {
   
       @RequestMapping("/secured")
        public void secureResource(HttpServletRequest request, HttpServletResponse response) {
            System.out.println("accessing secured resource");
        }
    }

, the app would build and start the controller inside an embedded tomcat container, provided I've had the following dependencies:

implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'

Then, I just run

./gradlew bootRun

and the container gets started and processes the requests. Mystery's solved...

英文:

Well, my question was caused by a confusion as to how the web context gets started and knows which controllers to use, so I was a bit stuck trying to wire up the WebApplicationInitializer and AnnotationConfigWebApplicationContext but then...

since it's a web-based application that uses controllers, all I had to do was to replace the main class with the following:

@SpringBootApplication
public class AppInitializer {

    public static void main(String[] args) {
        SpringApplication.run(AppInitializer.class, args);
    }
 }

Then, since I've had controllers, as in:

@Controller
public class SecuredResourceController {

       @RequestMapping(&quot;/secured&quot;)
        public void secureResource(HttpServletRequest request, HttpServletResponse response) {
            System.out.println(&quot;accessing secured resource&quot;);
        }
    }

, the app would build and start the controller inside an embedded tomcat container, provided I've had the following dependencies:

implementation &#39;org.springframework.boot:spring-boot-starter&#39;
implementation &#39;org.springframework.boot:spring-boot-starter-thymeleaf&#39;
implementation &#39;org.springframework.boot:spring-boot-starter-web&#39;

Then, I just run

./gradlew bootRun

and the container gets started and processes the requests. Mystery's solved...

huangapple
  • 本文由 发表于 2020年10月20日 19:41:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64444400.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定