2

我正在使用 JBoss 实现JBeret在 Java SE 下构建JSR352(Java 平台的批处理应用程序)演示。我想为这项工作运行集成测试。在这个批处理作业中,有 3 个组件:

  • io.mincong.batch.MyItemReader
  • io.mincong.batch.MyItemProcessor
  • io.mincong.batch.MyItemWriter

它们都被命名为带有注释的bean @Named,例如

@Named
public class MyItemProcessor implements ItemProcessor {
     // ...
}

多亏了这个注释,我可以使用作业类,而无需io.mincong.batch在我的作业文件中输入包名前缀myJob.xml这在 Java EE 容器中可以正常工作,例如 WildFly 10。但是,在 Java SE 中,它不再工作了,我必须添加包前缀。这里的工作 xml myJob.xml

<?xml version="1.0" encoding="UTF-8"?>
<job id="myJob" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/jobXML_1_0.xsd"
    version="1.0">
    <step id="printID">
        <chunk item-count="5">
            <!-- This works :
            <reader ref="io.mincong.batch.MyItemReader"/>
            <processor ref="io.mincong.batch.MyItemProcessor"/>
            <writer ref="io.mincong.batch.MyItemWriter"/> -->

            <!-- This doesn't work -->
            <reader ref="myItemReader"/>
            <processor ref="myItemProcessor"/>
            <writer ref="myItemWriter"/>
        </chunk>
    </step>
</job>

你可以在附件中找到 2 mvn install log,有和没有包前缀。换句话说,使用和不使用@Named。为什么会发生此错误?我是否缺少一些依赖项?任何想法 ?

你也可以在下面看到我的 pom.xml。附加信息:


使用 @Named 时 Maven 安装失败

