XML和註解
阿新 • • 發佈:2019-01-24
實際開發中使用XML還是註解?
XML:
bean的管理
註解:
注入屬性的時候比較方便
兩種方式的結合:一般使用XML註冊bean,使用註解來進行屬性注入。在applicationContext.xml中配置<context:annotation-config></context:annotation-config>
舉個簡單的例子:
新建3個bean:
- package com.js.demo4;
- publicclass OrderDao {
- }
- package com.js.demo4;
- publicclass CustomerDao {
- }
- package com.js.demo4;
- publicclass CustomerService {
- private CustomerDao customerDao;
- private OrderDao orderDao;
- @Override
- public String toString() {
- return"CustomerService [customerDao=" + customerDao + ", orderDao="
- + orderDao + "]";
- }
- publicvoid setCustomerDao(CustomerDao customerDao) {
- this.customerDao = customerDao;
- }
- publicvoid setOrderDao(OrderDao orderDao) {
- this.orderDao = orderDao;
- }
- }
配置applicationContext.xml:
- <?xmlversion="1.0"encoding="UTF-8"?>
- <beansxmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
- <!-- 純用註解配置bean的情況下context:annotation-config可以省略,當混合使用xml和註解配置bean的時候就不能省略了-->
- <context:annotation-config></context:annotation-config>
- <!-- 配置掃描註解的包 -->
- <!-- <context:component-scan base-package="com.js"></context:component-scan> -->
- <beanid="customerDao"class="com.js.demo4.CustomerDao"></bean>
- <beanid="customerService"class="com.js.demo4.CustomerService">
- <propertyname="customerDao"ref="customerDao"></property>
- <propertyname="orderDao"ref="orderDao"></property>
- </bean>
- <beanid="orderDao"class="com.js.demo4.OrderDao"></bean>
- </beans>
編寫測試類:
- package com.js.demo4;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- publicclass Test4 {
- @Test
- publicvoid demo(){
- ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext2.xml");
- CustomerService customerService = (CustomerService)applicationContext.getBean("customerService");
- System.out.println(customerService);
- }
- }
執行結果,注入成功:
我們能不能修改配置方式,只在xml中註冊bean,而將bean的屬性注入使用註解來完成呢?
修改CustomerService:使用註解傳入的屬性就不在需要setter方法,也不再需要Component-scan,因為我們的bean全部在xml中配置,此處修改兩個屬性之一的orderDao:
- package com.js.demo4;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Qualifier;
- publicclass CustomerService {
- private CustomerDao customerDao;
- @Autowired
- @Qualifier("orderDao")
- private OrderDao orderDao;
- @Override
- public String toString() {
- return"CustomerService [customerDao=" + customerDao + ", orderDao="
- + orderDao + "]";
- }
- publicvoid setCustomerDao(CustomerDao customerDao) {
- this.customerDao = customerDao;
- }
- }
修改xml配置檔案:
- <?xmlversion="1.0"encoding="UTF-8"?>
- <beansxmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
- <!-- 純用註解配置bean的情況下context:annotation-config可以省略,當混合使用xml和註解配置bean的時候就不能省略了-->
- <!-- 作用是使@Resource、@PostConstruct、@PreDestroy、@AutoWired生效-->
- <context:annotation-config></context:annotation-config>
- <beanid="customerDao"class="com.js.demo4.CustomerDao"></bean>
- <beanid="customerService"class="com.js.demo4.CustomerService">
- <propertyname="customerDao"ref="customerDao"></property>
- </bean>
- <beanid="orderDao"class="com.js.demo4.OrderDao"></bean>
- </beans>
執行測試,成功。