Spring的最终模板是简化应用开发的编程模型。spring用于替代更加剧量级的企业级java技术,如EJB(Enterprise JavaBean)。html
为了下降Java开发的复杂度。spring采起了如下4中关键策略:java
Java是一门面向对象编程语言。Java应用本质上是一个个对象及其关系的组合。举个简单的例子。面试
在传统的人员招聘模式中,流程通常都是这样:HR从多如海的应聘简历中挑选而后进行笔试、面试等等一系列筛选后发放offer。这一系列过程复杂并且费时,最关键的是结果还不理想,特别是针对某些特定的岗位很难经过这一模式物色到合适的人才资源。 (本身建立特定的对象,使用特定的对象)spring
后来逐渐出现了一些公司专门提供相似的人才寻访服务,这就是大名鼎鼎的猎头行业。猎头的兴起能够说很大程度上改变了人才招聘的模式,如今公司须要招聘某个职位的人才,只须要告诉猎头我要一个怎样的人干怎样的工做等等要求,猎头就会经过本身的渠道去物色人才,通过筛选后提供给客户,大大简化了招聘过程的繁琐,提升了招聘的质量和效率。(告诉中间人,中间人自动给你须要、合适的对象)编程
这其中一个很重要的变化就是公司HR将繁琐的招聘寻访人才的过程转移至了第三方,也就是猎头。相对比而言,IoC在这里充当了猎头的角色,开发者即公司HR,而对象的控制权就至关于人才寻访过程当中的一系列工做。编程语言
一句话,在java中,将对象的建立与对象的使用分离开,经过依赖注入(DI)的方式达到对象控制反转(IOC)的目的。本来须要本身作建立对象、维护各个对象关系,如今统一交给统一专业的人或服务处理。测试
建立对象等操做就是你对对象的控制权,把控制权交给三方,这就是 控制反转(IOC) 的意思。this
当须要某个对象的时候,三方将合适的对象给你,这个就是 依赖注入(DI) 的意思。spa
ApplicationContext 扩展了BeanFactory后的结果,BeanFactory为bean对象的出生地。.net
定义一个bean对象长什么样子,在spring中叫BeanDefinition。如何初始化一个BeanDefinition,spring中经常使用三种方式定义:xml文件、注解扫描、java代码.
Bean的完整生命周期经历了各类方法调用,这些方法能够划分为如下几类:
实现经过代理类完成不一样操做。
package bean;
public interface Animal {
public void sayName();
}
复制代码
package bean;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
@Primary
@Component
public class Cat implements Animal {
public void sayName() {
System.out.println("this is cat");
}
}
复制代码
package bean;
import org.springframework.stereotype.Component;
@Component
public class Dog implements Animal {
public void sayName() {
System.out.println("this is dog");
}
}
复制代码
package bean;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class AnimalProxy implements Animal {
@Autowired
private Animal animal;
public void sayName(){
animal.sayName();
}
}
复制代码
package bean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
/** * User: Rudy Tan * Date: 2017/11/24 */
@Configuration
@ComponentScan
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class AnimalConfig {
}
复制代码
import bean.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AnimalConfig.class)
public class AppTest {
@Autowired
private AnimalProxy animalProxy;
@Test
public void testBeanLoad(){
animalProxy.sayName();
}
}
复制代码
说明:
总结,多人我的或多个类协助处理问题或实现某个功能,总得要找个领头的来管理这些关系吧。