Spring的一個核心功能是IOC,就是將Bean初始化加載到容器中,Bean是如何加載到容器的,可使用Spring註解方式或者Spring XML配置方式。
Spring註解方式減小了配置文件內容,更加便於管理,而且使用註解能夠大大提升了開發效率!
下面按照分類講解Spring中經常使用的一些註解。html
思考:Spring怎麼知道應該把哪些Java類當成bean註冊到容器中呢?前端
答案:使用配置文件或者註解的方式進行標識須要處理的java類!java
@Component :標準一個普通的spring Bean類。
@Repository:標註一個DAO組件類。
@Service:標註一個業務邏輯組件類。
@Controller:標註一個控制器組件類。
這些都是註解在平時的開發過程當中出鏡率極高,@Component、@Repository、@Service、@Controller實質上屬於同一類註解,用法相同,功能相同,區別在於標識組件的類型。@Component能夠代替@Repository、@Service、@Controller,由於這三個註解是被@Component標註的。以下代碼git
@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@Componentpublic @interface Controller {spring
String value() default "";app
}ui
(1)當一個組件表明數據訪問層(DAO)的時候,咱們使用@Repository進行註解,以下spa
@Repositorypublic class HappyDaoImpl implements HappyDao{private final static Logger LOGGER = LoggerFactory.getLogger(HappyDaoImpl .class);public void club(){prototype
//do something ,like drinking and singingcomponent
}
(2)當一個組件表明業務層時,咱們使用@Service進行註解,以下
@Service(value="goodClubService")//使用@Service註解不加value ,默認名稱是clubServicepublic class ClubServiceImpl implements ClubService {
@Autowired
private ClubDao clubDao;
public void doHappy(){
//do some Happy
}
(3)當一個組件做爲前端交互的控制層,使用@Controller進行註解,以下
@Controllerpublic class HappyController {
@Autowired //下面進行講解
private ClubService clubService;
// Control the people entering the Club
// do something
}/*Controller相關的註解下面進行詳細講解,這裏簡單引入@Controller*/
1、被註解的java類當作Bean實例,Bean實例的名稱默認是Bean類的首字母小寫,其餘部分不變。@Service也能夠自定義Bean名稱,可是必須是惟一的!
2、儘可能使用對應組件註解的類替換@Component註解,在spring將來的版本中,@Controller,@Service,@Repository會攜帶更多語義。而且便於開發和維護!
3、指定了某些類可做爲Spring Bean類使用後,最好還須要讓spring搜索指定路徑,在Spring配置文件加入以下配置:
<!-- 自動掃描指定包及其子包下的全部Bean類 --><context:component-scan base-package="org.springframework.*"/>
@Autowired:屬於Spring 的org.springframework.beans.factory.annotation包下,可用於爲類的屬性、構造器、方法進行注值
@Resource:不屬於spring的註解,而是來自於JSR-250位於java.annotation包下,使用該annotation爲目標bean指定協做者Bean。
@PostConstruct 和 @PreDestroy 方法 實現初始化和銷燬bean以前進行的操做
(1):@Autowired
@Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface Autowired {
boolean required() default true;
@Controllerpublic class HappyController {
@Autowired //默認依賴的ClubDao 對象(Bean)必須存在
//@Autowired(required = false) 改變默認方式
@Qualifier("goodClubService")
private ClubService clubService;
// Control the people entering the Club
// do something
(2):@Resource
@Target({TYPE, FIELD, METHOD})
@Retention(RUNTIME)
public @interface Resource {
String name() default "";
Class type() default java.lang.Object.class;
...
public class AnotationExp {
@Resource(name = "HappyClient")
private HappyClient happyClient;
@Resource(type = HappyPlayAno .class)
private HappyPlayAno happyPlayAno;
}
(1):相同點
@Resource的做用至關於@Autowired,都可標註在字段或屬性的setter方法上。
(2):不一樣點
a:提供方 @Autowired是Spring的註解,@Resource是javax.annotation註解,而是來自於JSR-250,J2EE提供,須要JDK1.6及以上。
b :注入方式 @Autowired只按照Type 注入;@Resource默認按Name自動注入,也提供按照Type 注入;
c:屬性
@Autowired註解可用於爲類的屬性、構造器、方法進行注值。默認狀況下,其依賴的對象必須存在(bean可用),若是須要改變這種默認方式,能夠設置其required屬性爲false。
還有一個比較重要的點就是,@Autowired註解默認按照類型裝配,若是容器中包含多個同一類型的Bean,那麼啓動容器時會報找不到指定類型bean的異常,解決辦法是結合@Qualified註解進行限定,指定注入的bean名稱。
@Resource有兩個中重要的屬性:name和type。name屬性指定byName,若是沒有指定name屬性,當註解標註在字段上,即默認取字段的名稱做爲bean名稱尋找依賴對象,當註解標註在屬性的setter方法上,即默認取屬性名做爲bean名稱尋找依賴對象。
須要注意的是,@Resource若是沒有指定name屬性,而且按照默認的名稱仍然找不到依賴對象時, @Resource註解會回退到按類型裝配。但一旦指定了name屬性,就只能按名稱裝配了。
d:@Resource註解的使用性更爲靈活,可指定名稱,也能夠指定類型 ;@Autowired註解進行裝配容易拋出異常,特別是裝配的bean類型有多個的時候,而解決的辦法是須要在增長@Qualitied進行限定。
Spring中 @Autowired註解與@Resource註解的區別
<context:component-scan> <!--<context:component-scan>的使用,是默認激活<context:annotation-config>功能-->
則必定要配置 annotation-config
<context:annotation-config/>
Spring的官方團隊說@Component能夠替代 @Configuration註解,事實上咱們看源碼也能夠發現看到,以下
@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@Component //看這裏!!!public @interface Configuration {
String value() default "";
雖說能夠替代可是兩個註解之間仍是有區別的!
Bean註解主要用於方法上,有點相似於工廠方法,當使用了@Bean註解,咱們能夠連續使用多種定義bean時用到的註解,譬如用@Qualifier註解定義工廠方法的名稱,用@Scope註解定義該bean的做用域範圍,譬如是singleton仍是prototype等。
Spring 中新的 Java 配置支持的核心就是@Configuration 註解的類。這些類主要包括 @Bean 註解的方法來爲 Spring 的 IoC 容器管理的對象定義實例,配置和初始化邏輯。
使用@Configuration 來註解類表示類能夠被 Spring 的 IoC 容器所使用,做爲 bean 定義的資源。
@Configurationpublic class AppConfig {
@Bean
public MyService myService() {
return new MyServiceImpl();
}
這和 Spring 的 XML 文件中的很是相似
<beans>
<bean id="myService" class="com.acme.services.MyServiceImpl"/></beans>
@Bean 註解扮演了和元素相同的角色。
@Configurationpublic static class Config {
@Bean
public SimpleBean simpleBean() {
return new SimpleBean();
}
@Bean
public SimpleBeanConsumer simpleBeanConsumer() {
return new SimpleBeanConsumer(simpleBean());
}
}
@Componentpublic static class Config {
@Bean
public SimpleBean simpleBean() {
return new SimpleBean();
}
@Bean
public SimpleBeanConsumer simpleBeanConsumer() {
return new SimpleBeanConsumer(simpleBean());
}
}
第一個代碼正常工做,正如預期的那樣,SimpleBeanConsumer將會獲得一個單例SimpleBean的連接。第二個配置是徹底錯誤的,由於Spring會建立一個SimpleBean的單例bean,可是SimpleBeanConsumer將得到另外一個SimpleBean實例(也就是至關於直接調用new SimpleBean() ,這個bean是不歸Spring管理的),既new SimpleBean() 實例是Spring上下文控件以外的。
使用@ configuration,全部標記爲@ bean的方法將被包裝成一個CGLIB包裝器,它的工做方式就好像是這個方法的第一個調用,那麼原始方法的主體將被執行,最終的對象將在spring上下文中註冊。全部進一步的調用只返回從上下文檢索的bean。
在上面的第二個代碼塊中,新的SimpleBeanConsumer(simpleBean())只調用一個純java方法。爲了糾正第二個代碼塊,咱們能夠這樣作
@Componentpublic static class Config {
@Autowired
SimpleBean simpleBean;
@Bean
public SimpleBean simpleBean() {
return new SimpleBean();
}
@Bean
public SimpleBeanConsumer simpleBeanConsumer() {
return new SimpleBeanConsumer(simpleBean);
}
}
Spring @Configuration vs @Component
基本概念:@Configuration 和@Bean