Spring annotation memorandum
Since I am learning, I think that there are many misunderstandings and lack of explanation, so I would appreciate it if anyone noticed it. Also, if you know something that is not mentioned and is useful to remember, please comment ~~ crying ~~.
Classify in places that are likely to be used frequently (* Used in a specific class, not available)
Controller
1. @Controller
2. @RequestMapping
3. @ResponseBody
4. @PostMapping
5. @GetMapping
6. @PathVariable
7. @RequestParam
8. @Autowired
9. @Value
Service
1. @Service
2. @Transactional
Repository
1. @Repository
Component
1. @Component
2. @Data
Controller Control screen transition (request mapping) and call Service.
1-1. @Controller
ExampleController.java
@Controller
public class ExampleController {
//Omission
}
1-2. @RequestMapping(value="URL")
@ResponseBody
if you want to return something other than a view]@RequestMapping
(value="URL") ⇒ @RequestMapping
("URL")]@RequestMapping
(value =" URL "method = RequestMethod.POST) @RequestMapping
(value =" URL "method = {RequestMethod.POST, RequestMethod.GET})You can specify the HTTP header.
It seems. See [Reference: Mapping method between Controller processing method and request (URL etc.)]
You can specify request parameters. It seems. (See reference for details)
ExampleController.java
@Controller
@RequestMapping(value="/example")
public class ExampleController {
@RequestMapping("sample") //Http to URL://xxxx:xxxx/example/Called when sample is executed.(Relative path)
public String sampleMethod() {
return "example/sample"; //If the directory structure is the default, src/templates/example/sample.Returns html
}
}
1-3. @ResponseBody
When you want to return a view other than the view corresponding to the template path with @RequestMapping
. (Request mapping without page transition, etc.)
@RequestMapping
is set, give it to the method that you want to return other than the view.ExampleController.java
@Controller
@RequestMapping(value="/example")
public class ExampleController {
@RequestMapping("/sample")
@ResponseBody
public String sampleMethod() {
return "sample"; //"sample"Returns the string literal.
}
}
1-4.@PostMapping
@RequestMapping
(value =" URL ", method = RequestMethod.POST).ExampleController.java
@Controller
@RequestMapping("/example")
public class ExampleController {
@PostMapping("top")
public String top() {
//Omission
}
}
1-5. @GetMapping
@RequestMapping
(value =" URL ", method = RequestMethod.GET).ExampleController.java
@Controller
@RequestMapping("/example")
public class ExampleController {
@GetMapping("top")
public String top() {
//Omission
}
}
1-6. @PathVariable
@RequestParam
, but built directly into the request URL.ExampleController.java
@Controller
@RequestMapping("/example")
public class ExampleController {
//http://xxxx:xxxx/example/myPage/Called when a request like 12345 comes in
//PostMapping value and userId are the keys, and the URL value is the value.
@PostMapping("myPage/{userId}")
public String myPage(
@PathVariable("userId") int argUserId
) {
//Omission
}
}
1-7. @RequestParam
When you want to get the parameter value given to the URL.
@PathVariable
, but with an additional addition to the request URL.ExampleController.java
@Controller
@RequestMapping("/example")
public class ExampleController {
//http://xxxx:xxxx/example/account?userId=Called when a request like 12345 comes in
//userId=12345's userId is the key and 12345 is the value
@PostMapping("account")
public String top(
@RequestParam("userId") int argUserId
) {
//Omission
}
}
1-8.@Autowired
Used when you want to set an instance for a variable. (It seems that it is not recommended ... Search by constructor injection for details)
@Autowired is an annotation attached to an instance variable in order to set an appropriate object under Spring management to a variable. Reference: [Spring] Quoted from the basics of DI using @Autowired and @Component
I found this explanation to be the easiest to understand during my research. (* Objects under Spring management are recognized as components.)
CalcService.java
@Service
public interface CalcService {
public int add(int x, int y);
}
CalcServiceImpl.java
@Service
public class CalcServiceImpl implements CalcService {
public int add(int x, int y) {
return x + y;
}
}
ExampleController.java
@Controller
@RequestMapping("/example")
public class ExampleController {
@Autowired
CalcService calcService;
@RequestMapping("calc")
public String printTotal() {
int x = 5;
int y = 20;
calcService.add(x, y);
//(Omitted)
return "example/result";
}
}
1-9. @Value
Some kind of property file
sample.sampleId=12345
sample.samplePass=password
Sample.java
@Component
public class Sample {
@Value("${sample.sampleId}")
private int id;
@Value("${sample.samplePass}")
private String password;
}
2.Service Providing business processing (Business Logic).
2-1. @Service
SampleService.java
@Service
public interface SampleService {
public void print();
}
SampleServiceImpl.java
@Service
public class SampleServiceImpl implements SampleService {
public void print() {
System.out.print("Hello.");
}
}
2-2. @Transactional
@Transactional
(rollbackFor = Exception.class), rollback is performed when an exception from Exception to its child class occurs.
Below, quoted from referenceConditions for @Transactional to work ・ Must be a public method of the DI class -Called directly by another class or framework that is DI
//thinking
Repository Provides classes for the data access layer.
3-1. @Repository
SampleRepository.java
@Repository
public interface SampleRepository {
public List<String> search(String keyword);
}
SampleRepositoryImpl.java
@Repository
public class SampleRepositoryImpl implements SampleRepository {
public List<String> search(String keyword) {
//DB operation processing
}
}
Component
4-1. @Component
@ Controller
, @ Service
, @ Repository
. (Utility class, etc.)Account.java
@Component
public class Account {
private String id;
private String name;
//getter,setter abbreviation
}
4-2. @Data
When you want to reduce the amount of code in the Bean class.
Account.java
@Component
@Data
public class Account {
private String accountId;
private String password;
private String firstName;
private String lastName;
private int age;
//getter,There is no description of setter, but it can be used because it is implemented internally.
}