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")); }
}

最新文章

  1. 带有runat="server" 的服务器控件通过 ClientID 获取Id
  2. NotSerializableException解决方法
  3. UDP编程中client和server中使用recvfrom和sendto的区别
  4. 扩展KMP --- HDU 3613 Best Reward
  5. phpstom 实用laravel 需要附加的 命令
  6. 使用Div+CSS布局设计网站的优点
  7. leetcode@ [354] Russian Doll Envelopes (Dynamic Programming)
  8. UNIX标准化及实现之UNIX标准化、UNIX系统实现、标准和实现的关系以及ISO C标准头文件
  9. hadoop错误Operation category READ is not supported in state standby
  10. -AC自动机-题表
  11. SQL语言学习-数据操纵语言
  12. WCF创建到使用到发布
  13. HDU-Billboard-2795(线段树)
  14. why TCP guarentee delivery?
  15. html标签详解(1)
  16. ASP.NET Core JWT认证授权介绍
  17. git克隆源码时提示fatal: HTTP request failed怎么办?
  18. Gulp应用场景
  19. python使用(三)
  20. 搭建自己的挂Q平台

热门文章

  1. 用where导致group by分组字段的索引失效
  2. python基础之数据类型操作补充,集合及其操作,深浅拷贝
  3. Spring Cloud系列之客户端请求带“Authorization”请求头,经过zuul转发后丢失了
  4. Hadoop---Java-API对HDFS的操作
  5. 验证ip地址正则
  6. Python3+pdfminer+jieba+wordcloud+matplotlib生成词云(以深圳十三五规划纲要为例)
  7. Spring控制反转(依赖注入)的最简单说明
  8. windows启动/禁用telnet/IIS/ftp/IE等服务
  9. 把旧系统迁移到.Net Core 2.0 日记(2) - 依赖注入/日志NLog
  10. sql中,如何获取一个数的整数部分和余数部分