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:
配置applicationContext.xml:
编写测试类:
运行结果,注入成功:
我们能不能修改配置方式,只在xml中注册bean,而将bean的属性注入使用注解来完成呢?
修改CustomerService:使用注解传入的属性就不在需要setter方法,也不再需要Component-scan,因为我们的bean全部在xml中配置,此处修改两个属性之一的orderDao:
修改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>
运行测试,成功。
相关文章推荐
- jpa学习2---jpa和spring结合使用半注解方式
- Spring 3.0 学习-DI 依赖注入_创建Spring 配置-使用一个或多个XML 文件作为配置文件,使用自动注入(byName),在代码中使用注解代替自动注入,使用自动扫描代替xml中bea
- Spring2.5注解(标注)学习笔记(使用annotation代替XML)
- Spring2.5注解(标注)学习笔记(使用annotation代替XML)
- Java 学习笔记13:Spring JSTL 核心标签库 使用
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(二):进一步消除 hibernate.cfg.xml
- spring aop的使用(注解方式以及基于xml配置方式)
- spring基础学习三(注解使用)
- spring学习笔记7--使用spring进行面向切面的(AOP)编程(1)注解方式实现
- Spring学习笔记 使用XML配置实现Bean的auto-wiring (自动绑定)
- ITCAST视频-Spring学习笔记(使用Spring的注解方式实现AOP的细节)
- aop(使用xml方式)(spring团队建议我们使用注解的方式)
- Spring视频学习(九)使用Spring注解方式管理事务与传播行为详解
- spring 学习笔记 使用pojo+xml的方式开发aop
- 学习《Spring实战(第3版)》扎记1:Spring使用注解配置Bean的方式
- Spring学习笔记 在XML配置文件中使用properties文件的键值
- Spring学习笔记(15)----使用Spring注解方式管理事务
- Spring学习笔记(14)----使用Spring的注解方式实现AOP
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(一):消除hibernate中<mapping resouce 的xxxx. hbm.xml文件
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(二):进一步消除 hibernate.cfg.xml