Spring package with annotation

I'm new to the Spring party, looking for an example of a developed version of Spring with the Annotation concept .

This link ( click ) talks about the Spring package, but not the Spring package with the concept of annotation. As discussed in this link, the documentation is not clear. I am using the latest Spring framework. I want to avoid the xml configuration.

Is Spring batch a very good batch tool? or are there any better tools for batch processing instead of spring batch?

Are there any restrictions in the Spring package?

+6
source share
5 answers

The Spring package only supports limited functionality, which you can customize with annotations. These are basically listener callbacks such as @BeforeStep or @AfterChunk . Using these annotations, you can choose to either implement the appropriate interfaces or use annotated methods. An annotated bean must be entered in a task, step or fragment ( reader , processor , writer , retry-policy , skip-policy or listeners ) in the XML configuration, which you cannot avoid.

+3
source

You are looking at http://www.joshlong.com/jl/blogPost/java_configuration_with_spring_batch.html

If you have defined all the necessary "bean" object, in the main method you can simply get them in the application context.

 ApplicationContext ctx = new AnnotationConfigApplicationContext(MainConfig.class); job = (Job) ctx.getBean("SOME JOB"); jobLauncher = (JobLauncher) ctx.getBean("jobLauncher"); jobLauncher.run(job, jobParams); 
+1
source

I used the following technologies for this example: Spring package 3.0.5.RELEASE,

JDK 1.7,

Eclipse Mars Release (4.5.0),

Maven 3.3.3,

Springframework 4.0.5.ReLEASE.

Step 1: Create Simple POJOs, Employee.java

 public class Employee { private String name; private String empId; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmpId() { return empId; } public void setEmpId(String empId) { this.empId = empId; } @Override public String toString() { return "Employee [name=" + name + ", empId=" + empId + "]"; } } 

Step 2: Create a java Class, Reader.java Class

 package com.batch.main; import org.springframework.batch.item.ItemReader; import org.springframework.batch.item.NonTransientResourceException; import org.springframework.batch.item.ParseException; import org.springframework.batch.item.UnexpectedInputException; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import com.batch.beans.Employee; @Component("classReader") public class ClassReader implements ItemReader<Employee> { @Override public Employee read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException { Employee emp=new Employee(); //Set values in Employee object emp.setEmpId("123456"); emp.setName("Manohar"); System.out.println("Inside ClassReader..." + emp); return emp; } } 

Step 3: Create a java class, class Processor.java

 package com.batch.main; import org.springframework.batch.item.ItemProcessor; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import com.batch.beans.Employee; @Component("classProcesser") public class ClassProcessor implements ItemProcessor<Employee, Employee>{ @Override public Employee process(Employee emp) throws Exception { System.out.println("Inside ClassProcessor..." + emp); return emp; } } 

Step 4: Create a java class, ClassWriter.java

 package com.batch.main; import java.util.List; import org.springframework.batch.item.ItemWriter; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import com.batch.beans.Employee; @Component("classWriter") public class ClassWriter implements ItemWriter<Employee> { @Override public void write(List<? extends Employee> arg0) throws Exception { System.out.println("Inside ClassWriter..." + arg0); } } 

Step 5: create context.xml

 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd"> <bean id="transactionManager" class="org.springframework.batch.support.transaction. ResourcelessTransactionMana ger" /> <bean id="jobRepository" class="org.springframework.batch.core.repository.support. MapJobRepositoryFactoryBean"> <property name="transactionManager" ref="transactionManager" /> </bean> <bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher"> <property name="jobRepository" ref="jobRepository" /> </bean> </beans> 

Step 6: create job.xml

 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:batch="http://www.springframework.org/schema/batch" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:crypt="http://springcryptoutils.com/schema/crypt" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://springcryptoutils.com/schema/crypt http://springcryptoutils.com/schema/crypt.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd"> <import resource="/context.xml" /> <context:component-scan base-package="com.batch" /> <batch:job id="loadJob" xmlns="http://www.springframework.org/schema/batch"> <batch:step id="step1" > <batch:tasklet> <batch:chunk reader="classReader" writer="classWriter" processor="classProcesser" commit-interval="1" /> </batch:tasklet> </batch:step> </batch:job> <!-- <bean id="classReader" class="com.batch.main.ClassReader" > </bean> <bean id="classWriter" class="com.batch.main.ClassWriter" ></bean> <bean id="classProcesser" class="com.batch.main.ClassProcessor" > </bean>--> 

Step 7: Finally, Create Main.java

 package com.batch.main; import org.springframework.batch.core.Job; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobParameters; import org.springframework.batch.core.JobParametersInvalidException; import org.springframework.batch.core.launch.JobLauncher; import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException; import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException; import org.springframework.batch.core.repository.JobRestartException; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; @Configuration @ImportResource({"classpath:/com/spring/job/job.xml"})//load configuration file from classpath public class Main { public static void main(String[] args) throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException { @SuppressWarnings("resource") AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(Main.class); context.refresh(); //load jobLauncher details from context.xml file JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher"); //load Job details from job.xml file Job job = (Job) context.getBean("loadJob"); //run job JobExecution execution = jobLauncher.run(job, new JobParameters()); System.out.println("Exit Status : " + execution.getStatus()); } } 

Now run the program as a Java application, you will see the output on the console.

See http://manohark.com/simple-spring-batch-example-using-annotations/ for more details.

0
source

Please take a look at the tutorial along with github repo, maybe this will be useful for you.

Spring Package, as any other tool has its own limitations, but still quite flexible. I use it in a real project with 1,500 different jobs, and I think it is pretty good and covers a lot of use cases in batch processing applications.

0
source

I ran into the same issue when I read about Spring batch annotations. The best place you can read Spring annotations is the reference documentation. The concepts are the same for the package, regardless of whether XML or Java is used.

I would suggest the following steps:

  • Learning Party Concepts
  • Try making simple batch applications (reading from a database and saving to CSV)
  • After conveniently using basic concepts, study books such as Spring Batch Pro.
  • If you run into a problem, check out the online code and then cross-reference this documentation with the above Spring documentation.
0
source

Source: https://habr.com/ru/post/905053/


All Articles