1、DI

package com.zhen.highlights_spring4.ch1.di;

import org.springframework.stereotype.Service;

/**
* @author zhen
* @Date 2018/6/12 10:05
*/
@Service
public class FunctionService {
public String sayHello(String word){
return "Hello " + word + " !";
}
}
package com.zhen.highlights_spring4.ch1.di; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; /**
* @author zhen
* @Date 2018/6/12 10:07
*/
@Service
public class UseFunctionService { @Autowired
private FunctionService functionService; public String sayHello(String word){
return functionService.sayHello(word);
}
}
package com.zhen.highlights_spring4.ch1.di; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; /**
* @author zhen
* @Date 2018/6/12 10:09
*/
@Configuration
@ComponentScan("com.zhen.highlights_spring4.ch1.di")
public class DiConfig {
}
package com.zhen.highlights_spring4.ch1.di; import org.springframework.context.annotation.AnnotationConfigApplicationContext; /**
* @author zhen
* @Date 2018/6/12 10:10
*/
public class Main {
public static void main(String[] args){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DiConfig.class); UseFunctionService useFunctionService = context.getBean(UseFunctionService.class); System.out.println(useFunctionService.sayHello("di")); context.close();
}
}

2、AOP

package com.zhen.highlights_spring4.ch1.aop;

import java.lang.annotation.*;

/**
* @author zhen
* @Date 2018/6/12 10:45
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Action {
String name();
}
package com.zhen.highlights_spring4.ch1.aop; import org.springframework.stereotype.Service; /**
* @author zhen
* @Date 2018/6/12 10:48
*/
@Service
public class DemoAnnotationService {
@Action(name = "注解式拦截的aop操作")
public void add(){}
}
package com.zhen.highlights_spring4.ch1.aop; import org.springframework.stereotype.Service; /**
* @author zhen
* @Date 2018/6/12 10:49
*/
@Service
public class DemoMethodService {
public void add(){}
}
package com.zhen.highlights_spring4.ch1.aop; 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; import java.lang.reflect.Method; /**
* @author zhen
* @Date 2018/6/12 10:54
*/
@Aspect
@Component
public class LogAspect { @Pointcut("@annotation(com.zhen.highlights_spring4.ch1.aop.Action)")
public void annotationPointCut(){} @After("annotationPointCut()")
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());
} @Before("execution(* com.zhen.highlights_spring4.ch1.aop.DemoMethodService.*(..))")
public void before(JoinPoint joinPoint){
MethodSignature signature = (MethodSignature)joinPoint.getSignature();
Method method = signature.getMethod();
System.out.println("方法规则式拦截 " + method.getName());
}
}
package com.zhen.highlights_spring4.ch1.aop; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy; /**
* @author zhen
* @Date 2018/6/12 11:31
*/
@Configuration
@ComponentScan("com.zhen.highlights_spring4.ch1.aop")
@EnableAspectJAutoProxy
public class AopConfig {
}
package com.zhen.highlights_spring4.ch1.aop; import org.springframework.context.annotation.AnnotationConfigApplicationContext; /**
* @author zhen
* @Date 2018/6/12 11:33
*/
public class Main {
public static void main(String[] args){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AopConfig.class);
DemoAnnotationService demoAnnotationService = context.getBean(DemoAnnotationService.class);
DemoMethodService demoMethodService = context.getBean(DemoMethodService.class); demoAnnotationService.add();
demoMethodService.add(); context.close();
}
}

3.javaConfig

package com.zhen.highlights_spring4.ch1.javaconfig;

/**
* @author zhen
* @Date 2018/6/12 10:14
*/
public class FunctionService {
public String sayHello(String word){
return "Hello " + word + " !";
}
}
package com.zhen.highlights_spring4.ch1.javaconfig; /**
* @author zhen
* @Date 2018/6/12 10:15
*/ public class UseFunctionService { private FunctionService functionService; public String sayHello(String word){
return functionService.sayHello(word);
} public FunctionService getFunctionService() {
return functionService;
} public void setFunctionService(FunctionService functionService) {
this.functionService = functionService;
}
}
package com.zhen.highlights_spring4.ch1.javaconfig; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; /**
* @author zhen
* @Date 2018/6/12 10:17
*/
@Configuration
public class JavaConfig { @Bean
public FunctionService functionService(){
return new FunctionService();
} @Bean
public UseFunctionService useFunctionService(){
UseFunctionService useFunctionService = new UseFunctionService();
useFunctionService.setFunctionService(functionService());
return useFunctionService;
}
}
package com.zhen.highlights_spring4.ch1.javaconfig; import org.springframework.context.annotation.AnnotationConfigApplicationContext; /**
* @author zhen
* @Date 2018/6/12 10:22
*/
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("config")); }
}

