class Main {
interface Language {
void print(String s);
}
static class Java implements Language{
public void print(String x) {
System.out.println("System.out.print(\""+ x +"\")");
}
}
static class Coder {
private Language lang = new Java();
public void helloWorld() {
lang.print("hello world");
}
}
public static void main(String[] args) {
Coder coder = new Coder();
coder.helloWorld();
}
}
void print(String s);
}
static class Java implements Language{
public void print(String x) {
System.out.println("System.out.print(\""+ x +"\")");
}
}
static class Coder {
private Language lang;
public void setLang(Language lang) {
this.lang = lang;
}
public void helloWorld() {
lang.print("hello world");
}
}
public static void main(String[] args) {
Coder coder = new Coder();
Language java = new Java();
coder.setLang(java);
coder.helloWorld();
}
public void print(String x) {
System.out.println("Console.Write(\""+ x +"\")");
}
}
public static void main(String[] args) {
Coder coder = new Coder();
Language csharp = new CSharp();
coder.setLang(csharp);
coder.helloWorld();
}
关注微信公众号:Java技术栈,在后台回复:spring,可以获取我整理的 N 篇最新 Spring 教程,都是干货。
interface Language {
void print(String s);
}
class Java implements Language{
public void print(String x) {
System.out.println("System.out.print(\""+ x +"\")");
}
}
class CSharp implements Language{
public void print(String x) {
System.out.println("Console.Write(\""+ x +"\")");
}
}
class Coder {
private Language lang;
public void setLang(Language lang) {
this.lang = lang;
}
public Language getLang() {
return lang;
}
public void helloWorld() {
lang.print("hello world");
}
}
依赖关系将由XML配置实现
"1.0" encoding="utf-8"
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="java" class="Java">
</bean>
<bean id="csharp" class="CSharp">
</bean>
<bean id="coder" class="Coder">
<property name="lang" ref="csharp"></property>
</bean>
</beans>
xml version=创建Coder对象的代码变为
public static void main(String[] args) {
ApplicationContext context = new FileSystemXmlApplicationContext("applicationContext.xml");
Coder coder = (Coder) context.getBean("coder");
coder.helloWorld();
}
@Aspect
public class Logger {
@Before("execution(* controller.Default.*(..))")
public void before(JoinPoint join){}
@After("execution(* controller.Default.*(..))")
public void after(){}
@AfterReturning("execution(* controller.Default.*(..))")
public void afterReturning() {}
@AfterThrowing("execution(* controller.Default.*(..))")
public void afterThrowing(){}
@Around("execution(* controller.Default.*(..))")
public void around(ProceedingJoinPoint jp) {}
}
@Before注解
@After注解
@AfterReturning注解
@AfterThrowing注解
@Around注解
@Around("execution(* controller.Default.*(..))")
public void around(ProceedingJoinPoint jp) {
try {
System.out.println("before");
jp.proceed();
System.out.println("after");
} catch (Throwable e) {
System.out.println(e.getMessage());
}
}
@Pointcut注解
@Aspect
public class Logger {
@Pointcut( value = "execution(* controller.Default.*(..))")
public void pointcut() {}
@Before("pointcut()")
public void before(JoinPoint join){}
@After("pointcut()")
public void after(){}
@AfterReturning("pointcut()")
public void afterReturning() {}
@AfterThrowing("pointcut()")
public void afterThrowing(){}
@Around("pointcut()")
public void around(ProceedingJoinPoint jp) {}
}
作者:Java程序媛环环
https://blog.csdn.net/Lubanjava/article/details/100084602
点击「阅读原文」和栈长学更多~