springboard常用的使用方法和注解
1. Spring Boot 的主要特性
- 创建独立的 Spring 应用
- 内嵌 Tomcat、Jetty 或 Undertow,无需部署 WAR 文件
- 提供固定的 starter 依赖,简化构建配置
- 尽可能自动配置 Spring
- 提供生产级别的应用监控
- 与云计算无缝集成
2. Spring Boot 常用注解
Spring Boot 使用了许多注解来简化配置和开发过程。以下是一些最常用的注解:
-
@SpringBootApplication
: 这是一个复合注解,它组合了@Configuration
、@EnableAutoConfiguration
和@ComponentScan
。通常在主类上使用此注解。 -
@RestController
: 这是一个复合注解,它组合了@Controller
和@ResponseBody
。这个注解用于创建 RESTful web services。 -
@RequestMapping
: 用于映射 web 请求到特定的处理器类或处理器方法。 -
@Autowired
: 该注解用于自动装配 bean。 -
@Service
,@Repository
,@Controller
: 这些注解用于表示类层次结构的角色。 -
@ConfigurationProperties
: 该注解用于绑定 property 文件中的属性到 bean。
3. Spring Boot 使用方法
以下是一个简单的 Spring Boot 应用程序的创建步骤:
步骤1: 创建一个新的 Maven 项目并添加 Spring Boot 相关的依赖。可以使用 Spring Initializr 网站来快速创建项目。
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
</dependencies>
步骤2: 创建主类,并使用 @SpringBootApplication
注解。
@SpringBootApplication
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}
}
步骤3: 创建一个控制器类,并使用 @RestController
和 @RequestMapping
注解。
- @Controller注解:@Controller是一个类级别的注解,用于标记控制器组件。控制器通常是处理HTTP请求的地方。
@Controller
public class ExampleController {@Autowiredprivate ExampleService exampleService;@RequestMapping("/example")public String showExample(Model model) {List<Example> examples = exampleService.getAllExamples();model.addAttribute("examples", examples);return "example";}
}
@Service注解:@Service是一个类级别的注解,用于标记服务层组件。服务层组件通常包含业务逻辑
@Service
public class ExampleService {@Autowiredprivate ExampleRepository exampleRepository;public List<Example> getAllExamples() {return exampleRepository.findAll();}
}
- @Repository注解:@Repository是一个类级别的注解,用于标记数据访问组件,即DAO组件。
@Repository
public interface ExampleRepository extends JpaRepository<Example, Long> {
}
- @Autowired注解:@Autowired注解用于自动装配Spring容器中的bean。你可以在字段、构造器或者setter方法上使用@Autowired注解。
@Autowired
private ExampleService exampleService;
- @RequestMapping注解:@RequestMapping是一个方法级别的注解,用于映射HTTP请求到特定的处理方法。你可以指定HTTP方法(GET、POST等),URL模式,甚至特定的请求参数。
@RequestMapping(value = "/example", method = RequestMethod.GET)
public String showExample(Model model) {//...
}
- @Configuration和@Bean注解:@Configuration注解用于标记配置类,而@Bean注解用于标记返回一个被Spring管理的bean的方法。下面的示例展示了如何创建一个DataSource bean。
@Configuration
public class DataSourceConfig {@Beanpublic DataSource dataSource() {DriverManagerDataSource dataSource = new DriverManagerDataSource();dataSource.setDriverClassName("com.mysql.jdbc.Driver");dataSource.setUrl("jdbc:mysql://localhost:3306/example_db");dataSource.setUsername("user");dataSource.setPassword("password");return dataSource;}
}
- @Value注解:@Value注解用于注入值。你可以从属性文件中获取值,也可以直接硬编码。
@Value("${example.property}")
private String exampleProperty;
- @Qualifier注解:当有多个相同类型的Bean时,你可以用@Qualifier来指定具体使用哪一个Bean。
Autowired
@Qualifier("exampleBean")
private ExampleBean exampleBean;
以上就是Springboard的一些常用代码方法和注解的演示,希望对你有所帮助。在实际开发中,根据需要选择合适的注解和方法,可以大大提高开发效率,使代码更加清晰、易于维护。