【Spring】框架概述

Spring简介

     Spring框架是个轻量级的Java EE框架。所谓轻量级,是指不依赖于容器就能运行的。Struts、Hibernate也是轻量级的。 
  轻量级框架是相对于重量级框架而言的,重量级框架必须依赖特定的容器,例如EJB框架就必须运行在Glassfish、JBoss等支持EJB的容器中,而不能运行在Tomcat中。——《Java Web整合开发 王者归来》 
  Spring以IoC、AOP为主要思想,其中IoC,Inversion of Control 指控制反转或反向控制。在Spring框架中我们通过配置创建类对象,由Spring在运行阶段实例化、组装对象。AOP,Aspect Oriented Programming,面向切面编程,其思想是在执行某些代码前执行另外的代码,使程序更灵活、扩展性更好,可以随便地添加、删除某些功能。Servlet中的Filter便是一种AOP思想的实现。 
  Spring同时也是一个“一站式”框架,即Spring在JavaEE的三层架构[表现层(Web层)、业务逻辑层(Service层)、数据访问层(DAO层)]中,每一层均提供了不同的解决技术。如下:

  • 表现层(Web层):Spring MVC
  • 业务逻辑层(Service层):Spring的IoC
  • 数据访问层(DAO层):Spring的jdbcTemplate

导入Spring框架(maven)

导入jar包:

082339_dVKu_3867381.png

注:

ModuleDescription
coreThis is the main module that you will need for every Spring application. In this JAR file, you will find all the classes that are shared among all other Spring modules (for example, classes for accessing configuration files). Also, in this JAR, you will find selections of extremely useful utility classes that are used throughout the Spring codebase and that you can use in your own application.
beansThis module contains all the classes for supporting Spring’s manipulation of Spring beans. Most of the classes here support Spring’s bean factory implementation. For example, the classes required for processing the Spring XML configuration file and Java annotations are packed into this module.
contextThis module contains classes that provide many extensions to Spring Core.You will find that all classes need to use Spring’s ApplicationContext feature, along with classes for Enterprise JavaBeans (EJB), Java Naming and Directory Interface (JNDI), and Java Management Extensions (JMX) integration. Also contained in this module are the Spring remoting classes, classes for integration with dynamic scripting languages (for example, JRuby, Groovy, and BeanShell), JSR-303 (Bean Validation), scheduling and task execution, and so on.
expressionThis module contains all support classes for Spring Expression Language (SpEL).

maven:

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.0.6.RELEASE</version>
    </dependency>
</dependencies>

Spring HelloWorld

新建maven工程,并引入Spring框架依赖。

082822_RC3X_3867381.png

HelloWorld.java

public class HelloWorld {

    private String name;

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

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

    public HelloWorld() {
    }
}

Main.java

public class Main {
    public static void main(String[] args) {

        //创建HelloWorld的一个对象
        HelloWorld helloWorld = new HelloWorld();
        //为name属性赋值
        helloWorld.setName("wusuiwei");
        //调用hello方法
        helloWorld.hello();


        // 1.创建Spring的IOC容器对象
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        //2.从IOC容器中获取Bean实例
        HelloWorld bean = context.getBean(HelloWorld.class);

        //3.调用hello方法
        bean.hello();
    }
}

Beans.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.xsd">

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

运行结果:

081513_ATRr_3867381.png

 

转载于:https://my.oschina.net/u/3867381/blog/1819345

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值