SpringMVC学习日记 1.Spring框架

SpringMVC学习日记 1.Spring框架

Spring简介
Spring框架是一个开源框架,由Rod Johnson组织和开发,生产目的在于简化企业级应用的开发。
主要特性
  • 非侵入(no-invasive)框架。
  • 提供一种一致的,在任何环境下都可以使用的编程模型。
  • 提高代码的重用性。
  • 使系统架构更容易选择。
  • 不重复造轮子。
核心技术
  • 反向控制(IOC)和依赖注入
        控制反转和依赖注入在Spring环境下是等同的概念,控制反转是通过依赖注入实现的。所谓的依赖注入是容器负责创建和维护对象间的依赖关系,而不是通过对象本身负责自己创建和解决自己的依赖。
    依赖注入是为了解耦,体现“组合”的思想。即假如你希望你的一个类具有某项功能的时候,你是选择让它继承具有此功能的一个类好呢,还是组合一个具有此功能的类?继承一个类,子类与父类耦合,而组合另一个类其耦合度将大大降低。
  • 面向切面编程(AOP)
        AOP面向切面编程,相对于OOP面向对象编程。
        Spring的AOP的存在目的是为了解耦,摆脱类与类之间的依赖关系,AOP可以让一组类共享相同的行为,OOP中只能通过继承父类或实现接口,并且继承是单继承,阻碍了更多的行为添加到一组类上,AOP弥补了OOP的不足。
    Spring支持AspectJ的注解式的切面式编程
    1. 使用@Aspect声明是一个切面
    2. 使用@After、@Before、@Around定义建言(advice),可以直接将拦截规则(切点)作为参数
    3. 其中@After、@Before、@Around参数的拦截规则为切点(PointCut),为了使切点复用,可以使用@PointCut专门定义拦截规则,然后在@After、@Before、@Around的参数中调用
    4. 其中符合条件的每一个拦截处作为连接点(JoinPoint)
  • 一致抽象性
        Spring所使用的大多数框架并不是自己提供的,而是使用了现成的框架。并且对同类的框架提供接口,如基于MVC的Web框架、ORM框架等。
  • 异常处理
        在Spring中提供了统一的异常类,如数据访问层的org.springFramework.dao.DataAccessException。而且这些类实际上是RuntimeException的子类,并不需要try….catch进行捕获,因此可以使处理异常的代码最小化。
  • 资源管理
        Spring可以管理很多其他的资源,如:JDBC、JNDI、JTA等,这使得管理这些资源变得十分容易。
基于Spring的Hello World
新建maven工程

单击File -> New -> Project -> Maven

新建Maven工程

输入Maven的坐标值

输入Maven坐标

选择存储路径

选择存储路径

修改pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.chen</groupId>
    <artifactId>highlig_spring4</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
编写功能类的Bean
package com.chen.service;

import org.springframework.stereotype.Service;

/**
 * created by chen
 * on 2017/9/23 17:37
 */

//使用@Service注解申明当前FunctionService是Spring管理下的一个Bean。
//使用@Component、@Service、@Repository、@Controller是等效的,根据具体需要使用
@Service
public class FunctionService {
    public String sayHello(String message){
        return "Hello " + message + "!";
    }
}

使用@Service注解申明当前FunctionService是Spring管理下的一个Bean。使用@Component、@Service、@Repository、@Controller是等效的,根据具体需要使用

使用功能类Bean
package com.chen.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * created by chen
 * on 2017/9/23 17:41
 */

@Service
public class UseFunctionService {
    @Autowired
    private FunctionService functionService;

    public String sayString(String str){
        return functionService.sayHello(str);
    }
}
  • 使用@Service注解申明当前UseFunctionService类是Spring管理的一个Bean
  • 使用@Autowired将FunctionService的实体Bean注入到UseFunctionService中,让UseFunctionService具备FunctionService的功能,此处使用JSR-330的@JSR-250的@Resource注解是等效的。
配置类
package com.chen.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * created by chen
 * on 2017/9/23 17:43
 */
//@Configuration声明当前类是一个配置类
@Configuration
//@ComponentScan,自动扫描包名下所有只用@Service、@Component、@Repository和使用@Controller的类,并且注册为Bean
@ComponentScan("com.chen.service")
public class TestConfig {

}
运行
package com.chen.test;

import com.chen.config.TestConfig;
import com.chen.service.UseFunctionService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * created by chen
 * on 2017/9/23 17:47
 */
public class MainTest {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfig.class);//①
        UseFunctionService useFunctionService = context.getBean(UseFunctionService.class);//②
        System.out.println(useFunctionService.sayString("hahahahhahh"));
        context.close();
    }
}
  • 使用AnnotationConfigApplicationContext作为Spring容器,接受一个配置类作为参数
  • 获得申明配置的UseFunctionService
运行结果

运行结果

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值