Mincongs-MBP:batch mincong$ mvn clean install
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building batch 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ batch ---
[INFO] Deleting /Users/mincong/Documents/workspace/batch/target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ batch ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 2 resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ batch ---
[INFO] Compiling 3 source files to /Users/mincong/Documents/workspace/batch/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ batch ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ batch ---
[INFO] Compiling 1 source file to /Users/mincong/Documents/workspace/batch/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ batch ---
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ batch ---
[INFO] Building jar: /Users/mincong/Documents/workspace/batch/target/batch.jar
[INFO] 
[INFO] --- maven-failsafe-plugin:2.17:integration-test (default) @ batch ---
[INFO] Failsafe report directory: /Users/mincong/Documents/workspace/batch/target/failsafe-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running io.mincong.batch.JobIT
Jun 26, 2016 1:21:16 PM org.jboss.weld.bootstrap.WeldStartup <clinit>
INFO: WELD-000900: 2.3.4 (Final)
Jun 26, 2016 1:21:16 PM org.jboss.weld.environment.deployment.discovery.DiscoveryStrategyFactory create
INFO: WELD-ENV-000020: Using jandex for bean discovery
Jun 26, 2016 1:21:16 PM org.jboss.weld.bootstrap.WeldStartup startContainer
INFO: WELD-000101: Transactional services not available. Injection of @Inject UserTransaction not available. Transactional observers will be invoked synchronously.
Jun 26, 2016 1:21:16 PM org.jboss.weld.bootstrap.MissingDependenciesRegistry handleResourceLoadingException
INFO: WELD-000119: Not generating any bean definitions from org.jberet.support.io.KafkaItemReader because of underlying class loading error: Type org.apache.kafka.clients.consumer.KafkaConsumer not found.  If this is unexpected, enable DEBUG logging to see the full error.
Jun 26, 2016 1:21:16 PM org.jboss.weld.bootstrap.MissingDependenciesRegistry handleResourceLoadingException
INFO: WELD-000119: Not generating any bean definitions from org.jberet.support.io.KafkaItemWriter because of underlying class loading error: Type org.apache.kafka.clients.producer.KafkaProducer not found.  If this is unexpected, enable DEBUG logging to see the full error.
Jun 26, 2016 1:21:16 PM org.jboss.weld.bootstrap.MissingDependenciesRegistry handleResourceLoadingException
INFO: WELD-000119: Not generating any bean definitions from org.jberet.support.io.HornetQItemReader because of underlying class loading error: Type org.hornetq.api.core.HornetQException not found.  If this is unexpected, enable DEBUG logging to see the full error.
Jun 26, 2016 1:21:16 PM org.jboss.weld.bootstrap.MissingDependenciesRegistry handleResourceLoadingException
INFO: WELD-000119: Not generating any bean definitions from org.jberet.support.io.JasperReportsBatchlet because of underlying class loading error: Type net.sf.jasperreports.engine.data.JRAbstractTextDataSource not found.  If this is unexpected, enable DEBUG logging to see the full error.
Jun 26, 2016 1:21:16 PM org.jboss.weld.bootstrap.MissingDependenciesRegistry handleResourceLoadingException
INFO: WELD-000119: Not generating any bean definitions from org.jberet.support.io.HornetQItemWriter because of underlying class loading error: Type org.hornetq.api.core.HornetQException not found.  If this is unexpected, enable DEBUG logging to see the full error.
Jun 26, 2016 1:21:17 PM org.jboss.weld.environment.se.WeldContainer initialize
INFO: WELD-ENV-002003: Weld SE container STATIC_INSTANCE initialized
Jun 26, 2016 1:21:17 PM io.mincong.batch.JobIT setupJobEnv
INFO: setup env finished.
Jun 26, 2016 1:21:17 PM io.mincong.batch.JobIT shouldStart
INFO: executionId=1
Jun 26, 2016 1:21:17 PM org.jberet.runtime.runner.ChunkRunner run
ERROR: item-count=10, time-limit=0, skip-limit=-1, skipCount=0, retry-limit=-1, retryCount=0
Jun 26, 2016 1:21:17 PM org.jberet.runtime.runner.ChunkRunner run
ERROR: JBERET000007: Failed to run job myJob, printID, org.jberet.job.model.Chunk@4256e00a
java.lang.IllegalStateException: JBERET000600: Failed to create artifact with ref name myItemReader.  Ensure CDI beans.xml is present and batch.xml, if any, is configured properly.
    at org.jberet.runtime.context.JobContextImpl.createArtifact(JobContextImpl.java:185)
    at org.jberet.runtime.runner.AbstractRunner.createArtifact(AbstractRunner.java:156)
    at org.jberet.runtime.runner.ChunkRunner.run(ChunkRunner.java:134)
    at org.jberet.runtime.runner.StepExecutionRunner.runBatchletOrChunk(StepExecutionRunner.java:226)
    at org.jberet.runtime.runner.StepExecutionRunner.run(StepExecutionRunner.java:147)
    at org.jberet.runtime.runner.CompositeExecutionRunner.runStep(CompositeExecutionRunner.java:164)
    at org.jberet.runtime.runner.CompositeExecutionRunner.runFromHeadOrRestartPoint(CompositeExecutionRunner.java:88)
    at org.jberet.runtime.runner.JobExecutionRunner.run(JobExecutionRunner.java:60)
    at org.jberet.spi.JobExecutor$1.run(JobExecutor.java:99)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalStateException: JBERET000600: Failed to create artifact with ref name myItemReader.  Ensure CDI beans.xml is present and batch.xml, if any, is configured properly.
    at org.jberet.creation.ArtifactFactoryWrapper.getClassFromBatchXmlOrClassLoader(ArtifactFactoryWrapper.java:65)
    at org.jberet.creation.ArtifactFactoryWrapper.create(ArtifactFactoryWrapper.java:41)
    at org.jberet.runtime.context.JobContextImpl.createArtifact(JobContextImpl.java:183)
    ... 11 more
