Spring学习-13:Spring的XML和注解的结合使用

实际开发中使用XML还是注解?

XML:

bean的管理

注解:

注入属性的时候比较方便


两种方式的结合:一般使用XML注册bean,使用注解来进行属性注入。在applicationContext.xml中配置<context:annotation-config></context:annotation-config>

举个简单的例子:

新建3个bean:

package com.js.demo4;

public class OrderDao {

}


package com.js.demo4;

public class CustomerDao {

}


package com.js.demo4;

public class CustomerService {
	private CustomerDao customerDao;
	private OrderDao orderDao;
	@Override
	public String toString() {
		return "CustomerService [customerDao=" + customerDao + ", orderDao="
				+ orderDao + "]";
	}
	public void setCustomerDao(CustomerDao customerDao) {
		this.customerDao = customerDao;
	}
	public void setOrderDao(OrderDao orderDao) {
		this.orderDao = orderDao;
	}
	
}


配置applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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> -->
<bean id="customerDao" class="com.js.demo4.CustomerDao"></bean>
<bean id="customerService" class="com.js.demo4.CustomerService">
	<property name="customerDao" ref="customerDao"></property>
	<property name="orderDao" ref="orderDao"></property>
</bean>
<bean id="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;

public class Test4 {
	@Test
	public void 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;

public class CustomerService {
	private CustomerDao customerDao;
	@Autowired
	@Qualifier("orderDao")
	private OrderDao orderDao;
	@Override
	public String toString() {
		return "CustomerService [customerDao=" + customerDao + ", orderDao="
				+ orderDao + "]";
	}
	public void setCustomerDao(CustomerDao customerDao) {
		this.customerDao = customerDao;
	}
	
}

修改xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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>

<bean id="customerDao" class="com.js.demo4.CustomerDao"></bean>
<bean id="customerService" class="com.js.demo4.CustomerService">
	<property name="customerDao" ref="customerDao"></property>
</bean>
<bean id="orderDao" class="com.js.demo4.OrderDao"></bean>
</beans>

运行测试,成功。









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值