Skip to content

Commit 506962b

Browse files
fanatixanmaibin
authored andcommitted
Spring annotations article (eugenp#4232)
* Spring annotations * commented VehicleFactoryApplication to fix CI build
1 parent 5c4fe46 commit 506962b

16 files changed

Lines changed: 301 additions & 0 deletions
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.annotations;
2+
3+
import org.springframework.beans.factory.annotation.Required;
4+
import org.springframework.context.annotation.DependsOn;
5+
6+
@DependsOn
7+
public class Bike implements Vehicle {
8+
9+
private String color;
10+
11+
@Required
12+
public void setColor(String color) {
13+
this.color = color;
14+
}
15+
16+
public String getColor() {
17+
return color;
18+
}
19+
20+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.annotations;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.beans.factory.annotation.Qualifier;
5+
import org.springframework.stereotype.Component;
6+
7+
@Component
8+
public class Biker {
9+
10+
@Autowired
11+
@Qualifier("bike")
12+
private Vehicle vehicle;
13+
14+
@Autowired
15+
public Biker(@Qualifier("bike") Vehicle vehicle) {
16+
this.vehicle = vehicle;
17+
}
18+
19+
@Autowired
20+
public void setVehicle(@Qualifier("bike") Vehicle vehicle) {
21+
this.vehicle = vehicle;
22+
}
23+
24+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.annotations;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.context.annotation.DependsOn;
5+
import org.springframework.context.annotation.Primary;
6+
import org.springframework.stereotype.Component;
7+
8+
@Component
9+
@Primary
10+
@DependsOn("engine")
11+
public class Car implements Vehicle {
12+
13+
@Autowired
14+
private Engine engine;
15+
16+
@Autowired
17+
public Car(Engine engine) {
18+
this.engine = engine;
19+
}
20+
21+
@Autowired
22+
public void setEngine(Engine engine) {
23+
this.engine = engine;
24+
}
25+
26+
public Engine getEngine() {
27+
return engine;
28+
}
29+
30+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.annotations;
2+
3+
import org.springframework.context.annotation.Lazy;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component
7+
@Lazy
8+
public class CarMechanic {
9+
10+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.annotations;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
@Component
6+
public class CarUtility {
7+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.annotations;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.scheduling.annotation.Scheduled;
5+
import org.springframework.stereotype.Component;
6+
7+
@Component
8+
public class Driver {
9+
10+
@Autowired
11+
private Vehicle vehicle;
12+
13+
@Autowired
14+
public Driver(Vehicle vehicle) {
15+
this.vehicle = vehicle;
16+
}
17+
18+
@Autowired
19+
public void setVehicle(Vehicle vehicle) {
20+
this.vehicle = vehicle;
21+
}
22+
23+
public Vehicle getVehicle() {
24+
return vehicle;
25+
}
26+
27+
@Scheduled(fixedRate = 10000)
28+
@Scheduled(cron = "0 * * * * MON-FRI")
29+
public void checkVehicle() {
30+
}
31+
32+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.annotations;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
5+
public class Engine {
6+
7+
@Value("8")
8+
private int cylinderCount;
9+
10+
@Value("${engine.fuelType}")
11+
private String fuelType;
12+
13+
public Engine() {
14+
this(8);
15+
}
16+
17+
public Engine(@Value("8") int cylinderCount) {
18+
this.cylinderCount = cylinderCount;
19+
}
20+
21+
@Value("8")
22+
public void setCylinderCount(int cylinderCount) {
23+
this.cylinderCount = cylinderCount;
24+
}
25+
26+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.baeldung.annotations;
2+
3+
public interface Vehicle {
4+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.baeldung.annotations;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.stereotype.Controller;
5+
import org.springframework.web.bind.annotation.CrossOrigin;
6+
import org.springframework.web.bind.annotation.ExceptionHandler;
7+
import org.springframework.web.bind.annotation.ModelAttribute;
8+
import org.springframework.web.bind.annotation.PathVariable;
9+
import org.springframework.web.bind.annotation.PostMapping;
10+
import org.springframework.web.bind.annotation.RequestBody;
11+
import org.springframework.web.bind.annotation.RequestMapping;
12+
import org.springframework.web.bind.annotation.RequestMethod;
13+
import org.springframework.web.bind.annotation.RequestParam;
14+
import org.springframework.web.bind.annotation.ResponseBody;
15+
import org.springframework.web.bind.annotation.ResponseStatus;
16+
17+
@Controller
18+
@RequestMapping(value = "/vehicles", method = RequestMethod.GET)
19+
public class VehicleController {
20+
21+
@CrossOrigin
22+
@ResponseBody
23+
@RequestMapping("/hello")
24+
public String hello() {
25+
return "Hello World!";
26+
}
27+
28+
@RequestMapping("/home")
29+
public String home() {
30+
return "home";
31+
}
32+
33+
@PostMapping("/save")
34+
public void saveVehicle(@RequestBody Vehicle vehicle) {
35+
}
36+
37+
@RequestMapping("/{id}")
38+
public Vehicle getVehicle(@PathVariable("id") long id) {
39+
return null;
40+
}
41+
42+
@RequestMapping
43+
public Vehicle getVehicleByParam(@RequestParam("id") long id) {
44+
return null;
45+
}
46+
47+
@RequestMapping("/buy")
48+
public Car buyCar(@RequestParam(defaultValue = "5") int seatCount) {
49+
return null;
50+
}
51+
52+
@ExceptionHandler(IllegalArgumentException.class)
53+
@ResponseStatus(HttpStatus.BAD_REQUEST)
54+
public void onIllegalArgumentException(IllegalArgumentException exception) {
55+
}
56+
57+
@PostMapping("/assemble")
58+
public void assembleVehicle(@ModelAttribute("vehicle") Vehicle vehicle) {
59+
}
60+
61+
@ModelAttribute("vehicle")
62+
public Vehicle getVehicle() {
63+
return null;
64+
}
65+
66+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.annotations;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
// @SpringBootApplication
7+
public class VehicleFactoryApplication {
8+
9+
// public static void main(String[] args) {
10+
// SpringApplication.run(VehicleFactoryApplication.class, args);
11+
// }
12+
13+
}

0 commit comments

Comments
 (0)