一、Spring的优点
企业及系统:
1.大规模:用户数量多、数据规模大、功能众多
2.性能和安全要求高
3.业务复杂
4.灵活应变
Java技术:高入侵式依赖EJB技术框架-->Spring框架
优点:
轻量级框架,Java EE的春天
“一站式”的企业应用开发框架
*****
1.低侵入式设计
2.独立于各种应用服务器
3.依赖注入特性将组件关系透明化,降低了耦合度
4.面向切面编程特性允许将通用任务进行集中式处理
5.与第三方框架的良好整合
二、Spring的设计理念
使现有技术更加易用,推进编码最佳实践
三、Spring三个核心组件的作用
一、IoC容器
1.什么是控制反转?
将组件对象的控制权从代码本身转移到外部容器:
组件化的思想——分离关注点,使用接口,不再关注实现
目的:解耦合,只关注组件内部的事情
2.工厂模式
产品的规范
产品
工厂
客户端/调用
*****根据所需对象的描述,返回所需要的产品。
IoC注入实现控制反转:
步骤:
1.添加Spring到项目中
2.编写配置文件applicationContext.xml
1 26 8 9 10 1511 12 14Spring 13
3.编写代码获取实例
1 public class HelloSpring { 2 //定义who属性,该属性的值将通过Spring框架进行设置 3 private String who = null; 4 5 public String getWho() { 6 return who; 7 } 8 9 public void setWho(String who) {10 this.who = who;11 }12 /**13 * 定义打印方法,输出一句完整的问候14 */15 public void print(){16 System.out.println("Hello,"+this.getWho()+"!");17 }18 }
1 @Test2 public void test() {3 //通过ClassPathXmlApplicationContext实例化Spring的上下文4 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");5 //通过ApplicationContext的getBean()方法,根据id来获取Bean的实例6 HelloSpring helloSpring = (HelloSpring) context.getBean("helloSpring");7 //执行print()方法8 helloSpring.print();9 }