Caused by: java.lang.ClassNotFoundException: myItemReader
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at org.jberet.creation.ArtifactFactoryWrapper.getClassFromBatchXmlOrClassLoader(ArtifactFactoryWrapper.java:63)
    ... 13 more

Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 4.416 sec <<< FAILURE! - in io.mincong.batch.JobIT
shouldStart(io.mincong.batch.JobIT)  Time elapsed: 4.366 sec  <<< FAILURE!
java.lang.AssertionError: expected:<COMPLETED> but was:<FAILED>
    at org.junit.Assert.fail(Assert.java:91)
    at org.junit.Assert.failNotEquals(Assert.java:645)
    at org.junit.Assert.assertEquals(Assert.java:126)
    at org.junit.Assert.assertEquals(Assert.java:145)
    at io.mincong.batch.JobIT.shouldStart(JobIT.java:47)

Weld SE container STATIC_INSTANCE shut down by shutdown hook

Results :

Failed tests: 
  JobIT.shouldStart:47 expected:<COMPLETED> but was:<FAILED>

Tests run: 1, Failures: 1, Errors: 0, Skipped: 0

[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] 
[INFO] --- maven-failsafe-plugin:2.17:verify (default) @ batch ---
[INFO] Failsafe report directory: /Users/mincong/Documents/workspace/batch/target/failsafe-reports
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.863 s
[INFO] Finished at: 2016-06-26T13:21:20+02:00
[INFO] Final Memory: 31M/273M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-failsafe-plugin:2.17:verify (default) on project batch: There are test failures.
[ERROR] 
[ERROR] Please refer to /Users/mincong/Documents/workspace/batch/target/failsafe-reports for the individual test results.
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

不使用 @Named 时 Maven 安装成功

