Spring(一)--Spring-HelloWorld

1:Spring是什么


1:Spring是一个开源框架;
2:Spring为简化企业级应用开发而生,使用Spring可以使简单的javaBean实现只有EJB才能实现的功能;
3:Spring是一个IOC(DI)和AOP容器框架;

2:Spring 具体描述


-轻量级:Spring是非侵入性的,基于Spring开发的应用中对象可以不依赖Spring中的API;
-依赖注入:DI---Dependency Injection、IOC;
-面向切面编程:AOP---Aspect Oriented Programming
-容器:Spring是一个容器,因为它包含并且管理应用对象的生命周期;
-框架:Spring实现了使用简单的组件配置组合成一个复杂的应用,在Spring中可以使用XML和java注解组合这些对象;
-一站式:在IOC和AOP的基础上可以整合各种企业应用的开源框架和优秀的第三方类库(实际上Spring自身也提供了展现层的SpringMVC和持久层的Spring JDBC)


Spring 模块如下

Spring framework架构图

Spring HelloWorld 目录结构如下:

目录结构

applicationContext.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-3.2.xsd">

    <!-- 配置Bean -->
    <bean id="helloWorld" class="com.spring.beans.HelloWorld">
        <property name="name" value="spring"></property>
    </bean>

</beans>

HelloWorld.java

package com.spring.beans;

public class HelloWorld {

    private String name;

    public void setName(String name) {
        this.name = name;
    }

    public void hello(){
        System.out.println("Hello " + name);
    }
}

Main.java

package com.spring.beans;

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

public class Main {

    public static void main(String[] args) {
        /*//创建HelloWorld的一个对象
        HelloWorld helloWorld = new HelloWorld();   
        //为name属性赋值
        helloWorld.setName("wanghao");
        */

        //1:创建Spring的IOC容器对象
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        HelloWorld helloWorld = (HelloWorld) ac.getBean("helloWorld");
        //2:从IOC容器中获取IOC容器对象

        //3:调用hello方法
        helloWorld.hello();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值