Initial commit

This commit is contained in:
2025-11-24 23:02:23 +08:00
commit 11eaa86967
21 changed files with 355 additions and 0 deletions

38
.gitignore vendored Normal file
View File

@@ -0,0 +1,38 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

8
.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,8 @@
# 默认忽略的文件
/shelf/
/workspace.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

7
.idea/encodings.xml generated Normal file
View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>

14
.idea/misc.xml generated Normal file
View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

4
.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings" defaultProject="true" />
</project>

52
pom.xml Normal file
View File

@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wisely</groupId>
<artifactId>higtlight_spring4</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.5</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,13 @@
package com.wisely;
import com.wisely.highlight_spring4.ch1.di1.DiMain1;
import com.wisely.highlight_spring4.ch1.di2.DiMain2;
import com.wisely.highlight_spring4.ch1.di3.DiMain3;
public class Main {
public static void main(String[] args) {
DiMain1.main1();
DiMain2.main2();
DiMain3.main3();
}
}

View File

@@ -0,0 +1,9 @@
package com.wisely.highlight_spring4.ch1.di1;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.wisely.highlight_spring4.ch1.di1")
public class DiConfig {
}

View File

@@ -0,0 +1,13 @@
package com.wisely.highlight_spring4.ch1.di1;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class DiMain1 {
public static void main1(){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DiConfig.class);
UseFunctionService useFunctionService = context.getBean(UseFunctionService.class);
System.out.println(useFunctionService.sayHello("di"));
context.close();
}
}

View File

@@ -0,0 +1,11 @@
package com.wisely.highlight_spring4.ch1.di1;
import org.springframework.stereotype.Service;
@Service
public class FunctionService {
public String sayHello(String word) {
return "Hello " + word + " !";
}
}

View File

@@ -0,0 +1,16 @@
package com.wisely.highlight_spring4.ch1.di1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UseFunctionService {
@Autowired
private FunctionService functionService;
public String sayHello(String word) {
return functionService.sayHello(word);
}
}

View File

@@ -0,0 +1,30 @@
package com.wisely.highlight_spring4.ch1.di2;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.wisely.highlight_spring4.ch1.di2")
public class DiConfig {
@Bean
public UseFunctionService useFunctionService() {
UseFunctionService ret = new UseFunctionService();
ret.setFunctionService(functionService());
return ret;
}
// @Bean
// public UseFunctionService useFunctionService(FunctionService functionService) {
// UseFunctionService ret = new UseFunctionService();
// ret.setFunctionService(functionService);
// return ret;
// }
@Bean
public FunctionService functionService() {
System.out.println("functionService()");
return new FunctionService();
}
}

View File

@@ -0,0 +1,17 @@
package com.wisely.highlight_spring4.ch1.di2;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class DiMain2 {
public static void main2(){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DiConfig.class);
UseFunctionService useFunctionService = context.getBean(UseFunctionService.class);
FunctionService functionService = context.getBean(FunctionService.class);
System.out.println(useFunctionService.sayHello("di"));
System.out.println(useFunctionService.getFunctionService().equals(functionService));
System.out.println(useFunctionService.getFunctionService());
System.out.println(functionService);
context.close();
}
}

View File

@@ -0,0 +1,8 @@
package com.wisely.highlight_spring4.ch1.di2;
public class FunctionService {
public String sayHello(String word) {
return "Hello " + word + " !";
}
}

View File

@@ -0,0 +1,18 @@
package com.wisely.highlight_spring4.ch1.di2;
public class UseFunctionService {
private FunctionService functionService;
public FunctionService getFunctionService() {
return functionService;
}
public void setFunctionService(FunctionService functionService) {
this.functionService = functionService;
}
public String sayHello(String word) {
return functionService.sayHello(word);
}
}

View File

@@ -0,0 +1,11 @@
package com.wisely.highlight_spring4.ch1.di3;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Action {
String name();
}

View File

@@ -0,0 +1,11 @@
package com.wisely.highlight_spring4.ch1.di3;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@ComponentScan("com.wisely.highlight_spring4.ch1.di3")
@EnableAspectJAutoProxy
public class AopConfig {
}

View File

@@ -0,0 +1,13 @@
package com.wisely.highlight_spring4.ch1.di3;
import org.springframework.stereotype.Service;
@Service
public class DemoAnnotationService {
@Action(name = "注解式拦截的add操作")
public void add() {
System.out.println("DemoAnnotationService add");
}
}

View File

@@ -0,0 +1,11 @@
package com.wisely.highlight_spring4.ch1.di3;
import org.springframework.stereotype.Service;
@Service
public class DemoMethodService {
public void add(){
System.out.println("DemoMethodService add");
}
}

View File

@@ -0,0 +1,16 @@
package com.wisely.highlight_spring4.ch1.di3;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class DiMain3 {
public static void main3(){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AopConfig.class);
DemoAnnotationService demoAnnotationService = context.getBean(DemoAnnotationService.class);
demoAnnotationService.add();
DemoMethodService demoMethodService = context.getBean(DemoMethodService.class);
demoMethodService.add();
context.close();
}
}

View File

@@ -0,0 +1,35 @@
package com.wisely.highlight_spring4.ch1.di3;
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;
@Aspect
@Component
public class LogAspect {
@Pointcut("@annotation(com.wisely.highlight_spring4.ch1.di3.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() + " method :" + method.getName());
}
@Before("execution(* com.wisely.highlight_spring4.ch1.di3.DemoMethodService.*(..))")
public void before(JoinPoint joinPoint) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
System.out.println("方法规则式拦截: " + method.getName());
}
}