From 506962bc96309ad49a6ad1f38dd5bb91f73e91e7 Mon Sep 17 00:00:00 2001 From: fanatixan Date: Sat, 12 May 2018 22:43:12 +0200 Subject: [PATCH] Spring annotations article (#4232) * Spring annotations * commented VehicleFactoryApplication to fix CI build --- .../java/com/baeldung/annotations/Bike.java | 20 ++++++ .../java/com/baeldung/annotations/Biker.java | 24 +++++++ .../java/com/baeldung/annotations/Car.java | 30 +++++++++ .../com/baeldung/annotations/CarMechanic.java | 10 +++ .../com/baeldung/annotations/CarUtility.java | 7 ++ .../java/com/baeldung/annotations/Driver.java | 32 +++++++++ .../java/com/baeldung/annotations/Engine.java | 26 ++++++++ .../com/baeldung/annotations/Vehicle.java | 4 ++ .../annotations/VehicleController.java | 66 +++++++++++++++++++ .../VehicleFactoryApplication.java | 13 ++++ .../annotations/VehicleFactoryConfig.java | 30 +++++++++ .../annotations/VehicleRepository.java | 8 +++ .../annotations/VehicleRestController.java | 8 +++ .../baeldung/annotations/VehicleService.java | 13 ++++ .../src/main/resources/annotations.properties | 1 + .../src/main/resources/annotations.xml | 9 +++ 16 files changed, 301 insertions(+) create mode 100644 spring-mvc-java/src/main/java/com/baeldung/annotations/Bike.java create mode 100644 spring-mvc-java/src/main/java/com/baeldung/annotations/Biker.java create mode 100644 spring-mvc-java/src/main/java/com/baeldung/annotations/Car.java create mode 100644 spring-mvc-java/src/main/java/com/baeldung/annotations/CarMechanic.java create mode 100644 spring-mvc-java/src/main/java/com/baeldung/annotations/CarUtility.java create mode 100644 spring-mvc-java/src/main/java/com/baeldung/annotations/Driver.java create mode 100644 spring-mvc-java/src/main/java/com/baeldung/annotations/Engine.java create mode 100644 spring-mvc-java/src/main/java/com/baeldung/annotations/Vehicle.java create mode 100644 spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleController.java create mode 100644 spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleFactoryApplication.java create mode 100644 spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleFactoryConfig.java create mode 100644 spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleRepository.java create mode 100644 spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleRestController.java create mode 100644 spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleService.java create mode 100644 spring-mvc-java/src/main/resources/annotations.properties create mode 100644 spring-mvc-java/src/main/resources/annotations.xml diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/Bike.java b/spring-mvc-java/src/main/java/com/baeldung/annotations/Bike.java new file mode 100644 index 0000000000..2a893e7903 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/annotations/Bike.java @@ -0,0 +1,20 @@ +package com.baeldung.annotations; + +import org.springframework.beans.factory.annotation.Required; +import org.springframework.context.annotation.DependsOn; + +@DependsOn +public class Bike implements Vehicle { + + private String color; + + @Required + public void setColor(String color) { + this.color = color; + } + + public String getColor() { + return color; + } + +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/Biker.java b/spring-mvc-java/src/main/java/com/baeldung/annotations/Biker.java new file mode 100644 index 0000000000..dcbe534a90 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/annotations/Biker.java @@ -0,0 +1,24 @@ +package com.baeldung.annotations; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Component; + +@Component +public class Biker { + + @Autowired + @Qualifier("bike") + private Vehicle vehicle; + + @Autowired + public Biker(@Qualifier("bike") Vehicle vehicle) { + this.vehicle = vehicle; + } + + @Autowired + public void setVehicle(@Qualifier("bike") Vehicle vehicle) { + this.vehicle = vehicle; + } + +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/Car.java b/spring-mvc-java/src/main/java/com/baeldung/annotations/Car.java new file mode 100644 index 0000000000..cb252b60c9 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/annotations/Car.java @@ -0,0 +1,30 @@ +package com.baeldung.annotations; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.DependsOn; +import org.springframework.context.annotation.Primary; +import org.springframework.stereotype.Component; + +@Component +@Primary +@DependsOn("engine") +public class Car implements Vehicle { + + @Autowired + private Engine engine; + + @Autowired + public Car(Engine engine) { + this.engine = engine; + } + + @Autowired + public void setEngine(Engine engine) { + this.engine = engine; + } + + public Engine getEngine() { + return engine; + } + +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/CarMechanic.java b/spring-mvc-java/src/main/java/com/baeldung/annotations/CarMechanic.java new file mode 100644 index 0000000000..fc933dcd61 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/annotations/CarMechanic.java @@ -0,0 +1,10 @@ +package com.baeldung.annotations; + +import org.springframework.context.annotation.Lazy; +import org.springframework.stereotype.Component; + +@Component +@Lazy +public class CarMechanic { + +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/CarUtility.java b/spring-mvc-java/src/main/java/com/baeldung/annotations/CarUtility.java new file mode 100644 index 0000000000..e66ab02b01 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/annotations/CarUtility.java @@ -0,0 +1,7 @@ +package com.baeldung.annotations; + +import org.springframework.stereotype.Component; + +@Component +public class CarUtility { +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/Driver.java b/spring-mvc-java/src/main/java/com/baeldung/annotations/Driver.java new file mode 100644 index 0000000000..5a2d86e99e --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/annotations/Driver.java @@ -0,0 +1,32 @@ +package com.baeldung.annotations; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +@Component +public class Driver { + + @Autowired + private Vehicle vehicle; + + @Autowired + public Driver(Vehicle vehicle) { + this.vehicle = vehicle; + } + + @Autowired + public void setVehicle(Vehicle vehicle) { + this.vehicle = vehicle; + } + + public Vehicle getVehicle() { + return vehicle; + } + + @Scheduled(fixedRate = 10000) + @Scheduled(cron = "0 * * * * MON-FRI") + public void checkVehicle() { + } + +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/Engine.java b/spring-mvc-java/src/main/java/com/baeldung/annotations/Engine.java new file mode 100644 index 0000000000..f8c0154639 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/annotations/Engine.java @@ -0,0 +1,26 @@ +package com.baeldung.annotations; + +import org.springframework.beans.factory.annotation.Value; + +public class Engine { + + @Value("8") + private int cylinderCount; + + @Value("${engine.fuelType}") + private String fuelType; + + public Engine() { + this(8); + } + + public Engine(@Value("8") int cylinderCount) { + this.cylinderCount = cylinderCount; + } + + @Value("8") + public void setCylinderCount(int cylinderCount) { + this.cylinderCount = cylinderCount; + } + +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/Vehicle.java b/spring-mvc-java/src/main/java/com/baeldung/annotations/Vehicle.java new file mode 100644 index 0000000000..7176c3cc8a --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/annotations/Vehicle.java @@ -0,0 +1,4 @@ +package com.baeldung.annotations; + +public interface Vehicle { +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleController.java b/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleController.java new file mode 100644 index 0000000000..a979dff708 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleController.java @@ -0,0 +1,66 @@ +package com.baeldung.annotations; + +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.ResponseStatus; + +@Controller +@RequestMapping(value = "/vehicles", method = RequestMethod.GET) +public class VehicleController { + + @CrossOrigin + @ResponseBody + @RequestMapping("/hello") + public String hello() { + return "Hello World!"; + } + + @RequestMapping("/home") + public String home() { + return "home"; + } + + @PostMapping("/save") + public void saveVehicle(@RequestBody Vehicle vehicle) { + } + + @RequestMapping("/{id}") + public Vehicle getVehicle(@PathVariable("id") long id) { + return null; + } + + @RequestMapping + public Vehicle getVehicleByParam(@RequestParam("id") long id) { + return null; + } + + @RequestMapping("/buy") + public Car buyCar(@RequestParam(defaultValue = "5") int seatCount) { + return null; + } + + @ExceptionHandler(IllegalArgumentException.class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + public void onIllegalArgumentException(IllegalArgumentException exception) { + } + + @PostMapping("/assemble") + public void assembleVehicle(@ModelAttribute("vehicle") Vehicle vehicle) { + } + + @ModelAttribute("vehicle") + public Vehicle getVehicle() { + return null; + } + +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleFactoryApplication.java b/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleFactoryApplication.java new file mode 100644 index 0000000000..ab90d8cef2 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleFactoryApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.annotations; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +// @SpringBootApplication +public class VehicleFactoryApplication { + +// public static void main(String[] args) { +// SpringApplication.run(VehicleFactoryApplication.class, args); +// } + +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleFactoryConfig.java b/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleFactoryConfig.java new file mode 100644 index 0000000000..ad43a19b66 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleFactoryConfig.java @@ -0,0 +1,30 @@ +package com.baeldung.annotations; + +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.ImportResource; +import org.springframework.context.annotation.Lazy; +import org.springframework.context.annotation.PropertySource; +import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.scheduling.annotation.EnableScheduling; + +@Configuration +@ComponentScan(basePackages = "com.baeldung.annotations") +@ComponentScan(basePackageClasses = VehicleFactoryConfig.class) +@ImportResource("classpath:/annotations.xml") +@PropertySource("classpath:/annotations.properties") +@Lazy +@EnableAutoConfiguration +@EnableAsync +@EnableScheduling +public class VehicleFactoryConfig { + + @Bean + @Lazy(false) + public Engine engine() { + return new Engine(); + } + +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleRepository.java b/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleRepository.java new file mode 100644 index 0000000000..4dfd2dd771 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleRepository.java @@ -0,0 +1,8 @@ +package com.baeldung.annotations; + +import org.springframework.stereotype.Repository; + +@Repository +public class VehicleRepository { + +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleRestController.java b/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleRestController.java new file mode 100644 index 0000000000..9dd98e688f --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleRestController.java @@ -0,0 +1,8 @@ +package com.baeldung.annotations; + +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class VehicleRestController { + +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleService.java b/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleService.java new file mode 100644 index 0000000000..19e5a3b4a4 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleService.java @@ -0,0 +1,13 @@ +package com.baeldung.annotations; + +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Service; + +@Service +public class VehicleService { + + @Async + public void repairCar() { + } + +} diff --git a/spring-mvc-java/src/main/resources/annotations.properties b/spring-mvc-java/src/main/resources/annotations.properties new file mode 100644 index 0000000000..ac2f50b39b --- /dev/null +++ b/spring-mvc-java/src/main/resources/annotations.properties @@ -0,0 +1 @@ +engine.fuelType=petrol \ No newline at end of file diff --git a/spring-mvc-java/src/main/resources/annotations.xml b/spring-mvc-java/src/main/resources/annotations.xml new file mode 100644 index 0000000000..b477b68ef6 --- /dev/null +++ b/spring-mvc-java/src/main/resources/annotations.xml @@ -0,0 +1,9 @@ + + + + + + + +