BAEL-5336 Move code for the article to spring-di-3 (#11807)
* JAVA-7535 Moved ebook specific code snippets to maven-simple module * COMAUTO-9485 Fixed DB url in spring-project and added missing maven wrapper * Code for the Spring @Autowired Field Null – Common Causes and Solutions article * JAVA-7535 Added code for maven-multi-module * JAVA-7535 Pom formatting * JAVA-7535 Pom formatting * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Create README.md * Create README.md * JAVA-9825: Upgrade log4j version for modules that use log4j-core library directly * BAEL-5188 Add CSRF cookie protection + test (#11775) * BAEL-5188 Add CSRF cookie protection + test * BAEL-5188 Break long lines of code * BAEL-5188 Remove line-breaks before semi-colons * BAEL-5378 Rename exceptions to avoid confusion (#11801) * BAEL-5336 Move code for the article to spring-di-3 Co-authored-by: Dhawal Kapil <dhawal.kapil@africa.airtel.com> Co-authored-by: Dhawal Kapil <dhawalkapil@gmail.com> Co-authored-by: johnA1331 <53036378+johnA1331@users.noreply.github.com> Co-authored-by: sampadawagde <sampada.aws@gmail.com> Co-authored-by: Benjamin Caure <bcaure@gmail.com> Co-authored-by: ashleyfrieze <ashley@incredible.org.uk>
This commit is contained in:
parent
7f84edf0f8
commit
7a15270fc3
|
@ -0,0 +1,14 @@
|
||||||
|
package com.baeldung.autowiring;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class App {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(App.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
package com.baeldung.autowiring.controller;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
|
||||||
|
import com.baeldung.autowiring.service.MyService;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
public class CorrectController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
MyService myService;
|
||||||
|
|
||||||
|
public String control() {
|
||||||
|
return myService.serve();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
package com.baeldung.autowiring.controller;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
|
||||||
|
import com.baeldung.autowiring.service.MyService;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
public class FlawedController {
|
||||||
|
|
||||||
|
public String control() {
|
||||||
|
MyService userService = new MyService();
|
||||||
|
return userService.serve();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
package com.baeldung.autowiring.service;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class MyComponent {
|
||||||
|
|
||||||
|
public void doWork() {}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,19 @@
|
||||||
|
package com.baeldung.autowiring.service;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The bean corresponding to this class is defined in MyServiceConfiguration
|
||||||
|
* Alternatively, you could choose to decorate this class with @Component or @Service
|
||||||
|
*/
|
||||||
|
public class MyService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
MyComponent myComponent;
|
||||||
|
|
||||||
|
public String serve() {
|
||||||
|
myComponent.doWork();
|
||||||
|
return "success";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
package com.baeldung.autowiring.service;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class MyServiceConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
MyService myService() {
|
||||||
|
return new MyService();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
package com.baeldung.autowiring.controller;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||||
|
|
||||||
|
@ExtendWith(SpringExtension.class)
|
||||||
|
@SpringBootTest
|
||||||
|
public class CorrectControllerIntegrationTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
CorrectController controller;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenControl_ThenRunSuccessfully() {
|
||||||
|
assertDoesNotThrow(() -> controller.control());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,28 @@
|
||||||
|
package com.baeldung.autowiring.controller;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
|
@ExtendWith(SpringExtension.class)
|
||||||
|
@SpringBootTest
|
||||||
|
public class FlawedControllerIntegrationTest {
|
||||||
|
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(FlawedControllerIntegrationTest.class);
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
FlawedController myController;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenControl_ThenThrowNullPointerException() {
|
||||||
|
NullPointerException npe = assertThrows(NullPointerException.class, () -> myController.control());
|
||||||
|
LOGGER.error("Got a NullPointerException", npe);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue