spring学习—day1——spring介绍,IOC,DI,AOP

1. 什么是spring

2. spring的特性

3. IOC

4. DI

5. AOP

 

 

正文:

1. 什么是spring

  spring是一个轻量级的开源的框架。 

 

2. spring的特性

  spring的特性主要有IOC(控制反转),DI(依赖注入),AOP(面向切面编程)

 

3. IOC控制反转

  在理解控制反转前,先来理解一样,没有spring的话,我们编写代码一般是怎么做的。

  先来写一个例子:

  未使用spring前的例子

  

public class Store {
    private String storeName;
    private String storeAddress;
    private String storePhone;

    public String getStoreName() {
        return storeName;
    }

    public void setStoreName(String storeName) {
        this.storeName = storeName;
    }

    public String getStoreAddress() {
        return storeAddress;
    }

    public void setStoreAddress(String storeAddress) {
        this.storeAddress = storeAddress;
    }

    public String getStorePhone() {
        return storePhone;
    }

    public void setStorePhone(String storePhone) {
        this.storePhone = storePhone;
    }

    @Override
    public String toString() {
        return "Store{" +
                "storeName='" + storeName + '\'' +
                ", storeAddress='" + storeAddress + '\'' +
                ", storePhone='" + storePhone + '\'' +
                '}';
    }
}

先创建java bean store类。然后正常情况下,我们会怎么使用它呢?

public class App 
{
    public static void main( String[] args )
    {
        //step1: new store instance
       Store store = new Store();
       //step2: set properties value for store
       store.setStoreName("永辉超市");
       store.setStoreAddress("xx市xxx区xxx路520号");
       store.setStorePhone("123456789");
       //step3: invoke store tostring method
        System.out.println(store.toString());
    }
}

上面的代码中,就是没有使用spring的时候,我们常规情况下是怎么编写代码的。

 

spring的IOC,看过一个博主这样的介绍,觉得很形象。

IOC:控制反转,就是把创建实例的过程交给spring,就是不用再自己new 对象,只用从sping中获取,而不用自己创建

正转: 自己用对象就自己创建

反转:不在自己创建,而是从spring中直接获取

寿司例子:

我今天想吃寿司,然后呢。

没有spring的话

我要吃寿司——》自己买米,买海带,买寿司醋————》自己做————》吃

有spring的话

我要吃寿司——》寿司店(spring)——》吃

 

然后来看看,有spring的话,我们会怎么写代码呢?

 在main下面新建一个resources目录,resources下面再建一个META-INF目录。里面再建一个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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="hellostore" class="com.emma.test.Store">
        <property name="storeName" value="永辉哦"></property>
    </bean>

</beans>

 

public class App 
{
    public static void main( String[] args )
    {
        ApplicationContext ctx= new ClassPathXmlApplicationContext("META-INF/applicationContext.xml");
        Store store= (Store) ctx.getBean("hellostore");
        System.out.println(store.toString());

    }
}

综上,这就是IOC

 

   

转载于:https://www.cnblogs.com/emmaduan/p/11583363.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值