springboot是spring公司开发一套快速搭建javaWeb项目的框架,其最大的特点就是简便快捷,简化了Spring项目大量的配置文件,其核心思想就是约定大于配置,而注解这种简便快捷的方式自然成为不二选择,下面总结了springboot中一些常用的注解,以后也会持续更新加入新的注解,当然对于@Controller @RequestMapping 这种太过常见的就不再举例了。
(一)参数类
1.@RequestParam 属性 value = "name",required = true/false defaultValue = xxx
public void say(@RequestParam("name") String name,@RequestParam("pwd") String password){
}
知识兔2.@PathVariable 与@RequestParam 类似 基于restful风格
)
public void say(@PathVariable("id") String id,@PathVariable("name") String name1){
}
知识兔3.@RequestBody 将前端表单中的name属性绑定到实体类的属性上,要求表单中的name名称和实体类的属性名称一致。
public void say(@RequestBody User user){
}
知识兔4.@RequstAttribute 从请求中获取值、拦截器或@ModelAttribute中预存的属性值 默认值true,没有值时会抛异常ServletRequestBindingException,可将required属性设置成false.
(二).扫描类的注解
1.@Component @Controller @Service @Repository这三个是@Component的子类,将扫描的包加入到IOC容器中,对于没有扫描到的包可用@ComponentScan扫描指定的包,定义在类上
2.@Bean 告诉Spring这个方法产生一个类对其管理,产生一个实例Bean,@Bean相对于@Component更加的灵活
3.@Configuration 相当于spring的xml中的<beans>
+@Bean 则相当于xml中的<bean>
package com.dxz.demo.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
@Configuration
public class TestConfiguration {
public TestConfiguration() {
System.out.println("TestConfiguration容器启动初始化。。。");
}
// @Bean注解注册bean,同时可以指定初始化和销毁方法
// @Bean(name="testBean",initMethod="start",destroyMethod="cleanUp")
@Bean
@Scope("prototype")
public TestBean testBean() {
return new TestBean();
}
}
知识兔TestBean
package com.dxz.demo.configuration;
public class TestBean {
private String username;
private String url;
private String password;
public void sayHello() {
System.out.println("TestBean sayHello...");
}
public String toString() {
return "username:" + this.username + ",url:" + this.url + ",password:" + this.password;
}
public void start() {
System.out.println("TestBean 初始化。。。");
}
public void cleanUp() {
System.out.println("TestBean 销毁。。。");
}
}
知识兔测试类
复制代码
package com.dxz.demo.configuration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class TestMain {
public static void main(String[] args) {
// @Configuration注解的spring容器加载方式,用AnnotationConfigApplicationContext替换ClassPathXmlApplicationContext
ApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
// 如果加载spring-context.xml文件:
// ApplicationContext context = new
// ClassPathXmlApplicationContext("spring-context.xml");
//获取bean
TestBean tb = (TestBean) context.getBean("testBean");
tb.sayHello();
}
}
知识兔结果:
也可以在TestBean类上加@Componet配合@Configration使用
(三).资源引入类
1. @Value
@Value("${book.name}") 获取application.properties(也可以是你的其他配置文件中)的book.name=old man and sea
@Value("#{name}") 获取book实体类的name属性
2.@Resource 和@Autowired
都是作为Bean注入时使用,@Resource 是 java的注解,Spring也支持这一注解,有Name和Type属性,当都不指定时用name属性;@Autowired注解是Spring的注解默认是 ByType的
当一个类有多个实现时,@Resource(name="xxx") 或@Qualifier(
而 @Autowired需要和@Primary 在实现类上(man 和woman实现people,需要在man或woman上实现)3.@ImportResource("classpath:xxxxx.xml")加载xml的配置文件
4.@ConfigurationProperties
有时需要将配置文件中的信息封装成实体类,这样调用起来就会很方便,
配置文件:
admin
connection.password=kyjufskifas2jsfs
connection.remoteAddress=192.168.1.1
知识兔实体类:
@Component
@ConfigurationProperties(prefix = "connection")
@Data //lombok的注解 需要引入lombok的依赖 IDEA工具安装lombok 作用 自动生成get set 方法
public class ConnectionSettings {
private String username;
private String remoteAddress;
private String password ;
}
知识兔使用时
@RestController
@RequestMapping("/task")
public class TaskController {
@Autowired
ConnectionSettings conn;
@RequestMapping(value = {"/", ""})
public String hellTask(){
String userName = conn.getUsername();
return "hello task !!";
}
}
知识兔当然你也可以直接将@ConfigurationProperties 和@Bean 放一起使用 这样就不需要在ConnectionSettings类上加注解了
@Bean
@ConfigurationProperties(prefix = "connection")
public ConnectionSettings connectionSettings(){
return new ConnectionSettings();
}
知识兔(四).事务类
1.@Transactional