在開發中spring ioc的配置方式有多種方式,常用的一般都是byName、byType 、以及其自動裝配可以查看http://www.cnblogs.com/gubai/p/9140840.html ,其實這些方式也能滿足開發的日常需求但與Annotation 配置方式相比較 其配置方式也顯得比較繁瑣。下邊介紹下其常用注解及其用法:@Autowired
1.Autowired注解
1.Autowire注解可以修飾成員變量 配置Ioc實例如下:
1 //Cat 類 2 public class Cat { 3 private String Catname="小貓咪"; 4 5 public String toString(){ 6 7 return "CatName:"+Catname; 8 } 9 //Lion 類 10 public class Lion { 11 private String LionName="獅子王"; 12 13 public String toString(){ 14 15 return "LionName:"+LionName; 16 } 17 } 18 //Aniaml 19 public class Animal{ 20 @Autowired 21 private Cat cat; 22 @Autowired 23 private Lion lion;
//注意這里邊並沒有setter getter方法 24 25 public String toString (){ 26 return cat+ "\n"+lion; 27 } 28 29 } 30 //就是簡單的把Cat 類和Lion 注入到Animal 中
配置文件如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:util="http://www.springframework.org/schema/util" 5 xmlns:p="http://www.springframework.org/schema/p" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> 8 <!--byname bytype 配置時沒有這一行的--> 9 <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> 10 11 <bean id="animal" class="com.myspring2.annotation.Animal"></bean> 12 13 <bean id="cat" class="com.myspring2.annotation.Cat"> 14 </bean> 15 16 <bean id="lion" class="com.myspring2.annotation.Lion"> 17 </bean> 18 19 </beans>
測試:
public class Main { public static void main(String[] args) { ApplicationContext ac= new ClassPathXmlApplicationContext("applicationContext.xml"); Animal animal=ac.getBean(Animal.class); System.out.println(animal.toString()); } }
答應結果:CatName:小貓咪
LionName:獅子王
解讀:很顯然我們得到了想要的結果,當spring容器啟動時AutowiredAnnotationBeanPostProcessor會自動的掃描spring容器中所有的bean,當其發現有@Autowired注解標注是,就會將自動在代碼上下文中找到和其匹配(默認是類型匹配)的Bean,注入到該類中去。不需要再Animal類中寫set get 方法。
@Autowired 除了能修飾成員變量之外,還能修飾setter方法 以及帶參數的構造方法 。
需要注意的是:當使用這個@Autowired是應該保證spring 容器中當且僅當只有一個合適的bean,否則會報錯BeanCreationException,解決方法是@Autowreid(required=false)表示的意思是找不到合適的Bean也不會拋出異常。
2.Qualifier(指定注入Bean的名稱)
當spring容器中找到1個以上的bean是,會拋出異常,測試代碼如下:
1 //貓科動物接口 2 public interface Felidae { 3 public String getName(); 4 } 5 6 //Cat 類 7 public class Cat implements Felidae{ 8 String catName="小貓咪"; 9 public String getName(){ 10 return catName; 11 } 12 13 } 14 public class Lion implements Felidae{ 15 String lionName="獅子王"; 16 public String getName(){ 17 return lionName; 18 } 19 20 } 21 public class Animal { 22 @Autowired 23 Felidae felidae; 24 public String toString(){ 25 return felidae.getName(); 26 } 27 28 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:p="http://www.springframework.org/schema/p" 6 xmlns:context="http://www.springframework.org/schema/context" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context-3.0.xsd 11 "> 12 <context:component-scan base-package="com.myspring2.annotation" /> 13 <!--自動掃描上邊的包以及其子包 需要開啟--> 14 <bean id="animal" class="com.myspring2.annotation.Animal"></bean> 15 16 <bean id="cat" class="com.myspring2.annotation.Cat"> 17 </bean> 18 19 <bean id="lion" class="com.myspring2.annotation.Lion"> 20 </bean> 21 22 </beans> 23
Cat類和Lion 類實現了同一個幾口Felidae, 當在Animal 類中需要注入Felidae 是程序拋出了異常,原因是spring 找到了兩個匹配的bean,所以無法確定 到底需要注入那個bean
,解決方法只需要在@Autowired 下邊假如@Qualifier("cat")即明確告訴程序我們需要注入的bean,此處就是cat 了。
3.@Resource 次注解和 此注解和@Autowired 注解用法特別相似 ,只是其默認的Byname方法去匹配的,找不到再按Bytype 匹配的 ,@Autowired 是默認按bytype去匹配的
Spring 能夠從 classpath 下自動掃描, 偵測和實例化具有特定注解的組件.特定的組件包括以下幾個:
@Component:基本組件 標識了一個受spring 管理的組件,組件管理的通用形式,可以放在類上邊, 一般都不推薦使用--開發中使用的比較少
@Repository :標識持久層組件
@Service :標識服務層組件
@Controller:標識表現層組件
對於這些組件spring有默認的命名策略, 一般是首字母小寫,也可以通過注解中value 屬性命名。
簡單實例:
1 //表現層 2 @Controller 3 @Scope(value="prototype") 4 //用scope=prototype 來保證每次訪問都是單獨的action來處理spring 默認scope 是單例模式(scope="singleton"),這樣只會創建一個Action對象,每次訪問都是同一Action對象,數據不安全 5 public class UserAction { 6 @Autowired 7 UserService userService; 8 public void save(){ 9 userService.save(); 10 } 11 } 12 13 //服務層 14 public interface UserService { 15 16 public String save(); 17 } 18 19 @Service 20 public class UserServiceImpl implements UserService{ 21 @Autowired 22 UserDao userDao; 23 @Override 24 public String save() { 25 26 userDao.save(); 27 return "ok"; 28 } 29 30 } 31 //持久層 32 @Repository 33 public class UserDao { 34 public void save(){ 35 System.out.println("dao save"); 36 } 37 }
xml的配置文件中,需要開啟自動掃描即可