Mincongs-MBP:batch mincong$ mvn clean install
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building batch 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ batch ---
[INFO] Deleting /Users/mincong/Documents/workspace/batch/target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ batch ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 2 resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ batch ---
[INFO] Compiling 3 source files to /Users/mincong/Documents/workspace/batch/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ batch ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ batch ---
[INFO] Compiling 1 source file to /Users/mincong/Documents/workspace/batch/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ batch ---
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ batch ---
[INFO] Building jar: /Users/mincong/Documents/workspace/batch/target/batch.jar
[INFO] 
[INFO] --- maven-failsafe-plugin:2.17:integration-test (default) @ batch ---
[INFO] Failsafe report directory: /Users/mincong/Documents/workspace/batch/target/failsafe-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running io.mincong.batch.JobIT
Jun 26, 2016 1:25:25 PM org.jboss.weld.bootstrap.WeldStartup <clinit>
INFO: WELD-000900: 2.3.4 (Final)
Jun 26, 2016 1:25:25 PM org.jboss.weld.environment.deployment.discovery.DiscoveryStrategyFactory create
INFO: WELD-ENV-000020: Using jandex for bean discovery
Jun 26, 2016 1:25:26 PM org.jboss.weld.bootstrap.WeldStartup startContainer
INFO: WELD-000101: Transactional services not available. Injection of @Inject UserTransaction not available. Transactional observers will be invoked synchronously.
Jun 26, 2016 1:25:26 PM org.jboss.weld.bootstrap.MissingDependenciesRegistry handleResourceLoadingException
INFO: WELD-000119: Not generating any bean definitions from org.jberet.support.io.KafkaItemReader because of underlying class loading error: Type org.apache.kafka.clients.consumer.KafkaConsumer not found.  If this is unexpected, enable DEBUG logging to see the full error.
Jun 26, 2016 1:25:26 PM org.jboss.weld.bootstrap.MissingDependenciesRegistry handleResourceLoadingException
INFO: WELD-000119: Not generating any bean definitions from org.jberet.support.io.KafkaItemWriter because of underlying class loading error: Type org.apache.kafka.clients.producer.KafkaProducer not found.  If this is unexpected, enable DEBUG logging to see the full error.
Jun 26, 2016 1:25:26 PM org.jboss.weld.bootstrap.MissingDependenciesRegistry handleResourceLoadingException
INFO: WELD-000119: Not generating any bean definitions from org.jberet.support.io.HornetQItemReader because of underlying class loading error: Type org.hornetq.api.core.HornetQException not found.  If this is unexpected, enable DEBUG logging to see the full error.
Jun 26, 2016 1:25:26 PM org.jboss.weld.bootstrap.MissingDependenciesRegistry handleResourceLoadingException
INFO: WELD-000119: Not generating any bean definitions from org.jberet.support.io.JasperReportsBatchlet because of underlying class loading error: Type net.sf.jasperreports.engine.data.JRAbstractTextDataSource not found.  If this is unexpected, enable DEBUG logging to see the full error.
Jun 26, 2016 1:25:26 PM org.jboss.weld.bootstrap.MissingDependenciesRegistry handleResourceLoadingException
INFO: WELD-000119: Not generating any bean definitions from org.jberet.support.io.HornetQItemWriter because of underlying class loading error: Type org.hornetq.api.core.HornetQException not found.  If this is unexpected, enable DEBUG logging to see the full error.
Jun 26, 2016 1:25:26 PM org.jboss.weld.environment.se.WeldContainer initialize
INFO: WELD-ENV-002003: Weld SE container STATIC_INSTANCE initialized
Jun 26, 2016 1:25:26 PM io.mincong.batch.JobIT setupJobEnv
INFO: setup env finished.
Jun 26, 2016 1:25:26 PM io.mincong.batch.JobIT shouldStart
INFO: executionId=1
Jun 26, 2016 1:25:26 PM io.mincong.batch.MyItemWriter writeItems
INFO: 0
Jun 26, 2016 1:25:26 PM io.mincong.batch.MyItemWriter writeItems
INFO: 1
...
Jun 26, 2016 1:25:26 PM io.mincong.batch.MyItemWriter writeItems
INFO: 95
Jun 26, 2016 1:25:26 PM io.mincong.batch.MyItemWriter writeItems
INFO: 96
Jun 26, 2016 1:25:26 PM io.mincong.batch.MyItemWriter writeItems
INFO: 97
Jun 26, 2016 1:25:26 PM io.mincong.batch.MyItemWriter writeItems
INFO: 98
Jun 26, 2016 1:25:26 PM io.mincong.batch.MyItemWriter writeItems
INFO: 99
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.304 sec - in io.mincong.batch.JobIT
Weld SE container STATIC_INSTANCE shut down by shutdown hook

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] 
[INFO] --- maven-failsafe-plugin:2.17:verify (default) @ batch ---
[INFO] Failsafe report directory: /Users/mincong/Documents/workspace/batch/target/failsafe-reports
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ batch ---
[INFO] Installing /Users/mincong/Documents/workspace/batch/target/batch.jar to /Users/mincong/.m2/repository/io/github/mincongh/batch/0.0.1-SNAPSHOT/batch-0.0.1-SNAPSHOT.jar
[INFO] Installing /Users/mincong/Documents/workspace/batch/pom.xml to /Users/mincong/.m2/repository/io/github/mincongh/batch/0.0.1-SNAPSHOT/batch-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.810 s
[INFO] Finished at: 2016-06-26T13:25:28+02:00
[INFO] Final Memory: 32M/284M
[INFO] ------------------------------------------------------------------------

pom.xml

