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. ssh repo ----> struts+hibernate+spring( jar包和源码)各版本下载链接

    struts http://archive.apache.org/dist/struts/ hibernate http://hibernate.org/orm/releases/5.0/ sprin ...

  2. Jquery常用的一些事件 keyup focus

    (1)keyup 事件能在用户每次松开按键时触发,实现即时提醒: (2)focus 事件能在元素得到焦点的时候触发,也可以实现即时提醒. (3)为了使表单填写准确,在表单提交之前,需要对表单的必须填写 ...

  3. php算法,冒泡排序

    冒泡排序 /*** *从小到大排列 * 逻辑分析 假设数组 $arr=[a,b,c,d]; * 总数=4; * 比较对象 第几个元素 比较次数 * a 1 3 * b 2 2 * c 3 1 **/ ...

  4. Android BottomNavigationBar底部导航控制器的使用(包含默认postion的设置)

    转载请标明出处:http://blog.csdn.net/u010046908/article/details/50962081本文出自:[李东的博客] 最近Google在自己推出的Material ...

  5. 『MXNet』第七弹_多GPU并行程序设计

    资料原文 一.概述思路 假设一台机器上有个GPU.给定需要训练的模型,每个GPU将分别独立维护一份完整的模型参数. 在模型训练的任意一次迭代中,给定一个小批量,我们将该批量中的样本划分成份并分给每个G ...

  6. hash、hashchange事件

    1.hash即URL中"#"字符后面的部分. ①使用浏览器访问网页时,如果网页URL中带有hash,页面就会定位到id(或name)与hash值一样的元素的位置: ②hash还有另 ...

  7. 创建springboot的聚合工程(一)

    比起传统复杂的单体工程,使用Maven的多模块配置,可以帮助项目划分模块,鼓励重用,防止POM变得过于庞大,方便某个模块的构建,而不用每次都构建整个项目,并且使得针对某个模块的特殊控制更为方便.接下来 ...

  8. 把springboot的项目打包运行指南

    受到传统mvc模式的开发影响,多数人都会想到把springboot项目打成war包在服务器容器里运行,笔者试过很多种方法打成war包部署tomcat上运行.运行成功但是怎么也访问不了,一直报404的错 ...

  9. php并发

    bool flock ( int handle, int operation [, int &wouldblock] );flock() 操作的 handle 必须是一个已经打开的文件指针.o ...

  10. Python错误调试-raise、assert

    raise: raise语句手工引发一个异常:,这样做程序不会因异常而终止,而是运行报错 1 "raise" [expression ["," expressi ...