错误日志:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-12-20 23:00:50.664 ERROR 11184 --- [ 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).
Disconnected from the target VM, address: '127.0.0.1:53235', transport: 'socket'
Process finished with exit code 1
日志中有这么一句:no profiles are currently active
说明没有配置profiles 也就是项目运行的代码环境,赶紧看一下yml配置:
spring:
profiles:
active: '@spring.active@'
我是这样写的,说明可能是’@spring.active@'没生效 那么我换成local试试
spring:
profiles:
#active: '@spring.active@'
active: local
对我们的项目进行maven clean 然后再次运行:
Connected to the target VM, address: '127.0.0.1:53321', transport: 'socket'
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.2.RELEASE)
2019-12-20 23:07:27.055 INFO 10324 --- [ main] c.s.d.SelfDisciplineApplication : Starting SelfDisciplineApplication on Suvue with PID 10324 (Z:\personal\code_repo\self-discipline\target\classes started by yj020 in Z:\personal\code_repo\self-discipline)
2019-12-20 23:07:27.060 INFO 10324 --- [ main] c.s.d.SelfDisciplineApplication : The following profiles are active: local
2019-12-20 23:07:27.751 INFO 10324 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2019-12-20 23:07:27.754 INFO 10324 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Redis repositories in DEFAULT mode.
2019-12-20 23:07:27.794 INFO 10324 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 24ms. Found 0 Redis repository interfaces.
2019-12-20 23:07:28.094 INFO 10324 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-12-20 23:07:28.340 INFO 10324 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 80 (http)
2019-12-20 23:07:28.349 INFO 10324 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-12-20 23:07:28.349 INFO 10324 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.29]
2019-12-20 23:07:28.444 INFO 10324 --- [ main] o.a.c.c.C.[.[localhost].[/discipline] : Initializing Spring embedded WebApplicationContext
2019-12-20 23:07:28.445 INFO 10324 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1328 ms
_ _ |_ _ _|_. ___ _ | _
| | |\/|_)(_| | |_\ |_)||_|_\
/ |
3.3.0
2019-12-20 23:07:29.982 WARN 10324 --- [ main] c.b.m.core.metadata.TableInfoHelper : Warn: Could not find @TableId in Class: cn.suvue.discipline.modular.entity.SetArtitleSort.
2019-12-20 23:07:30.010 WARN 10324 --- [ main] c.b.m.core.metadata.TableInfoHelper : Warn: Could not find @TableId in Class: cn.suvue.discipline.modular.entity.Sorts.
2019-12-20 23:07:30.248 INFO 10324 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-12-20 23:07:30.586 INFO 10324 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 80 (http) with context path '/discipline'
2019-12-20 23:07:30.590 INFO 10324 --- [ main] c.s.d.SelfDisciplineApplication : Started SelfDisciplineApplication in 4.139 seconds (JVM running for 5.441)
此时项目已经能正常运行了!大功告成
可是还是不甘心,没找到问题的根源。
在网上一番查找,结果发现自己的配置出了问题,下面附上正确配置方式:
pom.xml
:
<profiles>
<!--开发环境-->
<profile>
<id>dev</id>
<properties>
<spring.active>dev</spring.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<!--测试环境-->
<profile>
<id>test</id>
<properties>
<spring.active>test</spring.active>
</properties>
</profile>
<!--生产环境-->
<profile>
<id>prod</id>
<properties>
<spring.active>prod</spring.active>
</properties>
</profile>
</profiles>
<properties>
<resource.delimiter>@</resource.delimiter>
</properties>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources.${spring.active}</directory>
<filtering>false</filtering>
</resource>
</resources>
</build>
这时候在yml中我们就可以使用高级操作方式了!如下:
spring:
profiles:
active: @spring.active@