赞
踩
欢迎访问我的个人博客:https://ddddddddd.top
搬运请注明
1.1Spring是分层的Java SE/EE应用 full-stack轻量级开源框架,以IoC(Inverse Of Control:反转控制)和AOP(Aspect Oriented Programming:面向切面编程)为内核,提供了展现层Spring MVC和持久层Spring JDBC以及业务层事务管理等众多的企业级应用技术,还能整合开源世界众多著名的第三方框架和类库,逐渐成为使用最多的Java EE企业应用开源框架。
2.1方便解耦,简化开发
通过Spring提供的IoC容器,可以将对象间的依赖关系交由Spring进行控制,避免硬编码所造成的过度程序耦合。用户也不必再为单例模式类、属性文件解析等这些很底层的需求编写代码,可以更专注于上层的应用。
2.2AOP编程的支持
通过Spring的AOP功能,方便进行面向切面的编程,许多不容易用传统OOP实现的功能可以通过AOP轻松应付。
声明式事务的支持
可以将我们从单调烦闷的事务管理代码中解脱出来,通过声明式方式灵活的进行事务的管理,提高开发效率和质量。
2.3方便程序的测试
可以用非容器依赖的编程方式进行几乎所有的测试工作,测试不再是昂贵的操作,而是随手可做的事情。
2.4方便集成各种优秀框架
Spring可以降低各种框架的使用难度,提供了对各种优秀框架(Struts、Hibernate、Hessian、Quartz等)的直接支持。
2.5降低JavaEE API的使用难度
Spring对JavaEE API(如JDBC、JavaMail、远程调用等)进行了薄薄的封装层,使这些API的使用难度大为降低。
2.6Java源码是经典学习范例
Spring的源代码设计精妙、结构清晰、匠心独用,处处体现着大师对Java设计模式灵活运用以及对Java技术的高深造诣。它的源代码无意是Java技术的最佳实践的范例。
需求
客户的业务层和持久层的依赖关系解决。在开始spring的配置之前,我们要先准备一下环境。由于我们是使用spring解决依赖关系,并不是真正的要做增伤改查操作,所以此时我们没必要写实体类。并且我们在此处使用的是java工程,不是java web工程。
4.1配置环境
4.1.1准备spring的开发包
官网:http://spring.io/
下载地址:
http://repo.springsource.org/libs-release-local/org/springframework/spring
解压:(Spring目录结构:)
* docs :API和开发规范.
* libs :jar包和源码.
* schema :约束.
4.1.2在src下创建一个bean.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- bean definitions here -->
</beans>
4.2创建业务层接口和实现类
/** * 客户的业务层接口 */ public interface ICustomerService { /** * 保存客户 * @param customer */ void saveCustomer(); } /** * 客户的业务层实现类 */ public class CustomerServiceImpl implements ICustomerService { private ICustomerDao customerDao = new CustomerDaoImpl();//此处有依赖关系 @Override public void saveCustomer() { customerDao.saveCustomer(); } }
/**
* 客户的业务层实现类
* @author FangPengbo
*
*/
public class ICustomerServiceImpl implements ICustomerService{
private ICustomerDao customerDao = new CustomerDaoImpl();//此处有依赖关系
@Override
public void save() {
// TODO Auto-generated method stub
customerDao.saveCustomer();
}
}
4.3创建持久层接口和实现类
/** * 客户的持久层接口 */ public interface ICustomerDao { /** * 保存客户 */ void saveCustomer(); } /** * 客户的持久层实现类 */ public class CustomerDaoImpl implements ICustomerDao { @Override public void saveCustomer() { System.out.println("保存了客户"); } }
4.4把资源交给spring来管理,在配置文件中配置service和dao
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- bean definitions here -->
<bean id="customerDao" class="com.itheima.dao.impl.CustomerDaoImpl"/>
<bean id="customerService" class="com.itheima.service.impl.CustomerServiceImpl"/>
</beans>
4.5测试配置是否成功
/** * 模拟一个表现层 */ public class Client { /** * 使用main方法获取容器测试执行 */ public static void main(String[] args) { //1.使用ApplicationContext接口,就是在获取spring容器 ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); //2.根据bean的id获取对象 ICustomerService cs = (ICustomerService) ac.getBean("customerService"); System.out.println(cs); ICustomerDao cd = (ICustomerDao) ac.getBean("customerDao"); System.out.println(cd); } }
总结
spring中工厂的类结构图
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。