1. 程式人生 > >XML和註解

XML和註解

實際開發中使用XML還是註解?

XML:

bean的管理

註解:

注入屬性的時候比較方便

兩種方式的結合:一般使用XML註冊bean,使用註解來進行屬性注入。在applicationContext.xml中配置<context:annotation-config></context:annotation-config>

舉個簡單的例子:

新建3個bean:

  1. package com.js.demo4;  
  2. publicclass OrderDao {  
  3. }  
  1. package com.js.demo4;  
  2. publicclass CustomerDao {  
  3. }  
  1. package com.js.demo4;  
  2. publicclass CustomerService {  
  3.     private CustomerDao customerDao;  
  4.     private OrderDao orderDao;  
  5.     @Override
  6.     public String toString() {  
  7.         return"CustomerService [customerDao=" + customerDao + ", orderDao="
  8.                 + orderDao + "]";  
  9.     }  
  10.     publicvoid setCustomerDao(CustomerDao customerDao) {  
  11.         this.customerDao = customerDao;  
  12.     }  
  13.     publicvoid setOrderDao(OrderDao orderDao) {  
  14.         this.orderDao = orderDao;  
  15.     }  
  16. }  


配置applicationContext.xml:
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beansxmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.        xmlns:context="http://www.springframework.org/schema/context"
  5.        xsi:schemaLocation="  
  6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  8. <!-- 純用註解配置bean的情況下context:annotation-config可以省略,當混合使用xml和註解配置bean的時候就不能省略了-->
  9. <context:annotation-config></context:annotation-config>
  10. <!-- 配置掃描註解的包 -->
  11. <!-- <context:component-scan base-package="com.js"></context:component-scan> -->
  12. <beanid="customerDao"class="com.js.demo4.CustomerDao"></bean>
  13. <beanid="customerService"class="com.js.demo4.CustomerService">
  14.     <propertyname="customerDao"ref="customerDao"></property>
  15.     <propertyname="orderDao"ref="orderDao"></property>
  16. </bean>
  17. <beanid="orderDao"class="com.js.demo4.OrderDao"></bean>
  18. </beans>

編寫測試類:
  1. package com.js.demo4;  
  2. import org.junit.Test;  
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5. publicclass Test4 {  
  6.     @Test
  7.     publicvoid demo(){  
  8.         ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext2.xml");  
  9.         CustomerService customerService = (CustomerService)applicationContext.getBean("customerService");  
  10.         System.out.println(customerService);  
  11.     }  
  12. }  

執行結果,注入成功:


我們能不能修改配置方式,只在xml中註冊bean,而將bean的屬性注入使用註解來完成呢?

修改CustomerService:使用註解傳入的屬性就不在需要setter方法,也不再需要Component-scan,因為我們的bean全部在xml中配置,此處修改兩個屬性之一的orderDao:

  1. package com.js.demo4;  
  2. import org.springframework.beans.factory.annotation.Autowired;  
  3. import org.springframework.beans.factory.annotation.Qualifier;  
  4. publicclass CustomerService {  
  5.     private CustomerDao customerDao;  
  6.     @Autowired
  7.     @Qualifier("orderDao")  
  8.     private OrderDao orderDao;  
  9.     @Override
  10.     public String toString() {  
  11.         return"CustomerService [customerDao=" + customerDao + ", orderDao="
  12.                 + orderDao + "]";  
  13.     }  
  14.     publicvoid setCustomerDao(CustomerDao customerDao) {  
  15.         this.customerDao = customerDao;  
  16.     }  
  17. }  

修改xml配置檔案:
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beansxmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.        xmlns:context="http://www.springframework.org/schema/context"
  5.        xsi:schemaLocation="  
  6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  8. <!-- 純用註解配置bean的情況下context:annotation-config可以省略,當混合使用xml和註解配置bean的時候就不能省略了-->
  9. <!-- 作用是使@Resource、@PostConstruct、@PreDestroy、@AutoWired生效-->
  10. <context:annotation-config></context:annotation-config>
  11. <beanid="customerDao"class="com.js.demo4.CustomerDao"></bean>
  12. <beanid="customerService"class="com.js.demo4.CustomerService">
  13.     <propertyname="customerDao"ref="customerDao"></property>
  14. </bean>
  15. <beanid="orderDao"class="com.js.demo4.OrderDao"></bean>
  16. </beans>

執行測試,成功。