初识Spring2021-04-19

Spring

*Spring是应用了很多优秀的设计模式,对于项目的实现,提供了优秀的解决方案;Spring是一个轻量级(低侵入)的控制反转IOC和面向切面编程(AOP)的容器框架。*好处:类与类之间的解耦性
功能强大
Spring对Dao层支持
Spring对Web支持
Spring的另外一个核心AOP
Spring的核心:实现解耦合/Spring对对象的控制
Spring对测试的支持
IOC(Inverse Of Control)概念控制反转 作用管家 管理对象
将传统创建对象的控制权,交由Spring容器中(配置文件)中来声明和实现
MVC框架中的,对象管理的大管家,实现类与类之间的解耦合
IOC的好处:解耦合
IOC实现原理:工厂设计模式+反射(无参构造方法) 配置文件+工厂类
使用Spring(IOC)管理Bean(创建对象)
创建java,并添加jar包
在这里插入图片描述
commons-logging-1.1.1.jar和log4j-1.2.17.jar添加日志时使用
首先需要配置applicationContext.xml
下面展示一些 applicationContext.xml。

// An highlighted block
var foo = 'bar';
<?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-3.2.xsd">
    <!--beans spring容器-->
    <!--beans对应一个对象  id class里面是实体类的路径  创建对象-->
    <bean id="student" class="com.rj.entity.Student"></bean>
</beans>

创建实体类 略
测试类
下面展示一些 测试类。

var foo = 'bar';
@Test
    public void method(){
        //测试
        //加载spring配置文件,创建工厂
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //根据bean id 获取对象
        Student student = (Student)context.getBean("student");
        //调用方法,或者打印输出
        System.out.println("Student ="+ student);
    }

测试图片
在这里插入图片描述
出现的错误

context.getBean(“student”)里获取的是applicationContext.xml里bean的id

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值