英文:
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.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
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
andAnnotationConfigWebApplicationContext
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("/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...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论