BAEL-1715- Control Bean Creation Order with @DependsOn annotation (#4028)

* removing Bean injection

* BAEL-1715- Control Bean Creation Order with @DependsOn annotation

* BAEL-1715- Formatting changes, test case improvement

* Created new Config file for UnitTest

* Corrected test case- from BeanCreationException to NoSuchBeanDefinitionException

* correcting error- for circular dependency expected exception is- BeanCreationException
This commit is contained in:
sachinp054 2018-06-11 04:36:27 +05:30 committed by pauljervis
parent 0313684ef6
commit 40d3b96820
8 changed files with 214 additions and 0 deletions

View File

@ -0,0 +1,14 @@
package com.baeldung.dependson;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.baeldung.dependson.config.Config;
import com.baeldung.dependson.file.processor.FileProcessor;
public class DriverApplication {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);
ctx.getBean(FileProcessor.class);
ctx.close();
}
}

View File

@ -0,0 +1,38 @@
package com.baeldung.dependson.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Lazy;
import com.baeldung.dependson.file.processor.FileProcessor;
import com.baeldung.dependson.file.reader.FileReader;
import com.baeldung.dependson.file.writer.FileWriter;
import com.baeldung.dependson.shared.File;
@Configuration
@ComponentScan("com.baeldung.dependson")
public class Config {
@Autowired
File file;
@Bean("fileProcessor")
@DependsOn({"fileReader","fileWriter"})
@Lazy
public FileProcessor fileProcessor(){
return new FileProcessor(file);
}
@Bean("fileReader")
public FileReader fileReader(){
return new FileReader(file);
}
@Bean("fileWriter")
public FileWriter fileWriter(){
return new FileWriter(file);
}
}

View File

@ -0,0 +1,19 @@
package com.baeldung.dependson.file.processor;
import org.springframework.beans.factory.annotation.Autowired;
import com.baeldung.dependson.file.reader.FileReader;
import com.baeldung.dependson.file.writer.FileWriter;
import com.baeldung.dependson.shared.File;
public class FileProcessor {
File file;
public FileProcessor(File file){
this.file = file;
if(file.getText().contains("write") && file.getText().contains("read")){
file.setText("processed");
}
}
}

View File

@ -0,0 +1,12 @@
package com.baeldung.dependson.file.reader;
import com.baeldung.dependson.shared.File;
public class FileReader {
public FileReader(File file) {
file.setText("read");
}
public void readFile() {}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.dependson.file.writer;
import com.baeldung.dependson.shared.File;
public class FileWriter {
public FileWriter(File file){
file.setText("write");
}
public void writeFile(){}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.dependson.shared;
import org.springframework.stereotype.Service;
@Service
public class File {
private String text = "";
public String getText() {
return text;
}
public void setText(String text) {
this.text = this.text+text;
}
}

View File

@ -0,0 +1,59 @@
package com.baeldung.dependson.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Lazy;
import com.baeldung.dependson.file.processor.FileProcessor;
import com.baeldung.dependson.file.reader.FileReader;
import com.baeldung.dependson.file.writer.FileWriter;
import com.baeldung.dependson.shared.File;
@Configuration
@ComponentScan("com.baeldung.dependson")
public class TestConfig {
@Autowired
File file;
@Bean("fileProcessor")
@DependsOn({"fileReader","fileWriter"})
@Lazy
public FileProcessor fileProcessor(){
return new FileProcessor(file);
}
@Bean("fileReader")
public FileReader fileReader(){
return new FileReader(file);
}
@Bean("fileWriter")
public FileWriter fileWriter(){
return new FileWriter(file);
}
@Bean("dummyFileProcessor")
@DependsOn({"dummyfileWriter"})
@Lazy
public FileProcessor dummyFileProcessor(){
return new FileProcessor(file);
}
@Bean("dummyFileProcessorCircular")
@DependsOn({"dummyFileReaderCircular"})
@Lazy
public FileProcessor dummyFileProcessorCircular(){
return new FileProcessor(file);
}
@Bean("dummyFileReaderCircular")
@DependsOn({"dummyFileProcessorCircular"})
@Lazy
public FileReader dummyFileReaderCircular(){
return new FileReader(file);
}
}

View File

@ -0,0 +1,42 @@
package com.baeldung.dependson.processor;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.baeldung.dependson.config.TestConfig;
import com.baeldung.dependson.shared.File;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TestConfig.class)
public class FileProcessorTest {
@Autowired
ApplicationContext context;
@Autowired
File file;
@Test
public void whenAllBeansCreated_FileTextEndsWithProcessed() {
context.getBean("fileProcessor");
assertTrue(file.getText().endsWith("processed"));
}
@Test(expected=NoSuchBeanDefinitionException.class)
public void whenDependentBeanNotAvailable_ThrowsNoSuchBeanDefinitionException(){
context.getBean("dummyFileProcessor");
}
@Test(expected=BeanCreationException.class)
public void whenCircularDependency_ThrowsBeanCreationException(){
context.getBean("dummyFileReaderCircular");
}
}