Spring annotation memorandum-There is also lombok-

title

Spring annotation memorandum

Overview

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 ~~.

Purpose

table of contents

Classify in places that are likely to be used frequently (* Used in a specific class, not available)

  1. Controller  1. @Controller  2. @RequestMapping  3. @ResponseBody  4. @PostMapping  5. @GetMapping  6. @PathVariable  7. @RequestParam  8. @Autowired  9. @Value

  2. Service  1. @Service  2. @Transactional

  3. Repository  1. @Repository

  4. Component  1. @Component  2. @Data

  5. Controller Control screen transition (request mapping) and call Service.

1-1. @Controller

When do you use it?

Various explanations

Example sentence

ExampleController.java


@Controller
public class ExampleController {
	//Omission
}

reference


1-2. @RequestMapping(value="URL")

When do you use it?

Various explanations

Attribute that can be specified

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)

Example sentence

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
	}
}

reference


1-3. @ResponseBody

When do you use it?

When you want to return a view other than the view corresponding to the template path with @RequestMapping. (Request mapping without page transition, etc.)

Various explanations

Example sentence

ExampleController.java


@Controller
@RequestMapping(value="/example")
public class ExampleController {

	@RequestMapping("/sample")
	@ResponseBody
	public String sampleMethod() {
		return "sample"; //"sample"Returns the string literal.
	}
}

reference


1-4.@PostMapping

When do you use it?

Various explanations

Example sentence

ExampleController.java


@Controller
@RequestMapping("/example")
public class ExampleController {

	@PostMapping("top")
	public String top() {
		//Omission
	}
}

reference



1-5. @GetMapping

When do you use it?

Various explanations

Example sentence

ExampleController.java


@Controller
@RequestMapping("/example")
public class ExampleController {

	@GetMapping("top")
	public String top() {
		//Omission
	}
}

reference


1-6. @PathVariable

When do you use it?

Various explanations

Example sentence

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
	}	
}

reference


1-7. @RequestParam

When do you use it?

When you want to get the parameter value given to the URL.

Various explanations

Example sentence

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
	}
}

reference


1-8.@Autowired

When do you use it?

Used when you want to set an instance for a variable. (It seems that it is not recommended ... Search by constructor injection for details)

Various explanations

@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.)

Example sentence

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"; 
	}
}

reference


1-9. @Value

When do you use it?

Various explanations

Example sentence

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;
}

reference


2.Service Providing business processing (Business Logic).

2-1. @Service

When do you use it?

Various explanations

Example sentence

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

When do you use it?

Various explanations

Conditions for @Transactional to work ・ Must be a public method of the DI class -Called directly by another class or framework that is DI

Example sentence


//thinking

reference


Repository Provides classes for the data access layer.

3-1. @Repository

When do you use it?

Various explanations

Example sentence

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
	}
}

reference


Component

4-1. @Component

When do you use it?

Various explanations

Example sentence

Account.java


@Component
public class Account {
	private String id;
	private String name;
	
	//getter,setter abbreviation
}

reference


4-2. @Data

When do you use it?

When you want to reduce the amount of code in the Bean class.

Various explanations

Example sentence

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.
}

reference


Recommended Posts

Spring annotation memorandum-There is also lombok-
What is Spring Tools 4
What is an annotation?
Spring Boot for annotation learning
Annotation instance is Dynamic Proxy