您的位置:首页 > 编程语言 > Java开发

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

2017-03-30 17:51 459 查看
实际开发中使用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>


运行测试,成功。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