Srping Boot 註解整理

記錄一些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可以對成員變量、方法和構造函數進行標註,來完成自動裝配的工作。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章