第1章 Spring基础

来源:互联网 发布:纸鹤网络验证 编辑:程序博客网 时间:2024/05/21 22:21

第1章 Spring基础

1.1 Spring概述

1.1.1 Spring的简史
第一阶段:xml配置
第二阶段:注解配置
基本配置(如数据库配置)用xml,业务配置用注解。
第三阶段:Java配置
Java配置可以更理解配置的Bean。推荐使用Java配置。

1.1.2 Spring概述
企业级开发的一站式解决方案。所谓解决方案就是可以基于Spring解决Java EE开发的所有问题。

1.Spring的模块

2.Spring的生态
Spring Boot:使用默认开发配置来实现快速开发。
Spring Data:对主流的关系型和NoSQL数据库的支持
Spring Security:通过认证和授权保护应用
Spring Session:提供一个API及实现来管理用户会话信息
。。。

1.2 Spring项目快速搭建

1.3 Spring基础配置

1.3.1 依赖注入
1.点睛
控制翻转(IOC)和依赖注入(DI)在Spring环境下是等同的概念,控制翻转是通过依赖注入实现的。所谓依赖注入指的是容器负责创建对象和维护对象间的依赖关系。
声明Bean的注解:
@Component
@Repository
@Service
@Controller
注入Bean的注解:
@Autowired:Spring提供的注解
@Inject:JSR-330提供的注解
@Resource:JSR-250提供的注解

2.示例
演示基于注解的Bean的初始化和依赖注入

package com.wisely.highlight_spring4.ch1.di;import org.springframework.stereotype.Component;import org.springframework.stereotype.Controller;import org.springframework.stereotype.Repository;import org.springframework.stereotype.Service;//@Service @Repository//@Controller//@Component//使用Repository,Service,Controller,Component是等效的public class FunctionService {    public String sayHello(String word){        return "Hello " + word +" !";     }}
package com.wisely.highlight_spring4.ch1.di;import javax.annotation.Resource;import javax.inject.Inject;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;@Service //1public class UseFunctionService {    //@Autowired     //@Inject    @Resource    //Autowired,Inject,Resource是等效的,使用Inject需要额外加入javax.inject依赖。    FunctionService functionService;    public String SayHello(String word){        return functionService.sayHello(word);    }}
package com.wisely.highlight_spring4.ch1.di;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration //声明当前类是一个配置类@ComponentScan("com.wisely.highlight_spring4.ch1.di") //自动扫描包下所有使用@Servcie,@Component,@Repository,@Controller的类,//并注册为Beanpublic class DiConfig {}
package com.wisely.highlight_spring4.ch1.di;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class Main {    public static void main(String[] args) {         AnnotationConfigApplicationContext context =                    new AnnotationConfigApplicationContext(DiConfig.class);          //使用AnnotationComfigApplicationContext作为Spring容器,接受输入一个配置类作为参数         UseFunctionService useFunctionService = context.getBean(UseFunctionService.class); //2         System.out.println(useFunctionService.SayHello("world"));         context.close();    }}

1.3.2 Java配置
1.点睛
Java配置是通过@Configuration和@Bean来实现的。
*@Configuration声明类是一个配置类,相当于一个Spring配置的xml文件。
*@Bean注解在方法上,声明当前方法的返回值为一个Bean。

*全局配置使用Java配置(如数据库相关配置、MVC相关配置),业务Bean的配置使用注解配置。

2.示例
演示简单的Java配置。

package com.wisely.highlight_spring4.ch1.javaconfig;//1public class FunctionService {    public String sayHello(String word){        return "Hello " + word +" !";     }}
package com.wisely.highlight_spring4.ch1.javaconfig;import com.wisely.highlight_spring4.ch1.javaconfig.FunctionService;//1public class UseFunctionService {    //2    FunctionService functionService;    public void setFunctionService(FunctionService functionService) {        this.functionService = functionService;    }    public String SayHello(String word){        return functionService.sayHello(word);    }}
package com.wisely.highlight_spring4.ch1.javaconfig;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configuration //1public class JavaConfig {    @Bean //2    public FunctionService functionService(){        return new FunctionService();    }    @Bean     public UseFunctionService useFunctionService(){        UseFunctionService useFunctionService = new UseFunctionService();        useFunctionService.setFunctionService(functionService()); //3        return useFunctionService;    }//  @Bean //  public UseFunctionService useFunctionService(FunctionService functionService){//4//      UseFunctionService useFunctionService = new UseFunctionService();//      useFunctionService.setFunctionService(functionService);//      return useFunctionService;//  }}
package com.wisely.highlight_spring4.ch1.javaconfig;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class Main {    public static void main(String[] args) {         AnnotationConfigApplicationContext context =                    new AnnotationConfigApplicationContext(JavaConfig.class);          UseFunctionService useFunctionService = context.getBean(UseFunctionService.class);          System.out.println(useFunctionService.SayHello("java config"));         context.close();    }}

1.3.3 AOP
1.点睛
AOP面向切面编程。
Spring支持AspectJ的注解式切面编程。
(1) 使用@Aspect申明是一个切面
(2)使用@After、@Before、@Around定义建言(advice),可直接将拦截规则(切点)作为参数。

package com.wisely.highlight_spring4.ch1.aop;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface Action {    String name();}
package com.wisely.highlight_spring4.ch1.aop;import org.springframework.stereotype.Service;@Servicepublic class DemoAnnotationService {    @Action(name="注解式拦截的add操作")    public void add(){} }
package com.wisely.highlight_spring4.ch1.aop;import org.springframework.stereotype.Service;@Servicepublic class DemoMethodService {    public void add(){}}
package com.wisely.highlight_spring4.ch1.aop;import java.lang.reflect.Method;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.aspectj.lang.reflect.MethodSignature;import org.springframework.stereotype.Component;@Aspect //1@Component //2public class LogAspect {    @Pointcut("@annotation(com.wisely.highlight_spring4.ch1.aop.Action)") //3    public void annotationPointCut(){};      @After("annotationPointCut()") //4        public void after(JoinPoint joinPoint) {            MethodSignature signature = (MethodSignature) joinPoint.getSignature();            Method method = signature.getMethod();            Action action = method.getAnnotation(Action.class);             System.out.println("注解式拦截 " + action.name()); //5        }       @Before("execution(* com.wisely.highlight_spring4.ch1.aop.DemoMethodService.*(..))") //6        public void before(JoinPoint joinPoint){            MethodSignature signature = (MethodSignature) joinPoint.getSignature();            Method method = signature.getMethod();            System.out.println("方法规则式拦截,"+method.getName());        }}
package com.wisely.highlight_spring4.ch1.aop;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.EnableAspectJAutoProxy;@Configuration@ComponentScan("com.wisely.highlight_spring4.ch1.aop")@EnableAspectJAutoProxy //1public class AopConfig {}
package com.wisely.highlight_spring4.ch1.aop;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class Main {    public static void main(String[] args) {         AnnotationConfigApplicationContext context =                    new AnnotationConfigApplicationContext(AopConfig.class); //1         DemoAnnotationService demoAnnotationService = context.getBean(DemoAnnotationService.class);         DemoMethodService demoMethodService = context.getBean(DemoMethodService.class);         demoAnnotationService.add();         demoMethodService.add();         context.close();    }}

这里写图片描述

0 0