-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
833 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?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>cdu.zch</groupId> | ||
<artifactId>review-reflect</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<properties> | ||
<maven.compiler.source>17</maven.compiler.source> | ||
<maven.compiler.target>17</maven.compiler.target> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
</project> |
23 changes: 23 additions & 0 deletions
23
spring6/review-reflect/src/main/java/cdu/zch/reflect/SomeService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package cdu.zch.reflect; | ||
|
||
/** | ||
* @author Zch | ||
* @data 2023/6/19 | ||
**/ | ||
public class SomeService { | ||
|
||
public void doSome() { | ||
System.out.println("void doSome()..."); | ||
} | ||
|
||
public String doSome(String s) { | ||
System.out.println("String doSome(String s)..."); | ||
return s; | ||
} | ||
|
||
public String doSome(String s, int i) { | ||
System.out.println("String doSome(String s, int i)..."); | ||
return s + i; | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
spring6/review-reflect/src/main/java/cdu/zch/reflect/Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package cdu.zch.reflect; | ||
|
||
/** | ||
* @author Zch | ||
* @data 2023/6/19 | ||
**/ | ||
public class Test { | ||
|
||
public static void main(String[] args) { | ||
// 不适用反射机制调用这些方法 | ||
SomeService service = new SomeService(); | ||
service.doSome(); | ||
/** | ||
* 分析:嗲用一个方法,当中含有几个要素?四要素 | ||
* 第一:调用那个对象 | ||
* 第二:嗲用哪个方法 | ||
* 第三:传什么参数 | ||
* 第四:返回什么结果 | ||
* | ||
* 即使使用反射机制调用方法,同样需要具备这四个要素 | ||
*/ | ||
String s1 = service.doSome("aaa"); | ||
System.out.println(s1); | ||
String s2 = service.doSome("bbb", 2); | ||
System.out.println(s2); | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
spring6/review-reflect/src/main/java/cdu/zch/reflect/Test2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package cdu.zch.reflect; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
/** | ||
* @author Zch | ||
* @data 2023/6/19 | ||
**/ | ||
public class Test2 { | ||
|
||
public static void main(String[] args) throws Exception{ | ||
// 使用反射机制调用方法 | ||
// 1. 获取类 | ||
Class<?> clazz = Class.forName("cdu.zch.reflect.SomeService"); | ||
// 2. 获取方法 | ||
Method doSome = clazz.getDeclaredMethod("doSome", String.class, int.class); | ||
// 3. 调用方法 | ||
// 四要素:哪个对象,哪个方法,传什么参数,返回什么 | ||
// obj 要素,哪个对象 | ||
// doSome 哪个方法 | ||
// ”aaa“ 传参数 | ||
// returnValue 返回 | ||
Object obj = clazz.newInstance(); | ||
Object returnValue = doSome.invoke(obj, "aaa", 111); | ||
System.out.println(returnValue); | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
spring6/review-reflect/src/main/java/cdu/zch/reflect/Test3.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package cdu.zch.reflect; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.Method; | ||
|
||
/** | ||
* @author Zch | ||
* @data 2023/6/19 | ||
**/ | ||
public class Test3 { | ||
|
||
public static void main(String[] args) throws Exception{ | ||
// 获取类 | ||
Class<?> clazz = Class.forName("cdu.zch.reflect.SomeService"); | ||
// 获取方法 | ||
Method doSome = clazz.getDeclaredMethod("doSome"); | ||
// 创建对象 | ||
Constructor<?> constructor = clazz.getDeclaredConstructor(); | ||
Object o = constructor.newInstance(); | ||
// Object obj = clazz.newInstance(); | ||
// 调用方法 | ||
doSome.invoke(o); | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
spring6/review-reflect/src/main/java/cdu/zch/reflect/Test4.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package cdu.zch.reflect; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
/** | ||
* @author Zch | ||
* @data 2023/6/19 | ||
**/ | ||
public class Test4 { | ||
|
||
public static void main(String[] args) throws Exception{ | ||
// 已知类名 | ||
String className = "cdu.zch.reflect.User"; | ||
// 已知属性名 | ||
String propertyName = "age"; | ||
|
||
// 通过反射机制给User对象的age属性赋值20岁 | ||
Class<?> clazz = Class.forName(className); | ||
Object obj = clazz.newInstance(); // 创建对象 | ||
|
||
// 根据属性名获取setter方法名 | ||
String setMethodName = "set" + propertyName.toUpperCase().charAt(0) + propertyName.substring(1); | ||
|
||
// 获取Method | ||
Method setMethod = clazz.getDeclaredMethod(setMethodName, int.class); | ||
|
||
// 调用Method | ||
setMethod.invoke(obj, 20); | ||
|
||
System.out.println(obj); | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
spring6/review-reflect/src/main/java/cdu/zch/reflect/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package cdu.zch.reflect; | ||
|
||
/** | ||
* @author Zch | ||
* @data 2023/6/19 | ||
**/ | ||
public class User { | ||
|
||
private String name; | ||
private int age; | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public int getAge() { | ||
return age; | ||
} | ||
|
||
public void setAge(int age) { | ||
this.age = age; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "User{" + | ||
"name='" + name + '\'' + | ||
", age=" + age + | ||
'}'; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module version="4"> | ||
<component name="FacetManager"> | ||
<facet type="Spring" name="Spring"> | ||
<configuration> | ||
<fileset id="fileset" name="Spring Application Context" removed="false"> | ||
<file>file://$MODULE_DIR$/spring6-006-lifecycle/src/main/resources/spring.xml</file> | ||
</fileset> | ||
</configuration> | ||
</facet> | ||
</component> | ||
</module> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
Bean的生命周期五步: | ||
- 第一步:实例化Bean | ||
- 第二步:Bean属性赋值 | ||
- 第三步:初始化Bean | ||
- 第四步:使用Bean | ||
- 第五步:销毁Bean | ||
|
||
Bean生命周期七步: | ||
- 第一步:实例化Bean | ||
- 第二步:Bean属性赋值 | ||
- **第三步:执行”Bean后处理器“的before方法** | ||
- 第四步:初始化Bean | ||
- **第五步:指定”Bean后处理器“的after方法** | ||
- 第六步:使用Bean | ||
- 第七步:销毁Bean | ||
|
||
Bean生命周期十步: | ||
- 第一步:实例化Bean | ||
- 第二步:Bean属性赋值 | ||
- **第三步:检查Bean是否实现了Aware相关接口,如果实现了则调用相关接口** | ||
- 第四步:执行”Bean后处理器“的before方法 | ||
- **第五步:检查Bean是否实现了InitializingBean,如果实现了,调用相关方法** | ||
- 第六步:初始化Bean | ||
- 第七步:指定”Bean后处理器“的after方法 | ||
- 第八步:使用Bean | ||
- **第九步:检查Bean是否实现了DisposableBean方法,如果实现了,调用相关方法** | ||
- 第十步:销毁Bean | ||
|
||
十步中添加的三个步骤都是检查这个Bean是否实现了某些特定的接口,实现了这些接口,Spring就会调用某些特定的方法 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?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>cdu.zch</groupId> | ||
<artifactId>spring6-006-lifecycle</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
|
||
<properties> | ||
<maven.compiler.source>17</maven.compiler.source> | ||
<maven.compiler.target>17</maven.compiler.target> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<dependencies> | ||
<!-- 加入spring-context依赖 | ||
引入context依赖之后,就将基础依赖引入了 | ||
如果想使用spring的jdbc,或者tx,还需要其他依赖 | ||
--> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-context</artifactId> | ||
<version>6.0.8</version> | ||
</dependency> | ||
<!-- junit--> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.13.2</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<!-- 启动Log4j2--> | ||
<dependency> | ||
<groupId>org.apache.logging.log4j</groupId> | ||
<artifactId>log4j-core</artifactId> | ||
<version>2.19.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.logging.log4j</groupId> | ||
<artifactId>log4j-slf4j2-impl</artifactId> | ||
<version>2.19.0</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
Oops, something went wrong.