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
02bea782
Commit
02bea782
authored
Jul 08, 2019
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Retain as much Batch auto-config as possible without Spring JDBC
Closes gh-17451
parent
a023f308
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
107 additions
and
25 deletions
+107
-25
BatchAutoConfiguration.java
...work/boot/autoconfigure/batch/BatchAutoConfiguration.java
+20
-25
BatchAutoConfigurationWithoutJdbcTests.java
...nfigure/batch/BatchAutoConfigurationWithoutJdbcTests.java
+87
-0
No files found.
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java
View file @
02bea782
...
@@ -39,7 +39,7 @@ import org.springframework.context.annotation.Bean;
...
@@ -39,7 +39,7 @@ import org.springframework.context.annotation.Bean;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Import
;
import
org.springframework.context.annotation.Import
;
import
org.springframework.core.io.ResourceLoader
;
import
org.springframework.core.io.ResourceLoader
;
import
org.springframework.jdbc.
core.JdbcOperations
;
import
org.springframework.jdbc.
datasource.init.DatabasePopulator
;
import
org.springframework.util.StringUtils
;
import
org.springframework.util.StringUtils
;
/**
/**
...
@@ -60,37 +60,20 @@ import org.springframework.util.StringUtils;
...
@@ -60,37 +60,20 @@ import org.springframework.util.StringUtils;
* @since 1.0.0
* @since 1.0.0
*/
*/
@Configuration
@Configuration
@ConditionalOnClass
({
JobLauncher
.
class
,
DataSource
.
class
,
JdbcOperations
.
class
})
@ConditionalOnClass
({
JobLauncher
.
class
,
DataSource
.
class
})
@AutoConfigureAfter
(
HibernateJpaAutoConfiguration
.
class
)
@AutoConfigureAfter
(
HibernateJpaAutoConfiguration
.
class
)
@ConditionalOnBean
(
JobLauncher
.
class
)
@ConditionalOnBean
(
JobLauncher
.
class
)
@EnableConfigurationProperties
(
BatchProperties
.
class
)
@EnableConfigurationProperties
(
BatchProperties
.
class
)
@Import
(
BatchConfigurerConfiguration
.
class
)
@Import
(
BatchConfigurerConfiguration
.
class
)
public
class
BatchAutoConfiguration
{
public
class
BatchAutoConfiguration
{
private
final
BatchProperties
properties
;
private
final
JobParametersConverter
jobParametersConverter
;
public
BatchAutoConfiguration
(
BatchProperties
properties
,
ObjectProvider
<
JobParametersConverter
>
jobParametersConverter
)
{
this
.
properties
=
properties
;
this
.
jobParametersConverter
=
jobParametersConverter
.
getIfAvailable
();
}
@Bean
@ConditionalOnMissingBean
@ConditionalOnBean
(
DataSource
.
class
)
public
BatchDataSourceInitializer
batchDataSourceInitializer
(
DataSource
dataSource
,
ResourceLoader
resourceLoader
)
{
return
new
BatchDataSourceInitializer
(
dataSource
,
resourceLoader
,
this
.
properties
);
}
@Bean
@Bean
@ConditionalOnMissingBean
@ConditionalOnMissingBean
@ConditionalOnProperty
(
prefix
=
"spring.batch.job"
,
name
=
"enabled"
,
havingValue
=
"true"
,
matchIfMissing
=
true
)
@ConditionalOnProperty
(
prefix
=
"spring.batch.job"
,
name
=
"enabled"
,
havingValue
=
"true"
,
matchIfMissing
=
true
)
public
JobLauncherCommandLineRunner
jobLauncherCommandLineRunner
(
JobLauncher
jobLauncher
,
JobExplorer
jobExplorer
,
public
JobLauncherCommandLineRunner
jobLauncherCommandLineRunner
(
JobLauncher
jobLauncher
,
JobExplorer
jobExplorer
,
JobRepository
jobRepository
)
{
JobRepository
jobRepository
,
BatchProperties
properties
)
{
JobLauncherCommandLineRunner
runner
=
new
JobLauncherCommandLineRunner
(
jobLauncher
,
jobExplorer
,
jobRepository
);
JobLauncherCommandLineRunner
runner
=
new
JobLauncherCommandLineRunner
(
jobLauncher
,
jobExplorer
,
jobRepository
);
String
jobNames
=
this
.
properties
.
getJob
().
getNames
();
String
jobNames
=
properties
.
getJob
().
getNames
();
if
(
StringUtils
.
hasText
(
jobNames
))
{
if
(
StringUtils
.
hasText
(
jobNames
))
{
runner
.
setJobNames
(
jobNames
);
runner
.
setJobNames
(
jobNames
);
}
}
...
@@ -106,16 +89,28 @@ public class BatchAutoConfiguration {
...
@@ -106,16 +89,28 @@ public class BatchAutoConfiguration {
@Bean
@Bean
@ConditionalOnMissingBean
(
JobOperator
.
class
)
@ConditionalOnMissingBean
(
JobOperator
.
class
)
public
SimpleJobOperator
jobOperator
(
JobExplorer
jobExplorer
,
JobLauncher
jobLauncher
,
public
SimpleJobOperator
jobOperator
(
JobExplorer
jobExplorer
,
JobLauncher
jobLauncher
,
ListableJobLocator
jobRegistry
,
JobRepository
jobRepository
)
throws
Exception
{
ListableJobLocator
jobRegistry
,
JobRepository
jobRepository
,
ObjectProvider
<
JobParametersConverter
>
jobParametersConverter
)
{
SimpleJobOperator
factory
=
new
SimpleJobOperator
();
SimpleJobOperator
factory
=
new
SimpleJobOperator
();
factory
.
setJobExplorer
(
jobExplorer
);
factory
.
setJobExplorer
(
jobExplorer
);
factory
.
setJobLauncher
(
jobLauncher
);
factory
.
setJobLauncher
(
jobLauncher
);
factory
.
setJobRegistry
(
jobRegistry
);
factory
.
setJobRegistry
(
jobRegistry
);
factory
.
setJobRepository
(
jobRepository
);
factory
.
setJobRepository
(
jobRepository
);
if
(
this
.
jobParametersConverter
!=
null
)
{
jobParametersConverter
.
ifAvailable
(
factory:
:
setJobParametersConverter
);
factory
.
setJobParametersConverter
(
this
.
jobParametersConverter
);
}
return
factory
;
return
factory
;
}
}
@ConditionalOnBean
(
DataSource
.
class
)
@ConditionalOnClass
(
DatabasePopulator
.
class
)
static
class
DataSourceInitializerConfiguration
{
@Bean
@ConditionalOnMissingBean
public
BatchDataSourceInitializer
batchDataSourceInitializer
(
DataSource
dataSource
,
ResourceLoader
resourceLoader
,
BatchProperties
properties
)
{
return
new
BatchDataSourceInitializer
(
dataSource
,
resourceLoader
,
properties
);
}
}
}
}
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationWithoutJdbcTests.java
0 → 100644
View file @
02bea782
/*
* Copyright 2012-2019 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
*
* https://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
.
batch
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.batch.core.configuration.annotation.BatchConfigurer
;
import
org.springframework.batch.core.configuration.annotation.EnableBatchProcessing
;
import
org.springframework.batch.core.explore.JobExplorer
;
import
org.springframework.batch.core.launch.JobLauncher
;
import
org.springframework.batch.core.launch.support.SimpleJobOperator
;
import
org.springframework.batch.core.repository.JobRepository
;
import
org.springframework.boot.autoconfigure.AutoConfigurations
;
import
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration
;
import
org.springframework.boot.test.context.runner.ApplicationContextRunner
;
import
org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions
;
import
org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.transaction.PlatformTransactionManager
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
mockito
.
Mockito
.
mock
;
/**
* Tests for {@link BatchAutoConfiguration} when Spring JDBC is not on the classpath.
*
* @author Andy Wilkinson
*/
@RunWith
(
ModifiedClassPathRunner
.
class
)
@ClassPathExclusions
(
"spring-jdbc-*.jar"
)
public
class
BatchAutoConfigurationWithoutJdbcTests
{
private
final
ApplicationContextRunner
contextRunner
=
new
ApplicationContextRunner
()
.
withConfiguration
(
AutoConfigurations
.
of
(
BatchAutoConfiguration
.
class
,
TransactionAutoConfiguration
.
class
))
.
withUserConfiguration
(
BatchConfiguration
.
class
);
@Test
public
void
whenThereIsNoJdbcOnTheClasspathThenComponentsAreStillAutoConfigured
()
{
this
.
contextRunner
.
run
((
context
)
->
{
assertThat
(
context
).
hasSingleBean
(
JobLauncherCommandLineRunner
.
class
);
assertThat
(
context
).
hasSingleBean
(
JobExecutionExitCodeGenerator
.
class
);
assertThat
(
context
).
hasSingleBean
(
SimpleJobOperator
.
class
);
});
}
@Configuration
@EnableBatchProcessing
static
class
BatchConfiguration
implements
BatchConfigurer
{
@Override
public
JobRepository
getJobRepository
()
throws
Exception
{
return
mock
(
JobRepository
.
class
);
}
@Override
public
PlatformTransactionManager
getTransactionManager
()
throws
Exception
{
return
mock
(
PlatformTransactionManager
.
class
);
}
@Override
public
JobLauncher
getJobLauncher
()
throws
Exception
{
return
mock
(
JobLauncher
.
class
);
}
@Override
public
JobExplorer
getJobExplorer
()
throws
Exception
{
return
mock
(
JobExplorer
.
class
);
}
}
}
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