<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>io.github.mincongh</groupId>
    <artifactId>batch</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>batch</name>
    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <maven.compiler.testSource>1.7</maven.compiler.testSource>
        <maven.compiler.testTarget>1.7</maven.compiler.testTarget>
        <failOnMissingWebXml>false</failOnMissingWebXml>
        <arquillian.version>1.0.0.Alpha5</arquillian.version>
        <wildfly.version>10.0.0.Final</wildfly.version>
        <jberet.version>1.3.0.Beta2</jberet.version>
    </properties>

    <build>
        <finalName>batch</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.17</version>
                <!-- enable execution to run an integration test -->
                <executions>
                  <execution>
                    <goals>
                      <goal>integration-test</goal>
                      <goal>verify</goal>
                    </goals>
                  </execution>
                </executions>
                <configuration>
                    <systemPropertyVariables>
                        <jberet.tmp.dir>${project.build.directory}${file.separator}tmp</jberet.tmp.dir>
                    </systemPropertyVariables>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.jboss.spec</groupId>
            <artifactId>jboss-javaee-7.0</artifactId>
            <version>1.0.0.Final</version>
            <type>pom</type>
            <scope>provided</scope>
        </dependency>
        <!-- Write test in JUnit 4.x -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.logging</groupId>
            <artifactId>jboss-logging</artifactId>
            <version>3.3.0.Final</version>
            <scope>provided</scope>
        </dependency>
        <!-- jberet dependencies -->
        <dependency>
            <groupId>org.jboss.spec.javax.batch</groupId>
            <artifactId>jboss-batch-api_1.0_spec</artifactId>
            <version>1.0.0.Final</version>
        </dependency>
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>
        <dependency>
            <groupId>javax.enterprise</groupId>
            <artifactId>cdi-api</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.spec.javax.transaction</groupId>
            <artifactId>jboss-transaction-api_1.2_spec</artifactId>
            <version>1.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.marshalling</groupId>
            <artifactId>jboss-marshalling</artifactId>
            <version>1.4.11.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.weld</groupId>
            <artifactId>weld-core</artifactId>
            <version>2.3.4.Final</version>
        </dependency>
        <dependency>
            <groupId>org.wildfly.security</groupId>
            <artifactId>wildfly-security-manager</artifactId>
            <version>1.1.2.Final</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>19.0</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.weld.se</groupId>
            <artifactId>weld-se</artifactId>
            <version>2.3.4.Final</version>
        </dependency>
        <!-- contains all classes of JBeret batch runtime. This library is 
             required to run batch applications in both Java SE and Java EE 
             environment. -->
        <dependency>
            <groupId>org.jberet</groupId>
            <artifactId>jberet-core</artifactId>
            <version>${jberet.version}</version>
        </dependency>
        <!-- contains implementation classes of JBeret batch runtime 
             specifically for Java SE environment. -->
        <dependency>
            <groupId>org.jberet</groupId>
            <artifactId>jberet-se</artifactId>
            <version>${jberet.version}</version>
        </dependency>
        <!-- a zip file containing the following content, and provides a 
             good start to write and run batch applications with JBeret in 
             Java SE. 
             *  All libraries required to run batch applications in 
                Java SE environment
             *  default configuration file, jberet.properties for 
                configuring JBeret batch runtime in Java SE environment 
             *  jberet-support library and its common transitive 
                dependencies 
         -->
        <dependency>
            <groupId>org.jberet</groupId>
            <artifactId>jberet-distribution</artifactId>
            <version>${jberet.version}</version>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.191</version>
        </dependency>
    </dependencies>

</project>
4

2 回答 2

3

经过几个小时的比较,我终于找出了区别!bean 发现模式beans.xmlannotated并且应该是all

<?xml version="1.0"?>
<!-- <beans bean-discovery-mode="annotated" version="1.1" -->
<beans bean-discovery-mode="all" version="1.1"
  xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"/>

根据Javadoc - Enum BeanDiscoveryMode

  • ALL:将考虑存档中的所有类型。
  • ANNOTATED: 只有那些带有 bean 定义注解的类型才会被考虑。
  • NONE:此存档将被忽略。
于 2016-06-26T16:10:08.663 回答
0

您应该将@Dependent注释添加到您的 Reader、Processor 和 Writer。

要确保上下文和依赖注入 (CDI) 在您的批处理工件中工作,请按照以下步骤操作。

  1. 使用命名注释将批处理工件实现定义为 CDI 命名 bean。
  2. 为您的批处理工件提供一个公共的、空的、无参数的构造函数。
  3. 在作业定义文件中指定批处理工件的 CDI 名称,而不是使用类的完全限定名称。
  4. 通过使用 javax.enterprise.context.Dependent 批注对批处理工件进行批注或在应用程序中包含空的 beans.xml 部署描述,确保您的模块是 CDI bean 存档。
@Dependent
@Named("MyItemReaderImpl")
public class MyItemReaderImpl implements ItemReader { ... }
<step id="stepA" next="stepB">
  <chunk>
    <reader ref="MyItemReaderImpl"></reader>
    ...
  </chunk>
</step>

更多信息: https ://docs.oracle.com/javaee/7/tutorial/batch-processing005.htm

于 2019-06-14T14:46:31.353 回答