Spring入门

什么是Spring1

Spring是一个轻量级控制反转(IoC)和面向切面(AOP)的容器框架。

  • 轻量级:完整Spring框架的大小只有一兆多,而Spring处理所需要的开销也是很小的。
  • 控制反转:Spring容器在对象初始化时不等对象请求就主动将依赖传递给它。
  • 面向切面:Spring提供了面向切面编程的丰富支持,允许通过分离应用的业务逻辑与系统级服务进行内聚性的开发。

第一个程序

首先,创建一个Java的maven项目

添加如下的依赖。

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.2.7.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.7.RELEASE</version>
        </dependency>
        
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

随便创建一个类,写上方法,然后再在配置文件中注册

如果是用idea的话,在添加依赖之后在创建xml文件的选项中会自动多一个创建spring配置文件的选项,选他就对了,配置文件名字随意。

package com.modevil.bean;

/**
 * @author modev
 * @date 2020/10/13  8:59
 */
public class User {

    public void hello() {
        System.out.println("hello word!!!");
    }
}
<?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="user" class="com.modevil.bean.User">
    </bean>
</beans>

最后,敲出来测试的代码

    @Test
    public void test() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("ApplicationConfig.xml");

        User user = applicationContext.getBean("user", User.class);

        user.hello();
    }

运行就可以看到结果了。
在这里插入图片描述


  1. 来自百度百科 ↩︎

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值