Srping Boot 注解整理

来源:互联网 发布:深圳存爱网络股份公司 编辑:程序博客网 时间:2024/06/05 06:03

记录一些Spring Boot中的注解,一方面帮助记忆,一方面用的时候可以查询.

@Controller 

@Controller 用于标记在一个类上,使用它标记的类就是一个SpringMVC Controller 对象。分发处理器将会扫描使用了该注解的类的方法,并检测该方法是否使用了@RequestMapping 注解。@Controller 只是定义了一个控制器类,而使用@RequestMapping 注解的方法才是真正处理请求的处理器

@RestController

@RestController注解相当于@ResponseBody + @Controller合在一起的作用。

@RequestBody

将HTTP请求正文转换为适合的HttpMessageConverter对象。

@ResponseBody

将内容或对象作为 HTTP 响应正文返回,并调用适合HttpMessageConverter的Adapter转换对象,写入输出流。

@Value

通过@Value("对应name"),获取到.properties配置内容

@ConfigurationProperties

使用上面的@Value注解,如果内容多的话太麻烦.可以把.properties改为yml格式.在bean中使用@ConfigurationProperties(prefix = "com.lrj")自动加载.



@Autowired

自动注解,@Autowired可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作。

@RequestMapping

@RequestMapping 用来映射URL 到控制器类,或者是到Controller 控制器的处理方法上。比如在@Controller下声明@RequestMapping那么在调用这个控制类
的时候需要加上对应的映射名,在方法上声明一样.
method属性用于声明调用方法的方式.如:method = RequestMethod.GET,RequestMethod.POST。
@SpringBootApplication
@SpringBootApplication = (默认属性)@Configuration + @EnableAutoConfiguration + @ComponentScan。
下面分别写下三个对应注解的功能。
@Configuration
@Configuration的注解类标识这个类可以使用Spring IoC容器作为bean定义的来源。@Bean注解告诉Spring,一个带有@Bean的注解方法将返回一个对象,该
对象应该被注册为在Spring应用程序上下文中的bean。
      @Configuration和在xml中配置的区别:
基于XML配置的方式是这样:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
        default-lazy-init="true">
    <!--bean定义-->
<bean id="mockService" class="..MockServiceImpl">
    <propery name ="dependencyService" ref="dependencyService" />
</bean>
<bean id="dependencyService" class="DependencyServiceImpl"></bean>
   </beans>
基础@Configuration注解的代码是这样:
@Configuration
public class MockConfiguration{
    @Bean
    public MockService mockService(){
        return new MockServiceImpl(dependencyService());
    }
    
    @Bean
    public DependencyService dependencyService(){
        return new DependencyServiceImpl();
    }
}
@ComponentScan
     @component (把普通pojo实例化到spring容器(IOC)中,相当于配置文件中的<bean id="" class=""/>)
     @Component,@Service,@Controller,@Repository注解的类,并把这些类纳入进spring容器(IOC)中管理。 

下面写这个是引入component的扫描组件 
<context:component-scan base-package=”com.mmnc”>    
其中base-package为需要扫描的包(含所有子包) 
       1、@Service用于标注业务层组件 
       2、@Controller用于标注控制层组件(如struts中的action) 
       3、@Repository用于标注数据访问组件,即DAO组件. 
       4、@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。    
         @Service public class UserServiceImpl implements UserService { } 
         @Repository public class UserDaoImpl implements UserDao { } getBean的默认名称是类名(头字母小写),如果想自定义,
可以@Service(“***”)这样来指定,这种bean默认是单例的,如果想改变,可以使用@Service(“beanName”)          @Scope(“prototype”)来改变。可以使用以下方式指定初始化方法和销毁方法(方法名任意):
 @PostConstruct public void init() { } 

@EnableAutoConfiguration 
       @EnableAutoConfiguration能够自动配置spring的上下文,试图猜测和配置你想要的bean类,通常会自动根据你的类路径和你的bean定义自动配置。

@Autowired

自动注解,@Autowired可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作。

原创粉丝点击