顯示具有 Spring Boot 標籤的文章。 顯示所有文章
顯示具有 Spring Boot 標籤的文章。 顯示所有文章

2022年3月2日 星期三

Error creating bean with name 'dataConfig': Invocation of init method failed; nested exception is java.awt.HeadlessException

System.setProperty("java.awt.headless", "false");
//DatabaseManagerSwing前面加紅色這一行
DatabaseManagerSwing.main(new String[] { "--url", "jdbc:hsqldb:mem:testdb", "--noexit" });


完整錯誤訊息:
Caused by: java.awt.HeadlessException: null
        at java.desktop/java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:166) ~[na:na]
        at java.desktop/java.awt.Window.<init>(Window.java:553) ~[na:na]
        at java.desktop/java.awt.Frame.<init>(Frame.java:428) ~[na:na]
        at java.desktop/java.awt.Frame.<init>(Frame.java:393) ~[na:na]
        at java.desktop/javax.swing.JFrame.<init>(JFrame.java:180) ~[na:na]
        at org.hsqldb.util.DatabaseManagerSwing.<init>(Unknown Source) ~[hsqldb-2.6.1.jar:2.6.1]
        at org.hsqldb.util.DatabaseManagerSwing.main(Unknown Source) ~[hsqldb-2.6.1.jar:2.6.1]

The bean 'localeResolver', defined in class path resource

在組態檔application.properties加入以下設定
spring.main.allow-bean-definition-overriding=true

參考
https://stackoverflow.com/questions/63658346/a-bean-with-that-name-has-already-been-defined-in-class-path-resource-path-and


完整錯誤訊息:
The bean 'localeResolver', defined in class path resource [com/aabbcc/config/WebConfig.class], could not be registered. A bean with that name has already been defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class] and overriding is disabled.

2021年3月10日 星期三

內網環境編譯Maven專案,設定Proxy的步驟

一般公司電腦連外上網都會透過Proxy Server,編譯Maven專案時需要特別設定Proxy並將Maven網站的SSL憑證匯入到keystore,步驟如下。

(一)
設定Proxy:
settings.xml放在使用者目錄的.m2資料夾

(二)
因為Maven站台使用https連線,需要透過瀏覽器匯出並將此SSL憑證匯入到keystore
參考
https://stackoverflow.com/questions/25911623/problems-using-maven-and-ssl-behind-proxy

Use a browser (I used IE) to go to https://repo.maven.apache.org
Click on lock icon and choose "View Certificate"
Go to the "Details" tab and choose "Save to File"
Choose type "Base 64 X.509 (.CER)" and save it somewhere

Now open a command prompt and type (use your own paths):
keytool -import -file C:\temp\mavenCert.cer -keystore C:\temp\mavenKeystore

(三)
加入以下紅色參數(以執行Spring Boot專案為例)
mvn spring-boot:run -Djavax.net.ssl.trustStore=C:\temp\mavenKeystore


備註:
如果沒有設定 -Djavax.net.ssl.trustStore=會有以下錯誤訊息
Maven sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

2021年3月1日 星期一

JSR 315: Java Servlet 3.0 Specification

https://jcp.org/en/jsr/detail?id=315

https://openhome.cc/Gossip/ServletJSP/Configuration.html

https://zhuanlan.zhihu.com/p/81885441

使用Spring Boot免去web.xml

使用Spring Boot进行Web开发的时候,按照官方的推荐都是使用内嵌的Servlet容器,和应用一起打包成jar包部署,当然,我们可以使用传统war包来部署,Main Class只需继承org.springframework.boot.web.servlet.support.SpringBootServletInitializer即可(启动时会加载所有ServletContainerlnitializer)。

免去web.xml是通过Servlet 3.0中的javax.servlet.ServletContainerInitializer来实现的,ServletContainerInitializer是提供了一个实现和web.xml类似功能的接口,在应用启动的时候能够通过编程的方式来注册Servlet、Fileter、Listener的功能。

SpringBoot通过Servlet3.0的这个设计,结合SPI机制,在spring-web包下发现META-INF/services/javax.servlet.ServletContainerInitializer实现类:org.springframework.web.SpringServletContainerInitializer从而进行初始化,包括对DispatcherServlet的注册,ContextLoaderListener的注册等等,最终免去web.xml


2021年2月28日 星期日

Reload Templates without Restarting the Container

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-hotswapping

Thymeleaf Templates

If you use Thymeleaf, set spring.thymeleaf.cache to false. See ThymeleafAutoConfiguration for other Thymeleaf customization options.

FreeMarker Templates

If you use FreeMarker, set spring.freemarker.cache to false. See FreeMarkerAutoConfiguration for other FreeMarker customization options.

Groovy Templates

If you use Groovy templates, set spring.groovy.template.cache to false. See GroovyTemplateAutoConfiguration for other Groovy customization options.

DEMO:
https://github.com/imrexhuang/Spring-Boot-Security-Thymeleaf-Demo

2020年8月20日 星期四

SpringBootCodeGenerator

 https://github.com/moshowgame/SpringBootCodeGenerator

 

√基于SpringBoot2+Freemarker的代码生成器,

√支持mysql/oracle/pgsql三大数据库, 

√用DDL-SQL语句生成JPA/JdbcTemplate/Mybatis/MybatisPlus/BeetlSQL相关代码

 

ref:

https://zhengkai.blog.csdn.net

  

2020年6月23日 星期二

