Jhihster:创建Spring Boot + Angular/React应用程序
在本教程中,我们将看看Jhipster。
Jhihster是一个强大的开发工具,可以生成,创建和部署Spring引导和Angular JS或者更快地反应JS应用程序。
它将在任何时间为我们创建一个应用程序,它提供了许多开箱函数,如日志记录,指标,健康等。
我将展示我对Spring MVC Angular JS示例进行了证明的类似应用程序,并且我们将实现自动启动Spring 启动+角度/反应的更容易程度。
第1步:快速安装
- 从Oracle安装Java 8.
- 从Node.js安装Node.js(请使用LTS 64位版本,不支持非LTS版本)
- 从 Yarn 安装 Yarn
- 开放终端
- 如果我们想使用Jhipster Marketplace,请安装Yeoman:
yarn global add yo
- 使用命令安装Jhipster:
yarn global add generator-jhipster
完成上述安装后,让我们创建示例项目。
第2步:项目设置
- 打开终端并执行以下命令
- Mkdir JhipsterhelloWorld.
- CD Jhipsterhelworld.
- Jhipster.
这将要求我们选择许多选项可供选择。
? Jan JHipster anonymously report usage statistics to improve the tool over time? No ? Which *type* of application would you like to create? Monolithic application (recommended for simple projects) ? What is the base name of your application? JhipsterHelloWorld ? What is your default Java package name? org.igi.theitroad ? Do you want to use the JHipster Registry to configure, monitor and scale your application? No ? Which *type* of authentication would you like to use? HTTP Session Authentication (stateful, default Spring Security mechanism) ? Which *type* of database would you like to use? SQL (H2, MySQL, MariaDB, PostgreSQL, Oracle, MSSQL) ? Which *production* database would you like to use? MySQL ? Which *development* database would you like to use? H2 with in-memory persistence ? Do you want to use the Spring cache abstraction? Yes, with the Ehcache implementation (local cache, for a single node) ? Do you want to use Hibernate 2nd level cache? No ? Would you like to use Maven or Gradle for building the backend? Maven ? Which other technologies would you like to use? ? Which *Framework* would you like to use for the client? Angular 6 ? Would you like to enable *SASS* support using the LibSass stylesheet preprocessor? No ? Would you like to enable internationalization support? No ? Besides JUnit and Jest, which testing frameworks would you like to use? ? Would you like to install other generators from the JHipster Marketplace? No
完成上述步骤后,它将构建项目。
第3步:IDE中导入项目
我们需要将Eclipse作为Maven项目导入项目。
项目看起来如下。
后端
- Java代码始终包含在SRC/Main/Java文件夹中
- SRC/Main/Resources文件夹包含Java代码使用的静态资源。它还包含国际化文件和其他配置文件。
- 我们可以在SRC/TEST/JAVA文件夹中找到单元和集成测试
前端
- src/main/webapp是前手的根文件夹。
- app文件夹包含AngularJS模块
- I18N包含前端部分的国际化文件
- 前端的单元测试位于SRC/TEST/JAVASCRIPT/SPEC文件夹中
正如我们所看到的,Jhipster为我们创建了大量的软件包和文件。
第4步:运行NPM
在我们继续之前,我们需要启动Node.js服务器。
在IDE中运行以下命令。
CD JhipsterhelloWorld NPM开始
第5步:创建名为国家/地区的实体
实体是建筑物块的时髦应用程序。
实体实际上代表了商业对象,如用户,客户等。
创建实体实际上非常简单。
我们只需执行下面的命令并按照说明操作。
jhipster entity Country
执行上面的命令后,我们将得到以下选项
Executing jhipster:entity Country Options:The entity Country is being created.Generating field #1? Do you want to add a field to your entity? Yes ? What is the name of your field? name ? What is the type of your field? String ? Do you want to add validation rules to your field? No================= Country ================= Fields name (String)Generating field #2? Do you want to add a field to your entity? Yes ? What is the name of your field? population ? What is the type of your field? Long ? Do you want to add validation rules to your field? No================= Country ================= Fields name (String) population (Long) Generating field #3 ? Do you want to add a field to your entity? No ================= Country ================= Fields name (String) population (Long) Generating relationships to other entities <!-- /8691100/theitroad_S2S_Leaderboard_ROS_InContent -->
?
我们想与其他实体添加关系吗?
不
=================国家=================字段名称(字符串)人口(长)
?
我们是否想为业务逻辑使用单独的服务程序?
不,其余控制器应直接使用存储库?
你想要在你的实体上分页吗?
不
一切都配置了,生成实体......
第6步:构建Maven项目
以"清洁安装"为目标构建Maven项目。
第7步:运行项目
转到SRC/Main/Java/ORG/igi/ONITORAD和OPEN JHIPSERHOWLOWOLLLDAPL.JAVA
package org.igi.theitroad; import org.igi.theitroad.config.ApplicationProperties; import org.igi.theitroad.config.DefaultProfileUtil; import io.github.jhipster.config.JHipsterConstants; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.liquibase.LiquibaseProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.core.env.Environment; import javax.annotation.PostConstruct; import java.net.InetAddress; import java.util.Arrays; import java.util.Collection; @SpringBootApplication @EnableConfigurationProperties({LiquibaseProperties.class, ApplicationProperties.class}) public class JhipsterHelloWorldApp { private static final Logger log = LoggerFactory.getLogger(JhipsterHelloWorldApp.class); private final Environment env; public JhipsterHelloWorldApp(Environment env) { this.env = env; } /** * Initializes JhipsterHelloWorld. * <p> * Spring profiles can be configured with a program arguments --spring.profiles.active=your-active-profile * <p> * You can find more information on how profiles work with JHipster on <a href="https://www.jhipster.tech/profiles/">https://www.jhipster.tech/profiles/</a>. */ @PostConstruct public void initApplication() { Collection<String> activeProfiles = Arrays.asList(env.getActiveProfiles()); if (activeProfiles.contains(JHipsterConstants.SPRING_PROFILE_DEVELOPMENT) && activeProfiles.contains(JHipsterConstants.SPRING_PROFILE_PRODUCTION)) { log.error("You have misconfigured your application! It should not run " + "with both the 'dev' and 'prod' profiles at the same time."); } if (activeProfiles.contains(JHipsterConstants.SPRING_PROFILE_DEVELOPMENT) && activeProfiles.contains(JHipsterConstants.SPRING_PROFILE_CLOUD)) { log.error("You have misconfigured your application! It should not " + "run with both the 'dev' and 'cloud' profiles at the same time."); } } /** * Main method, used to run the application. * * @param args the command line arguments */ public static void main(String[] args) { SpringApplication app = new SpringApplication(JhipsterHelloWorldApp.class); DefaultProfileUtil.addDefaultProfile(app); Environment env = app.run(args).getEnvironment(); String protocol = "http"; if (env.getProperty("server.ssl.key-store") != null) { protocol = "https"; } String hostAddress = "localhost"; try { hostAddress = InetAddress.getLocalHost().getHostAddress(); } catch (Exception e) { log.warn("The host name could not be determined, using `localhost` as fallback"); } log.info("\n----------------------------------------------------------\n\t" + "Application '{}' is running! Access URLs:\n\t" + "Local: \t\t{}://localhost:{}\n\t" + "External: \t{}://{}:{}\n\t" + "Profile(s): \t{}\n----------------------------------------------------------", env.getProperty("spring.application.name"), protocol, env.getProperty("server.port"), protocol, hostAddress, env.getProperty("server.port"), env.getActiveProfiles()); } }
如果我们注意到,此类是用@springBootApplication注释的,因此当我们运行此类时,它将被执行为Spring Boot应用程序。
运行java类以上时,我们将得到以下输出:
The Class-Path manifest attribute in /Users/apple/.m2/repository/org/liquibase/liquibase-core/3.5.5/liquibase-core-3.5.5.jar referenced one or more files that do not exist: file:/Users/apple/.m2/repository/org/liquibase/liquibase-core/3.5.5/lib/snakeyaml-1.13.jar 22:02:31.640 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Included patterns for restart : [] 22:02:31.645 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Excluded patterns for restart : [/spring-boot-actuator/target/classes/, /spring-boot-devtools/target/classes/, /spring-boot/target/classes/, /spring-boot-starter-[\w-]+/, /spring-boot-autoconfigure/target/classes/, /spring-boot-starter/target/classes/] 22:02:31.646 [main] DEBUG org.springframework.boot.devtools.restart.ChangeableUrls - Matching URLs for reloading : [file:/Users/apple/JhipsterHelloWorld/target/classes/]██╗ ██╗ ██╗ ████████╗ ███████╗ ██████╗ ████████╗ ████████╗ ███████╗ ██║ ██║ ██║ ╚══██╔══╝ ██╔═══██╗ ██╔════╝ ╚══██╔══╝ ██╔═════╝ ██╔═══██╗ ██║ ████████║ ██║ ███████╔╝ ╚█████╗ ██║ ██████╗ ███████╔╝ ██╗ ██║ ██╔═══██║ ██║ ██╔════╝ ╚═══██╗ ██║ ██╔═══╝ ██╔══██║ ╚██████╔╝ ██║ ██║ ████████╗ ██║ ██████╔╝ ██║ ████████╗ ██║ ╚██╗ ╚═════╝ ╚═╝ ╚═╝ ╚═══════╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══════╝ ╚═╝ ╚═╝:: JHipster