Spring完全注解開發


 

注解的好處:如果管理很多的Bean,要求這些Bean都配置在applocationContext.xml文件中。用了注解之后,就不需要在xml文件中配置了,Spring提供了幾個輔助類會自動掃描和裝配這些Bean。所以大大減少了xml文件的體積,Spring會根據配置去掃描某些包里面的類,得到類的方法或注解,不同的注解會進行不同的操作。

@Configuration

這個注解一般的來說,用在類上面,加上這個注解的類可以成為一個spring的xml配置文件,使用的是java代碼的配置

//配置Spring裝載類
//Configurable 指定此類為Spring配置類。作為配置類,替代XML配置文件
//ComponentScan 指定Bean范圍(注解范圍),掃描裝載
@Configurable
@ComponentScan(basePackages = {"com.minelsh"})
public class springConfig {
}

 

創建對象的注解

Spring中作用在作用類上的注解有@Component注解和三個衍生注解:@Responsity  @Service和@Controller
三個衍生注解的說明:

@Controller:通常用於Controller類,也就是控制層(MVC)。

@Service:通常用於Service類,也就是服務層。

@Repository:通常用於DAO類,也就是持久層。

當注解在作用類上時,表明這些類似交給Spring容器進行管理的。

@Component注解的使用

@Component("conversionImpl")
//其實默認的spring中的Bean id 為 conversionImpl(首字母小寫)
public class ConversionImpl implements Conversion {
​
}

 

案例1:

不指定bean的名稱,默認為類名首寫小寫字母

@Component
public class University {
    to do sthing...
}

 

獲取bean方式:

ApplicationContext ctx  = new 
                ApplicationContext context = new AnnotationConfigApplicationContext(Bean配置類);
University ust = (University) ctx.getBean("university");

 

案列2:

指定bean的名稱

@Component("university1")
public class University {
    to do sthing...
}

 

獲取bean方式:

ApplicationContext ctx  = new 
                ApplicationContext context = new AnnotationConfigApplicationContext(Bean配置類);
University ust = (University) ctx.getBean("university1");

 

 

在構造器上的注解

@Autowired  :根據類型(ByType)來自動匹配(核心注解)

這個注解可以用於屬性,setter方法,還有構造器上,這個注解用於注入依賴的對象。當再一個屬性上加上@Autowired注解,有時可能要指定一些額外的值,Spring然后會自動的將值賦給這個屬性。

案例1:作用在構造器上

public class MovieRecommender {
    private final CustomerPreferenceDao customerPreferenceDao;
    @Autowired
    public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
    this.customerPreferenceDao = customerPreferenceDao;
    }
 // ...
}

 

案列2:使用在setter方法上

public class SimpleMovieLister {
    private MovieFinder movieFinder;
     @Autowired
    public void setMovieFinder(MovieFinder movieFinder) {
    this.movieFinder = movieFinder;
    }
 // ...
}

 

案列3:使用在域屬性上

public class MovieRecommender {
    private final CustomerPreferenceDao customerPreferenceDao;
    @Autowired
    private MovieCatalog movieCatalog;
    @Autowired
    public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
    this.customerPreferenceDao = customerPreferenceDao;
    }
 // ...
}

 

案列4:使用在任意方法名和產生的普通方法上

public class MovieRecommender {
    private MovieCatalog movieCatalog;
    private CustomerPreferenceDao customerPreferenceDao;
    @Autowired
    public void prepare(MovieCatalog movieCatalog,
    CustomerPreferenceDao customerPreferenceDao) {
    this.movieCatalog = movieCatalog;
    this.customerPreferenceDao = customerPreferenceDao;
    }
    // ...
}

 

案列5:使用在域屬性數組上

public class MovieRecommender {
    @Autowired
    private MovieCatalog[] movieCatalogs;
    // ...
}

 

案列6:使用在集合類型上

public class MovieRecommender {
    private Set<MovieCatalog> movieCatalogs;
    @Autowired
    public void setMovieCatalogs(Set<MovieCatalog> movieCatalogs) {
    this.movieCatalogs = movieCatalogs;
    }
    // ...
}

 

 

@Qualifier

這個注解和@Autowired一起使用,當想對注入的過程做更多的控制,@Qualifier可以幫助指定做更詳細的配置。一般在兩個或多個bean是相同的類型,spring在注入的時候會出現混亂

例如有個接口叫做Hellointerface,然后兩個bean都實現了Hellointerface接口

@Component
public class Bean1 implements HelloInterface {
  //
}
​
@Component
public class Bean2 implements HelloInterface {
  //
}

 

如果只使用@Autowired注解,Spring就不知道到底要注入哪一個bean。解決辦法就是加上@Qualifier注解

@Component
public class BeanA {
​
  @Autowired
  @Qualifier("bean2")
  private HelloInterface dependency;
  ...

 

 

@Resource:根據類型和名稱(ByType & ByName)來自動匹配(核心注解)

@Resource和@Autowired注解都是用來實現依賴注入的。只是@AutoWried按by type自動注入,而@Resource默認按byName自動注入。

@Resource有兩個重要屬性,分別是name和type
spring將name屬性解析為bean的名字,而type屬性則被解析為bean的類型。所以如果使用name屬性,這使用ByName的自動注入策略,如果使用Type類型則使用ByTyoe的自動注入策略。如果都沒有指定,則聽過反射機制使用ByName自動注入策略。

案列1:使用@Resource注解

@Resource(name="bucket")
    private String bucketName;
    @Resource(name="style")
    private String styleName;

 

翻譯為xml模式來看

<bean name="bucket" class="java.lang.String">
    <constructor-arg value="${oos.bucketName}"/>
</bean> 

<bean name="style" class="java.lang.String">
    <constructor-arg value="${oos.styleName}"/>
</bean>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM