Spring的核心思想,总结得非常好!
Posted Java技术栈
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring的核心思想,总结得非常好!相关的知识,希望对你有一定的参考价值。
class Main {
interface Language {
void print(String s);
}
static class Java implements Language{
@Override
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();
}
}
interface Language {
void print(String s);
}
static class Java implements Language{
@Override
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();
}
static class CSharp implements Language{
@Override
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();
}
interface Language {
void print(String s);
}
class Java implements Language{
@Override
public void print(String x) {
System.out.println("System.out.print(\""+ x +"\")");
}
}
class CSharp implements Language{
@Override
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配置实现
<?xml version="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>
创建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) {}
}
https://blog.csdn.net/Lubanjava/article/details/100084602
点击「阅读原文」和栈长学更多~
以上是关于Spring的核心思想,总结得非常好!的主要内容,如果未能解决你的问题,请参考以下文章