springboot学习章节代码-spring基础的更多相关文章

  1. springboot学习章节代码-Spring MVC基础

    1.项目搭建. <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...

  2. springboot学习章节代码-spring高级话题

    1.Spring Aware(获取Spring容器的服务) hi, i am guodaxia! test.txt package com.zhen.highlights_spring4.ch3.aw ...

  3. SpringBoot学习(一)基础篇

    目录 关于Springboot Springboot优势 快速入门 关于SpringBoot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭 ...

  4. springboot学习章节-spring常用配置

    1.Scope package com.zhen.highlights_spring4.ch2.scope; import org.springframework.context.annotation ...

  5. springboot学习之授权Spring Security

    SpringSecurity核心功能:认证.授权.攻击防护(防止伪造身份) 涉及的依赖如下: <dependency> <groupId>org.springframework ...

  6. 1.1(Spring学习笔记)Spring基础(BeanFactory、ApplicationContext 、依赖注入)

    1.准备工作 下载Spring:http://repo.spring.io/libs-release-local/org/springframework/spring/    选择需要下载的版本    ...

  7. SpringBoot学习笔记:Spring Data Jpa的使用

    更多请关注公众号 Spring Data Jpa 简介 JPA JPA(Java Persistence API)意即Java持久化API,是Sun官方在JDK5.0后提出的Java持久化规范(JSR ...

  8. SpringBoot学习笔记(2) Spring Boot的一些配置

    外部配置 Spring Boot允许使用properties文件.yaml文件或者命令行参数作为外部配置 使用@Value注解,可以直接将属性值注入到你的beans中,并通过Spring的Enviro ...

  9. Springboot学习:Thymeleaf 语法基础

    详细内容见:Thymeleaf Tutorial 中文翻译,中文文档 参考: thymeleaf官方指南 新一代Java模板引擎Thymeleaf Thymeleaf基本知识 thymeleaf总结文 ...

随机推荐

  1. asp.net网站服务器搭建之从零开始

    asp.net网站服务器搭建之从零开始 一 IIS(Internet Information Services)安装:  1.选择"控制面板".  2.点"添加或删除程序 ...

  2. [LintCode] Binary Tree Level Order Traversal(二叉树的层次遍历)

    描述 给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问) 样例 给一棵二叉树 {3,9,20,#,#,15,7} : 3 / \ 9 20 / \ 15 7 返回他的分层遍历结果: [ [3] ...

  3. CentOS7 下源代码安装apache2.4

    Apache httpd 2.4 源代码安装   https://httpd.apache.org/docs/2.4/install.html   这里选用Apache2.4版本. wget http ...

  4. CentOS6.8逻辑卷管理实战

    CentOS6.8逻辑卷管理实战 要求:利用现有的四块磁盘,创建一个有两个PV组成的大小为80G的名为testvg的VG:要求PE大小为16MB, 而后在卷组中创建大小为5G的逻辑卷testlv:挂载 ...

  5. css中伪类与伪元素的区别

    一:伪类:1:定义:css伪类用于向某些选择器添加特殊效果. 伪类其实与普通的css类相类似,可以为已有的元素添加样式,但是他只有处于dom无法描述的状态下才能为文档树中的元素添加样式,所以将其称为伪 ...

  6. React Router页面传值的三种方法

    文章地址:https://blog.csdn.net/qq_23158083/article/details/68488831

  7. phpStorm中Structure窗口中的符号代表的意思

    参考:https://www.jetbrains.com/help/phpstorm/2016.3/symbols.html Icon   Description Class 类 Final clas ...

  8. Hadoop介绍-4.Hadoop中NameNode、DataNode、Secondary、NameNode、JobTracker TaskTracker

    Hadoop是一个能够对大量数据进行分布式处理的软体框架,实现了Google的MapReduce编程模型和框架,能够把应用程式分割成许多的 小的工作单元,并把这些单元放到任何集群节点上执行.在MapR ...

  9. Linux 环境下 网络IO模型

    本文讨论的背景是Linux环境下的network IO. IO发生时涉及的对象和步骤: 对于一个network IO (这里我们以read举例),它会涉及到两个系统对象,一个是调用这个IO的proce ...

  10. linux重命名所有find查找到的文件/文件夹

    一.说明 在某些时候我们想要将所有find命令查找到的文件或文件夹全都重命名,比如都加上.bak后辍 二.操作命令 find /dir -name "*pattern*" -exe ...