Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
e53e8bf7
Commit
e53e8bf7
authored
Oct 15, 2018
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '2.0.x'
parents
eaf6a116
0fedf8d2
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
156 additions
and
29 deletions
+156
-29
LiquibaseEndpointAutoConfiguration.java
...nfigure/liquibase/LiquibaseEndpointAutoConfiguration.java
+28
-0
LiquibaseEndpointAutoConfigurationTests.java
...re/liquibase/LiquibaseEndpointAutoConfigurationTests.java
+60
-4
DataSourceClosingSpringLiquibase.java
...configure/liquibase/DataSourceClosingSpringLiquibase.java
+68
-0
LiquibaseAutoConfiguration.java
...t/autoconfigure/liquibase/LiquibaseAutoConfiguration.java
+0
-25
No files found.
spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/liquibase/LiquibaseEndpointAutoConfiguration.java
View file @
e53e8bf7
...
...
@@ -18,6 +18,8 @@ package org.springframework.boot.actuate.autoconfigure.liquibase;
import
liquibase.integration.spring.SpringLiquibase
;
import
org.springframework.beans.BeansException
;
import
org.springframework.beans.factory.config.BeanPostProcessor
;
import
org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint
;
import
org.springframework.boot.actuate.liquibase.LiquibaseEndpoint
;
import
org.springframework.boot.autoconfigure.AutoConfigureAfter
;
...
...
@@ -25,6 +27,7 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnBean
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnClass
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
;
import
org.springframework.boot.autoconfigure.liquibase.DataSourceClosingSpringLiquibase
;
import
org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.annotation.Bean
;
...
...
@@ -49,4 +52,29 @@ public class LiquibaseEndpointAutoConfiguration {
return
new
LiquibaseEndpoint
(
context
);
}
@Bean
@ConditionalOnBean
(
SpringLiquibase
.
class
)
@ConditionalOnEnabledEndpoint
(
endpoint
=
LiquibaseEndpoint
.
class
)
public
static
BeanPostProcessor
preventDataSourceCloseBeanPostProcessor
()
{
return
new
BeanPostProcessor
()
{
@Override
public
Object
postProcessBeforeInitialization
(
Object
bean
,
String
beanName
)
throws
BeansException
{
if
(
bean
instanceof
DataSourceClosingSpringLiquibase
)
{
((
DataSourceClosingSpringLiquibase
)
bean
)
.
setCloseDataSourceOnceMigrated
(
false
);
}
return
bean
;
}
@Override
public
Object
postProcessAfterInitialization
(
Object
bean
,
String
beanName
)
throws
BeansException
{
return
bean
;
}
};
}
}
spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/liquibase/LiquibaseEndpointAutoConfigurationTests.java
View file @
e53e8bf7
...
...
@@ -16,11 +16,13 @@
package
org
.
springframework
.
boot
.
actuate
.
autoconfigure
.
liquibase
;
import
liquibase.exception.LiquibaseException
;
import
liquibase.integration.spring.SpringLiquibase
;
import
org.junit.Test
;
import
org.springframework.boot.actuate.liquibase.LiquibaseEndpoint
;
import
org.springframework.boot.autoconfigure.AutoConfigurations
;
import
org.springframework.boot.autoconfigure.liquibase.DataSourceClosingSpringLiquibase
;
import
org.springframework.boot.test.context.runner.ApplicationContextRunner
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
...
...
@@ -37,23 +39,48 @@ public class LiquibaseEndpointAutoConfigurationTests {
private
final
ApplicationContextRunner
contextRunner
=
new
ApplicationContextRunner
()
.
withConfiguration
(
AutoConfigurations
.
of
(
LiquibaseEndpointAutoConfiguration
.
class
))
.
withUserConfiguration
(
LiquibaseConfiguration
.
class
);
AutoConfigurations
.
of
(
LiquibaseEndpointAutoConfiguration
.
class
));
@Test
public
void
runShouldHaveEndpointBean
()
{
this
.
contextRunner
.
run
(
this
.
contextRunner
.
withUserConfiguration
(
LiquibaseConfiguration
.
class
).
run
(
(
context
)
->
assertThat
(
context
).
hasSingleBean
(
LiquibaseEndpoint
.
class
));
}
@Test
public
void
runWhenEnabledPropertyIsFalseShouldNotHaveEndpointBean
()
{
this
.
contextRunner
this
.
contextRunner
.
withUserConfiguration
(
LiquibaseConfiguration
.
class
)
.
withPropertyValues
(
"management.endpoint.liquibase.enabled:false"
)
.
run
((
context
)
->
assertThat
(
context
)
.
doesNotHaveBean
(
LiquibaseEndpoint
.
class
));
}
@Test
public
void
disablesCloseOfDataSourceWhenEndpointIsEnabled
()
{
this
.
contextRunner
.
withUserConfiguration
(
DataSourceClosingLiquibaseConfiguration
.
class
)
.
run
((
context
)
->
{
assertThat
(
context
).
hasSingleBean
(
LiquibaseEndpoint
.
class
);
assertThat
(
context
.
getBean
(
DataSourceClosingSpringLiquibase
.
class
))
.
hasFieldOrPropertyWithValue
(
"closeDataSourceOnceMigrated"
,
false
);
});
}
@Test
public
void
doesNotDisableCloseOfDataSourceWhenEndpointIsDisabled
()
{
this
.
contextRunner
.
withUserConfiguration
(
DataSourceClosingLiquibaseConfiguration
.
class
)
.
withPropertyValues
(
"management.endpoint.liquibase.enabled:false"
)
.
run
((
context
)
->
{
assertThat
(
context
).
doesNotHaveBean
(
LiquibaseEndpoint
.
class
);
DataSourceClosingSpringLiquibase
bean
=
context
.
getBean
(
DataSourceClosingSpringLiquibase
.
class
);
assertThat
(
bean
).
hasFieldOrPropertyWithValue
(
"closeDataSourceOnceMigrated"
,
true
);
});
}
@Configuration
static
class
LiquibaseConfiguration
{
...
...
@@ -64,4 +91,33 @@ public class LiquibaseEndpointAutoConfigurationTests {
}
@Configuration
static
class
DataSourceClosingLiquibaseConfiguration
{
@Bean
public
SpringLiquibase
liquibase
()
{
return
new
DataSourceClosingSpringLiquibase
()
{
private
boolean
propertiesSet
=
false
;
@Override
public
void
setCloseDataSourceOnceMigrated
(
boolean
closeDataSourceOnceMigrated
)
{
if
(
this
.
propertiesSet
)
{
throw
new
IllegalStateException
(
"setCloseDataSourceOnceMigrated "
+
"invoked after afterPropertiesSet"
);
}
super
.
setCloseDataSourceOnceMigrated
(
closeDataSourceOnceMigrated
);
}
@Override
public
void
afterPropertiesSet
()
throws
LiquibaseException
{
this
.
propertiesSet
=
true
;
}
};
}
}
}
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/DataSourceClosingSpringLiquibase.java
0 → 100644
View file @
e53e8bf7
/*
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
.
autoconfigure
.
liquibase
;
import
java.lang.reflect.Method
;
import
javax.sql.DataSource
;
import
liquibase.exception.LiquibaseException
;
import
liquibase.integration.spring.SpringLiquibase
;
import
org.springframework.beans.factory.DisposableBean
;
import
org.springframework.util.ReflectionUtils
;
/**
* A custom {@link SpringLiquibase} extension that closes the underlying
* {@link DataSource} once the database has been migrated.
*
* @author Andy Wilkinson
* @since 2.0.6
*/
public
class
DataSourceClosingSpringLiquibase
extends
SpringLiquibase
implements
DisposableBean
{
private
volatile
boolean
closeDataSourceOnceMigrated
=
true
;
public
void
setCloseDataSourceOnceMigrated
(
boolean
closeDataSourceOnceMigrated
)
{
this
.
closeDataSourceOnceMigrated
=
closeDataSourceOnceMigrated
;
}
@Override
public
void
afterPropertiesSet
()
throws
LiquibaseException
{
super
.
afterPropertiesSet
();
if
(
this
.
closeDataSourceOnceMigrated
)
{
closeDataSource
();
}
}
private
void
closeDataSource
()
{
Class
<?>
dataSourceClass
=
getDataSource
().
getClass
();
Method
closeMethod
=
ReflectionUtils
.
findMethod
(
dataSourceClass
,
"close"
);
if
(
closeMethod
!=
null
)
{
ReflectionUtils
.
invokeMethod
(
closeMethod
,
getDataSource
());
}
}
@Override
public
void
destroy
()
throws
Exception
{
if
(!
this
.
closeDataSourceOnceMigrated
)
{
closeDataSource
();
}
}
}
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java
View file @
e53e8bf7
...
...
@@ -16,7 +16,6 @@
package
org
.
springframework
.
boot
.
autoconfigure
.
liquibase
;
import
java.lang.reflect.Method
;
import
java.util.function.Supplier
;
import
javax.annotation.PostConstruct
;
...
...
@@ -24,7 +23,6 @@ import javax.persistence.EntityManagerFactory;
import
javax.sql.DataSource
;
import
liquibase.change.DatabaseChange
;
import
liquibase.exception.LiquibaseException
;
import
liquibase.integration.spring.SpringLiquibase
;
import
org.springframework.beans.factory.ObjectProvider
;
...
...
@@ -50,7 +48,6 @@ import org.springframework.jdbc.core.JdbcOperations;
import
org.springframework.orm.jpa.AbstractEntityManagerFactoryBean
;
import
org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean
;
import
org.springframework.util.Assert
;
import
org.springframework.util.ReflectionUtils
;
/**
* {@link EnableAutoConfiguration Auto-configuration} for Liquibase.
...
...
@@ -211,26 +208,4 @@ public class LiquibaseAutoConfiguration {
}
/**
* A custom {@link SpringLiquibase} extension that closes the underlying
* {@link DataSource} once the database has been migrated.
*/
private
static
final
class
DataSourceClosingSpringLiquibase
extends
SpringLiquibase
{
@Override
public
void
afterPropertiesSet
()
throws
LiquibaseException
{
super
.
afterPropertiesSet
();
closeDataSource
();
}
private
void
closeDataSource
()
{
Class
<?>
dataSourceClass
=
getDataSource
().
getClass
();
Method
closeMethod
=
ReflectionUtils
.
findMethod
(
dataSourceClass
,
"close"
);
if
(
closeMethod
!=
null
)
{
ReflectionUtils
.
invokeMethod
(
closeMethod
,
getDataSource
());
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment