Spring随笔

最近重温了Spring,随手记一下自己认为要点。
1.Spring用的核心jar包:core, beans, context, expression, logging.

2.IoC:Inverse of Control 反转控制

原本程序中创建对象是要new的,现在对象的控制权被反转到了Spring框架,所以叫IoC.

DI:Dependency Injection 依赖注入
DI是从另一个角度考虑,与IoC是一样的东西,即:在Spring框架负责创建Bean对象时,动态地将依赖对象注入到Bean组件。

我的GitHub上有个最原始的例子:戳这里

下面来一点点分析:
目录结构

pom.xml中,引入上述说的4个Spring相关包和1个日志包。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ahua.test1</groupId>
    <artifactId>springDemo1</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.3.14.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.3.14.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.14.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>4.3.14.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>RELEASE</version>
        </dependency>


    </dependencies>



</project>

再创建一个service接口和实现类

关于IoC:
接下来利用Spring IoC控制反转创建实例,具体步骤为:
(1)在resources文件夹下配置文件beans.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对象-->
    <bean id="userService" class="com.ahua.service.UserServiceImpl">
        <!-- 依赖注入数据,调用属性的set方法 -->
        <property name="name" value="zhangsan"/>
    </bean>
</beans>

(2)创建一个测试类,从beans.xml获取bean

import com.ahua.service.IUserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class test1 {
    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        IUserService userService = (IUserService)context.getBean("userService");
        userService.add();
        System.out.println(userService);
    }
}

由上述分析可得:
beans.xml中做的事情是,将UserServiceImpl给放到了Spring里面,当要利用这个对象的时候,不需要像以前一样去new(即创建对象),而是直接交给Spring框架来进行管理,IoC的意思就是对象控制权被反转到了Spring框架。

与直接new的区别:
这里就懒得写代码举例了,直接说明:
(1)如果是用new,则每次new的都是新的对象;
(2)如果用的是Spring框架来进行管理,每次拿到的对象都是同一个,而不是新的对象。

关于DI:
在IUserService中设置一个name属性,并提供其get/set方法,在beans.xml中通过property去注入,如下所示:

<bean id="userService" class="com.ahua.service.UserServiceImpl">
        <!-- 依赖注入数据,调用属性的set方法 -->
        <property name="name" value="zhangsan"/>
</bean>

这一句就等价于

IUserService service = new UserServiceImpl();
service.setName("zhangsan");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值