Spring--初识

目的:解决企业级应用开发的复杂性

理念:使现有技术更容易使用

优点:
  1. 开源免费
  2. 轻量级(本身很小),非入侵式(使用他不会改变原有代码)的框架
  3. 控制反转(IOC),面向切面(AOP)
  4. 支持事务,对框架整合的支持
    **总结:**一个轻量级的控制翻转和面向切面编程的框架
IOC:
  1. 控制翻转:是一种通过描述(XML或注解)并通过第三方去生产或获取特定对象的方式,DI(依赖注入)是实现它的方法
  2. 使用Set()注入后,程序不在具有主动性,它只能被动的接受对象
    3.从本质上解决了问题,程序员不在去管理对象的创建,系统耦合性大大降低,可以更加专注与业务的实现
具体实现
1.配置文件
<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--使用Spring来创建对象;
    bean相当于new 一个对象
    id==变量名   class=全路径,new的对象
    property:相当于给属性赋值
    -->
    <bean id="hello" class="com.liye.pijo.Hello">
        <property name="str" value="Spring"/>
    </bean>
</beans>
2.实现的类
public class Hello {
    private String str;

    public String getStr() {
        return str;
    }

    public void setStr(String str) {
        this.str = str;
    }

    @Override
    public String toString() {
        return "Hello{" +
                "str='" + str + '\'' +
                '}';
    }
}
3.测试
public class MyTest {
    public static void main(String[] args) {
        //获取Spring上下文对象
        ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
        //我们的对象都在Spring管理
        Hello hello= (Hello) context.getBean("hello");
        System.out.println(hello.toString());
    }
}
4.运行结果
Hello{str='Spring'}
IOC:对象由Spring来创建,管理和装配;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值