Spring 核心概念------------------

Spring  是大规模企业级框架,用户数量多,数据规模大,功能众多,业务复杂,

             性能和安全要求高   灵活多变

             Spring框架是轻量级的框架,javaEE的春天,当前主流的框架,一站式的企业应用开发框架

Spring 目标:是使现有的技术更加易用,推进代码的最佳实践

Spring 内容:IOC容器 控制反转。

                      Aop实现 面向切面编程

                      数据访问支持  1 简化jdbc/orm框架

                                             2 声明式事务

1. Spring容器的主要目的:降低业务逻辑层和其他层的耦合度(IOC)

2. Spring容器 用来创建和管理(管理对象和对象之间的关系)程序中的所有对象的实例

3. 非侵入式框架轻量级开源框架

侵入式余姚我们显示使用框架中的API才能实现某种功能。--框架强行的将功能推送给我们。

非侵入式不需要改变我们之间的编码。--我们资源从框架中获取想要的功能。

Spring的核心  ( IOC  ,AOP )

IOC(Inversin Of Control)  控制反转

在没有使用框架之前我们都是在Service 层创建dao的实例对象!控制权在service !

现在我们使用了Spring框架,创建dao的实例对象---使用Spring容器   控制权在 Spring容器!

这种控制权从程序的代码中转到Spring容器的行为就称为 IOC 控制反转   

studentDao dao=null;

public void setDao(StudentDao dao){

      this.dao=dao;

}

Spring 容器给我们创建了对象的实例,然后通过setxxx(); 把对象的实例给我们需要的地方,这个过程称为

DI依赖注入 (Dependency  Injection)

下面我们看一个Spring的小例子:

 

复制代码
public class HelloSpring {
    private String who;//定义变量who 他的值通过Spring框架进行注入

    public HelloSpring() {
    }
    public  void print(){
    System.out.println("Hello"+this.getWho()+"okokokok!");
}
    public HelloSpring(String who) {
        this.who = who;
    }

    @Override
    public String toString() {
        return "HelloSpring{" +
                "who='" + who + '\'' +
                '}';
    }

    public String getWho() {
        return who;
    }

    public void setWho(String who) {
        this.who = who;
    }
}
复制代码

applicationContext.xml   Spring容器   <!--bean 对象声明Spring创建的对象实例--> 通过Spring框架进行注入

复制代码
<?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 id="HelloSpring" class="cn.bean.HelloSpring">
                             <!-- 未声明的实例对象的类   类的位置和类名-->
     <property name="who" value="Spring hhahhahah"></property>
     <!-- name 被指定赋值的属性名set 后的 。value 赋值的内容 -->
 </bean>
    <!--bean 对象声明Spring创建的对象实例-->
复制代码
复制代码
public class SpringOneTest {
    @Test
    public void SpringHello(){
        ApplicationContext  context=new ClassPathXmlApplicationContext("ApplicationContext.xml");
        //Spring 提供的接口                  实现类                                                配置文件路径
        HelloSpring  helloSpring = (HelloSpring)context.getBean("HelloSpring");//id
        helloSpring.print();

    }
}
复制代码

运行结果

 

posted @ 2017-10-16 22:50  花落知到啥  阅读(158)  评论(0编辑  收藏  举报