Spring annotations article (#4232)
* Spring annotations * commented VehicleFactoryApplication to fix CI build
This commit is contained in:
parent
5c4fe46ea2
commit
506962bc96
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.annotations;
|
||||
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@Lazy
|
||||
public class CarMechanic {
|
||||
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package com.baeldung.annotations;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class CarUtility {
|
||||
}
|
|
@ -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() {
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
package com.baeldung.annotations;
|
||||
|
||||
public interface Vehicle {
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
// }
|
||||
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.annotations;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public class VehicleRepository {
|
||||
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.annotations;
|
||||
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class VehicleRestController {
|
||||
|
||||
}
|
|
@ -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() {
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
engine.fuelType=petrol
|
|
@ -0,0 +1,9 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" 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.xsd">
|
||||
|
||||
<bean class="com.baeldung.annotations.Bike" name="bike">
|
||||
<property name="color" value="green" />
|
||||
</bean>
|
||||
|
||||
</beans>
|
Loading…
Reference in New Issue