พัฒนา web application ด้วย Java

spring annotation ทั้งหมด

  1. 1. Core Application Annotations:
    • @SpringBootApplicationMarks the main class of a Spring Boot application, combining @Configuration@EnableAutoConfiguration, and @ComponentScan.
    Java
        @SpringBootApplication    public class MyApplication {        public static void main(String[] args) {            SpringApplication.run(MyApplication.class, args);        }    }
    2. Web Layer (REST) Annotations:
    • @RestControllerCombines @Controller and @ResponseBody, indicating a class handles RESTful requests and its methods return data directly as the response body.
    • @RequestMappingMaps HTTP requests to handler methods. Can be used at class or method level.
    • @GetMapping@PostMapping@PutMapping@DeleteMappingSpecific HTTP method mapping annotations for cleaner code.
    • @PathVariableBinds a method parameter to a URI template variable.
    • @RequestParamBinds a method parameter to a web request parameter.
    • @RequestBodyBinds the HTTP request body to a method parameter.
    • @ResponseBodyIndicates that the return value of a method should be bound to the web response body.
    Java
        @RestController    @RequestMapping("/api/products")    public class ProductController {        @GetMapping("/{id}")        public Product getProductById(@PathVariable Long id) {            // ...            return product;        }        @PostMapping        public Product createProduct(@RequestBody Product product) {            // ...            return savedProduct;        }    }
    3. Dependency Injection and Configuration:
    • @AutowiredAutomatically injects dependencies by type.
    • @QualifierSpecifies which bean to inject when multiple beans of the same type exist.
    • @ValueInjects values from properties files or environment variables.
    • @Component@Service@RepositoryStereotype annotations for marking classes as Spring components, services, or data access objects, respectively, enabling component scanning.
    • @ConfigurationIndicates a class provides bean definitions.
    • @BeanMarks a method as a bean factory method within a @Configuration class.
    Java
        @Service    public class ProductService {        @Autowired        private ProductRepository productRepository;        @Value("${app.name}")        private String appName;        // ...    }    @Configuration    public class AppConfig {        @Bean        public MyBean myBean() {            return new MyBean();        }    }
    4. Data Access and Transactions:
    • @EntityMarks a class as a JPA entity.
    • @Table@Column@IdJPA annotations for mapping entities to database tables and columns.
    • @TransactionalManages transactions for methods or classes.
    Java
        @Entity    @Table(name = "products")    public class Product {        @Id        private Long id;        private String name;        // ...    }    @Service    public class OrderService {        @Transactional        public void placeOrder(Order order) {            // ...        }    }
    5. Conditional Annotations:
    • @ConditionalOnPropertyCreates a bean only if a specific property is present and/or has a particular value.
    • @ConditionalOnClassCreates a bean only if a specific class is on the classpath.
    Java
        @Configuration    @ConditionalOnProperty(name = "feature.enabled", havingValue = "true")    public class FeatureConfig {        // ...    }
    6. Testing Annotations:
    • @SpringBootTestLoads the entire application context for integration tests.
    • @WebMvcTestFocuses testing on the web layer, mocking other layers.
    • @DataJpaTestFocuses testing on JPA components.
    • @MockBeanAdds mocks to the Spring application context.
    Java
        @SpringBootTest    public class MyIntegrationTest {        // ...    }
spring annotation ทั้งหมด spring annotation ทั้งหมด Reviewed by Mr.Boonchai on พฤศจิกายน 17, 2568 Rating: 5

ไม่มีความคิดเห็น:

Thank you

ขับเคลื่อนโดย Blogger.