免费开发网站,案例学习网站建设方案摸摸学校,营销型网站建设范文,优化方案范文1. SpringBootApplication
1.1 概述
SpringBootApplication是SpringBoot应用程序的核心注解#xff0c;通常用于主类上。它包含了以下三个注解#xff1a;
Configuration#xff1a;表示该类是一个配置类#xff0c;用于定义Spring的配置信息。EnableAutoConfigurationSpringBootApplication
1.1 概述
SpringBootApplication是SpringBoot应用程序的核心注解通常用于主类上。它包含了以下三个注解
Configuration表示该类是一个配置类用于定义Spring的配置信息。EnableAutoConfiguration表示启用自动配置SpringBoot会根据项目中的依赖自动配置相应的组件。ComponentScan表示启用组件扫描SpringBoot会自动扫描当前包及其子包下的所有组件。
1.2 使用方法
在主类上添加SpringBootApplication注解然后在main方法中调用SpringApplication.run()方法启动应用程序。
SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}1.3 注意事项
主类应放在根包名下以便能够扫描到所有的组件否则会报错。可以使用exclude用于排除自动配置的类。
2. RestController
2.1 概述
RestController是一个组合注解用于定义RESTful风格的Web服务。它包含了以下两个注解
Controller表示该类是一个控制器类用于处理HTTP请求。ResponseBody表示将方法返回值作为HTTP响应体而不是视图名称。
2.2 使用方法
在控制器类上添加RestController注解然后在方法上添加相应的HTTP请求映射注解例如GetMapping、PostMapping等。
RestController
public class HelloController {GetMapping(/hello)public String hello() {return Hello!;}
}2.3 注意事项
如果需要返回视图名称可以使用Controller注解替换RestController。如果需要在方法上单独使用ResponseBody可以将RestController替换为Controller。
3. Autowired
3.1 概述
Autowired用于实现依赖注入。它可以自动装配Bean默认按类型装配无需手动创建和管理对象。
3.2 使用方法
在需要注入的字段、构造方法或者Setter方法上添加Autowired注解。
RestController
public class UserController {Autowiredprivate UserService userService;GetMapping(/users)public ListUser getUsers() {return userService.getUsers();}
}3.3 注意事项
如果有多个实现类可以使用Qualifier注解指定Bean的名称。与之类似的还有Resource也可以实现依赖注入只是注入的方式不同根据name属性注入
4. Component
4.1 概述
Component用于定义组件。它表示该类是一个Spring管理的Bean可以被自动扫描和装配。
4.2 使用方法
在类上添加Component注解然后在需要注入的地方使用Autowired注解。
// 定义组件
Component
public class UserService {public ListUser getUsers() {// ...}
}// 使用组件
RestController
public class UserController {Autowiredprivate UserService userService;GetMapping(/users)public ListUser getUsers() {return userService.getUsers();}
}
4.3 注意事项
Component是一个通用注解还有一些特定场景的注解例如Repository、Service、Controller等但都是依赖于Component。如果需要自定义Bean的名称可以在Component注解中添加value属性。
5. Configuration
5.1 概述
Configuration是Spring的核心注解之一用于定义配置类。它表示该类是一个Java配置类可以用来替代XML配置文件。
5.2 使用方法
在类上添加Configuration注解然后在方法上添加Bean注解定义Bean。
Configuration
public class AppConfig {Beanpublic UserService userService() {return new UserService();}
}5.3 注意事项
配置类通常与ComponentScan、EnableAutoConfiguration等注解一起使用。如果需要导入其他配置类可以使用Import注解。
6. Bean
6.1 概述
Bean是Spring的核心注解之一用于定义Bean。它表示该方法返回一个Bean可以被Spring容器管理。
6.2 使用方法
在配置类的方法上添加Bean注解然后在需要注入的地方使用Autowired注解。
Configuration
public class AppConfig {Beanpublic UserService userService() {return new UserService();}
}
12345676.3 注意事项
如果需要自定义Bean的名称可以在Bean注解中添加name属性。如果需要指定Bean的初始化和销毁方法可以使用initMethod和destroyMethod属性。
7. RequestMapping
7.1 概述
RequestMapping用于定义HTTP请求映射。它可以将HTTP请求映射到控制器类或方法上。
7.2 使用方法
在控制器类或方法上添加RequestMapping注解然后设置相应的属性例如value、method、produces等。
RestController
RequestMapping(/users)
public class UserController {GetMapping(/{id})public User getUser(PathVariable(id) Long id) {// ...}
}7.3 注意事项
RequestMapping是一个通用注解还有一些特定HTTP方法的注解例如GetMapping、PostMapping、PutMapping、DeleteMapping等。如果需要处理多个URL可以在value属性中使用数组。
8. PathVariable
8.1 概述
PathVariable用于获取URL路径中的变量。它可以将URL路径中的变量绑定到方法参数上。
8.2 使用方法
在方法参数上添加PathVariable注解然后设置相应的属性例如value、required等。
RestController
RequestMapping(/users)
public class UserController {GetMapping(/info/{id}/{name})public User getUser(PathVariable(id) Long id, PathVariable(name) String userName) {// ...}
}8.3 注意事项
如果方法参数名称与URL路径中的变量名称相同可以省略value属性。如果允许路径变量不存在可以将required属性设置为false。
9. RequestParam
9.1 概述
RequestParam是Spring MVC的核心注解之一用于获取HTTP请求参数。它可以将HTTP请求参数绑定到方法参数上。
9.2 使用方法
在方法参数上添加RequestParam注解然后设置相应的属性例如value、required、defaultValue等。
RestController
RequestMapping(/users)
public class UserController {GetMapping(/search)public ListUser searchUsers(RequestParam(keyword) String keyword) {// ...}
}9.3 注意事项
如果方法参数名称与HTTP请求参数名称相同可以省略value属性。如果允许请求参数不存在可以将required属性设置为false。
10. Value
10.1 概述
Value用于获取配置文件中的属性值。它可以将配置文件中的属性值绑定到字段或方法参数上。
10.2 使用方法
在字段或方法参数上添加Value注解然后设置相应的属性例如${property.name}。
Component
public class AppConfig {Value(${app.name})private String appName;public String getAppName() {return appName;}
}10.3 注意事项
如果需要使用默认值可以在Value注解中使用:分隔符例如${property.name:default}。如果需要使用占位符可以在Value注解中使用#{}例如#{Hello, property.name}。
11. ConfigurationProperties 11.1 概述
ConfigurationProperties该注解可以直接注入整个类的数据作用于类
11.2 使用方式
配置文件application.yml中添加配置
#模拟的类
student:name: 张三age: 12定义配置类StudentInfoprefixstudent的student对应配置文件的student
Component
ConfigurationProperties(prefixstudent) // 对应配置文件的student
public class StudentInfo {String name; // 对应配置文件student下的nameString age; // 对应配置文件student下的agepublic String getName() {return name;}public void setName(String name) {this.name name;} public String getAge() { return age; }public void setAge(String age) { this.age age; }public void print() {System.out.println(name name age: age);}
}通过Autowired使用
Autowired
private StudentInfo student
/**student的使用*
Test
void StudentTest()student.print();11.3 注意事项
类的字段名必须和配置文件的字段名一致必须要有get和set方法才能注入成功
我是Tz 想把我知道的分享给你