java - Overwriting spring-boot autoconfiguration -
i'm little bit confused behaviour of spring-boot when overwriting specific autoconfigurations.
i partly overwrite batchautoconfiguration, guess, question not specific batchautoconfiguration. actually, want "overwrite" 2 methods of class: public batchdatabaseinitializer batchdatabaseinitializer()
, public exitcodegenerator jobexecutionexitcodegenerator()
.
therefore, i've written following code: package ch.test.autoconfig.autoconfigure;
import org.springframework.batch.core.launch.joblauncher; import org.springframework.boot.exitcodegenerator; import org.springframework.boot.autoconfigure.autoconfigureafter; import org.springframework.boot.autoconfigure.autoconfigurebefore; import org.springframework.boot.autoconfigure.batch.batchautoconfiguration; import org.springframework.boot.autoconfigure.batch.batchdatabaseinitializer; import org.springframework.boot.autoconfigure.batch.batchproperties; import org.springframework.boot.autoconfigure.batch.jobexecutionexitcodegenerator; 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.orm.jpa.hibernatejpaautoconfiguration; import org.springframework.boot.context.properties.enableconfigurationproperties; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; import org.springframework.jdbc.core.jdbcoperations; import javax.sql.datasource; /** * i'm using same annotations defined in batchautoconfiguration... */ @configuration @conditionalonclass({ joblauncher.class, datasource.class, jdbcoperations.class }) @autoconfigureafter(hibernatejpaautoconfiguration.class) @conditionalonbean(joblauncher.class) @enableconfigurationproperties(batchproperties.class) // ... add @autoconfigurebefore(batchautoconfiguration.class) take precedence on batchautoconfiguration @autoconfigurebefore(batchautoconfiguration.class) public class javabatchautoconfiguration { @bean @conditionalonmissingbean @conditionalonbean(datasource.class) public batchdatabaseinitializer batchdatabaseinitializer() { system.out.println("entering overwritten batchdatabaseinitializer"); return new batchdatabaseinitializer(); } @bean @conditionalonmissingbean public exitcodegenerator jobexecutionexitcodegenerator() { system.out.println("entering overwritten jobexecutionexitcodegenerator"); return new jobexecutionexitcodegenerator(); } }
as mentioned in code, i'm using exact same annotions class , methods, definend in batchautoconfiguration.class
. exception adding @autoconfigurebefore(batchautoconfiguration.class)
. since should take precedence on batchautoconfiguration, assumed, kind of "overwrite" original.
to test is, use following pom.xml:
<?xml version="1.0" encoding="utf-8"?> <project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupid>ch.test.autoconfig</groupid> <artifactid>auto_config_test</artifactid> <version>1.0-snapshot</version> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>1.2.3.release</version> </parent> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-batch</artifactid> </dependency> </dependencies> </project>
and i'm using simple job main method:
package ch.test.autoconfig; import org.springframework.batch.core.job; import org.springframework.batch.core.step; import org.springframework.batch.core.stepcontribution; import org.springframework.batch.core.configuration.annotation.enablebatchprocessing; import org.springframework.batch.core.configuration.annotation.jobbuilderfactory; import org.springframework.batch.core.configuration.annotation.stepbuilderfactory; import org.springframework.batch.core.scope.context.chunkcontext; import org.springframework.batch.core.step.tasklet.tasklet; import org.springframework.batch.repeat.repeatstatus; import org.springframework.beans.factory.annotation.autowired; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.context.annotation.bean; /** * created u802552 on 01.06.2015. */ @springbootapplication @enablebatchprocessing public class mainjob { @autowired private jobbuilderfactory jobs; @autowired private stepbuilderfactory steps; @bean protected tasklet tasklet() { return new tasklet() { public repeatstatus execute(stepcontribution contribution, chunkcontext context) { return repeatstatus.finished; } }; } @bean public job job() throws exception { return this.jobs.get("job").start(step1()).build(); } @bean protected step step1() throws exception { return this.steps.get("step1").tasklet(tasklet()).build(); } public static void main(string[] args) throws exception { // switch on auto-configuration-report system.setproperty("debug","true"); springapplication.run(mainjob.class, args); } }
i expect, 2 overwritten methods called, auto-configuration-report states, javabatchautoconfiguration class has "negative match" because of missing "joblauncher" instance. report states, original batchautoconfiguration class has positive match same conditionalonbean
check.
if comment @conditionalonbean(joblauncher.class)
on class , @conditionalonbean(datasource.class)
on batchdatabaseinitializer
-method, works expected.
does have explanation behaviour?
thanks hansjörg
you making way complex. thing need create configuration defines 2 specific beans want use.
@configuration public class mybatchconfiguration { @bean public exitcodegenerator myexitcodegenerator() { return new myexitcodegenerator(); } @bean public batchdatabaseinitializer mybatchdatabaseinitializer() { return new mybatchdatabaseinitializer(); } }
that need.
Comments
Post a Comment