mall项目是一套电商系统,基于SpringBoot+MyBatis实现

後台
https://github.com/macrozheng/mall
http://localhost:8080

前台(Vue+Element)
https://github.com/macrozheng/mall-admin-web
前台佈署
http://www.macrozheng.com/#/deploy/mall_deploy_web
npm install node@8.9.4
修改\config\dev.env.js,把BASE_API改成自己開發環境網址(預設是8080)
npm run dev
登入頁面
http://localhost:8090
預設帳密:admin/macro123

微服務
https://github.com/macrozheng/mall-swarm

教學
https://github.com/macrozheng/mall-learning

※Docker佈署
 http://www.macrozheng.com/#/deploy/mall_deploy_docker

請先修改Docker組態檔開放TCP 2375 (有資安風險,請僅用於開發環境!)
vi /usr/lib/systemd/system/docker.service
加入以下組態
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
參考
https://www.cnblogs.com/hongdada/p/11512901.html

重啟Docker服務
systemctl daemon-reload
systemctl restart docker

\mall\pom.xml內的<docker.host>修改成自己的Docker主機IP位址
\mall\mall-admin\pom.xml內的<plugin>區塊請取消註解,並package推送images
\mall\mall-search\pom.xml內的<plugin>區塊請取消註解,並package推送images
\mall\mall-portal\pom.xml內的<plugin>區塊請取消註解,並package推送images


package推送images:



package推送images後使用docker images指令確認推送成功到伺服器:


# 啟動所有的 containers
因為啟動有順序性,第一次啟動失敗再執行一次指令
$ docker start $(docker ps -a -q)
參考
https://ithelp.ithome.com.tw/articles/10186431


※編譯問題
因為mall-admin專案相依於mall-mbg和mall-security,又間接相依於mall-common,請依下圖方式先mvn install把相依專案編譯成jar安裝到local repository

編譯錯誤訊息
[ERROR] Failed to execute goal on project mall-mbg: Could not resolve dependencies for project com.macro.mall:mall-mbg:jar:1.0-SNAPSHOT: Could not find artifact com.macro.mall:mall-common:jar:1.0-SNAPSHOT -> [Help 1]


※Lombok
以下package的程式有使用到Lombok
package com.macro.mall.dto;
package com.macro.mall.portal.domain;
package com.macro.mall.security.config;

2019年8月18日 星期日

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

 解決方式:
加入以下紅字部分停用Spring Boot的資料庫自動配置
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class XXXApplication {
 public static void main(String[] args) {
  SpringApplication.run(XXXApplication.class, args);
 }
}

錯誤訊息:
:: Spring Boot ::        (v2.1.7.RELEASE)
2019-08-18 19:59:10.049  INFO 18260 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-08-18 19:59:10.049  INFO 18260 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1388 ms
2019-08-18 19:59:10.109  WARN 18260 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
2019-08-18 19:59:10.111  INFO 18260 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2019-08-18 19:59:10.122  INFO 18260 --- [           main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-08-18 19:59:10.128 ERROR 18260 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   :
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class

Action:
Consider the following:
 If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
 If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

2019年8月16日 星期五

Spring Boot 1.5.x to 2.0.x Migration – first step of migration to Java 11


如果要把專案改成Java 11,可以參考以下文章
Spring Boot 1.5.x to 2.0.x Migration – first step of migration to Java 11
https://altkomsoftware.pl/en/blog/spring-boot-migration-java/

Spring Boot 1.5不支援Java 11造成ClassCastException: class jdk.internal.loader.ClassLoaders$AppClassLoader cannot be cast to class java.net.URLClassLoader

原因:
Spring Boot 1.5不支援Java 11
改用JDK 8後就正常

如果要把專案改成Java 11,可以參考以下文章
Spring Boot 1.5.x to 2.0.x Migration – first step of migration to Java 11
https://altkomsoftware.pl/en/blog/spring-boot-migration-java/

Exception in thread "main" java.lang.ClassCastException: class jdk.internal.loader.ClassLoaders$AppClassLoader cannot be cast to class java.net.URLClassLoader (jdk.internal.loader.ClassLoaders$AppClassLoader and java.net.URLClassLoader are in module java.base of loader 'bootstrap')
 at org.springframework.boot.devtools.restart.DefaultRestartInitializer.getUrls(DefaultRestartInitializer.java:93)
 at org.springframework.boot.devtools.restart.DefaultRestartInitializer.getInitialUrls(DefaultRestartInitializer.java:56)
 at org.springframework.boot.devtools.restart.Restarter.<init>(Restarter.java:140)
 at org.springframework.boot.devtools.restart.Restarter.initialize(Restarter.java:546)
 at org.springframework.boot.devtools.restart.RestartApplicationListener.onApplicationStartingEvent(RestartApplicationListener.java:67)
 at org.springframework.boot.devtools.restart.RestartApplicationListener.onApplicationEvent(RestartApplicationListener.java:45)
 at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
 at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
 at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
 at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:122)
 at org.springframework.boot.context.event.EventPublishingRunListener.starting(EventPublishingRunListener.java:69)
 at org.springframework.boot.SpringApplicationRunListeners.starting(SpringApplicationRunListeners.java:48)
 at org.springframework.boot.SpringApplication.run(SpringApplication.java:292)
 at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
 at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
 at com.example.demo.DemoJavaWebApplication.main(DemoJavaWebApplication.java:12)