Spring:笔记整理(1)——HelloWorld

Spring:笔记整理(1)——HelloWorld

导入JAR包:

核心Jar包

Jar包解释

Spring-core
  这个jar 文件包含Spring 框架基本的核心工具类。Spring 其它组件要都要使用到这个包里的类,是其它组件的基本核心,当然你也可以在自己的应用系统中使用这些工具类。
  外部依赖Commons Logging, (Log4J)
Spring-Beans
  这个jar 文件是所有应用都要用到的,它包含访问配置文件、创建和管理bean 以及进行Inversion ofControl / Dependency Injection(IoC/DI)操作相关的所有类。如果应用只需基本的IoC/ DI 支持,引入spring-core.jar 及spring-beans.jar 文件就可以了。外部依赖spring-core,(CGLIB)。
Spring-expression
  Spring表达式语言。
Spring-Context
  这个jar 文件为Spring 核心提供了大量扩展。可以找到使用Spring ApplicationContext特性时所需的全部类,JDNI 所需的全部类,instrumentation组件以及校验Validation 方面的相关类。
  外部依赖spring-beans, (spring-aop)。
Spring-aop
  这个jar 文件包含在应用中使用Spring 的AOP 特性时所需的类和源码级元数据支持。
  使用基于AOP 的Spring特性,如声明型事务管理(Declarative Transaction Management),也要在应用里包含这个jar包。
  外部依赖spring-core, (spring-beans,AOP Alliance, CGLIB,Commons Attributes)。

开发步骤

1.编写接口及实现类

编写输出helloworld的接口及其实现类。

/*接口*/
package Beans;
public interface HelloApi {
    public void sayHello();
}
————————————————————————————————————————————————————
/*实现类*/
package Beans;

public class HelloImp implements HelloApi{
    public void sayHello() {
        System.out.println("Hello");
    }
}

2.编写配置文件

为了让Spring IOC来找到要管理的对象,我们需要编写配置文件进行配置。

<?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="helloworld" class="Beans.HelloImp">
    </bean>

</beans>

3.实例化IOC容器并从中获取所需对象

package Beans;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class HelloWorld {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
        HelloApi helloApi = context.getBean("helloworld",HelloImp.class);
        helloApi.sayHello();
    }

}

4.运行查看效果

 


转载于:https://www.cnblogs.com/MrSaver/p/6587757.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值