初始化
commit
fff8ad7df6
|
@ -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
|
|
@ -0,0 +1,8 @@
|
||||||
|
# 默认忽略的文件
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# 基于编辑器的 HTTP 客户端请求
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
|
@ -0,0 +1,11 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="accountSettings">
|
||||||
|
<option name="activeRegion" value="us-east-1" />
|
||||||
|
<option name="recentlyUsedRegions">
|
||||||
|
<list>
|
||||||
|
<option value="us-east-1" />
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -0,0 +1,11 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Encoding">
|
||||||
|
<file url="file://$PROJECT_DIR$/shiyi-lock/src/main/java" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/shiyi-lock/src/main/resources" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/shiyi-mvc/src/main/java" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/shiyi-mvc/src/main/resources" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="JpaBuddyIdeaProjectConfig">
|
||||||
|
<option name="renamerInitialized" value="true" />
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -0,0 +1,17 @@
|
||||||
|
<?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_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
|
||||||
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
|
</component>
|
||||||
|
<component name="ProjectType">
|
||||||
|
<option name="id" value="jpab" />
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -0,0 +1,124 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Palette2">
|
||||||
|
<group name="Swing">
|
||||||
|
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
|
||||||
|
<initial-values>
|
||||||
|
<property name="text" value="Button" />
|
||||||
|
</initial-values>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
||||||
|
<initial-values>
|
||||||
|
<property name="text" value="RadioButton" />
|
||||||
|
</initial-values>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
||||||
|
<initial-values>
|
||||||
|
<property name="text" value="CheckBox" />
|
||||||
|
</initial-values>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
|
||||||
|
<initial-values>
|
||||||
|
<property name="text" value="Label" />
|
||||||
|
</initial-values>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||||
|
<preferred-size width="150" height="-1" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||||
|
<preferred-size width="150" height="-1" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||||
|
<preferred-size width="150" height="-1" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
||||||
|
<preferred-size width="200" height="200" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
||||||
|
<preferred-size width="200" height="200" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
|
||||||
|
<preferred-size width="-1" height="20" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
|
||||||
|
</item>
|
||||||
|
</group>
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -0,0 +1,24 @@
|
||||||
|
<?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>org.example</groupId>
|
||||||
|
<artifactId>test1028</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<packaging>pom</packaging>
|
||||||
|
<modules>
|
||||||
|
<module>shiyi-lock</module>
|
||||||
|
<module>shiyi-mvc</module>
|
||||||
|
</modules>
|
||||||
|
|
||||||
|
<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>
|
|
@ -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
|
|
@ -0,0 +1,20 @@
|
||||||
|
<?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>
|
||||||
|
<parent>
|
||||||
|
<groupId>org.example</groupId>
|
||||||
|
<artifactId>test1028</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>shiyi-lock</artifactId>
|
||||||
|
|
||||||
|
<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>
|
|
@ -0,0 +1,54 @@
|
||||||
|
package com.shiyi.lock;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 题目:实现一个自旋锁,
|
||||||
|
* 自旋锁的好处:循环比较获取没有类似wait的阻塞
|
||||||
|
*
|
||||||
|
* 通过CAS操作完成自旋锁,A线成先进来调用myLock方法自己持有锁5秒钟,B随后进来后发现
|
||||||
|
* 当前有线程持有锁,所以只能通过自旋等待,直到A释放锁后B随后抢到
|
||||||
|
*/
|
||||||
|
public class SpinLockDemo
|
||||||
|
{
|
||||||
|
AtomicReference<Thread> atomicReference = new AtomicReference<>();
|
||||||
|
|
||||||
|
public void lock()
|
||||||
|
{
|
||||||
|
Thread thread = Thread.currentThread();
|
||||||
|
System.out.println(Thread.currentThread().getName()+"\t"+"----come in");
|
||||||
|
while (!atomicReference.compareAndSet(null,thread)){
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void unLock()
|
||||||
|
{
|
||||||
|
Thread thread = Thread.currentThread();
|
||||||
|
atomicReference.compareAndSet(thread,null);
|
||||||
|
System.out.println(Thread.currentThread().getName()+"\t"+"----task over,unLock....");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args)
|
||||||
|
{
|
||||||
|
SpinLockDemo spinLockDemo = new SpinLockDemo();
|
||||||
|
|
||||||
|
new Thread(() ->{
|
||||||
|
spinLockDemo.lock();
|
||||||
|
//暂停几秒线程
|
||||||
|
try{ TimeUnit.SECONDS.sleep(5);} catch (InterruptedException e) {e.printStackTrace();}
|
||||||
|
spinLockDemo.unLock();
|
||||||
|
},"A").start();
|
||||||
|
|
||||||
|
//暂停500毫秒,线程A先于B启动
|
||||||
|
try{TimeUnit.MILLISECONDS.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}
|
||||||
|
|
||||||
|
new Thread(() -> {
|
||||||
|
spinLockDemo.lock();
|
||||||
|
|
||||||
|
spinLockDemo.unLock();
|
||||||
|
},"B").start();
|
||||||
|
}
|
||||||
|
}
|
|
@ -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
|
|
@ -0,0 +1,28 @@
|
||||||
|
<?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>
|
||||||
|
<parent>
|
||||||
|
<groupId>org.example</groupId>
|
||||||
|
<artifactId>test1028</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>shiyi-mvc</artifactId>
|
||||||
|
|
||||||
|
<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>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.servlet</groupId>
|
||||||
|
<artifactId>servlet-api</artifactId>
|
||||||
|
<version>2.4</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,16 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
|
||||||
|
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
|
||||||
|
<!-- Your configuration elements go here -->
|
||||||
|
<display-name>shiyi-mvc</display-name>
|
||||||
|
<servlet>
|
||||||
|
<servlet-name>DispatcherServlet</servlet-name>
|
||||||
|
<servlet-class>com.enjoy.shiyi.servlet.DispatcherServlet</servlet-class>
|
||||||
|
<load-on-startup>0</load-on-startup>
|
||||||
|
</servlet>
|
||||||
|
|
||||||
|
<servlet-mapping>
|
||||||
|
<servlet-name>DispatcherServlet</servlet-name>
|
||||||
|
<url-pattern>/</url-pattern>
|
||||||
|
</servlet-mapping>
|
||||||
|
</web-app>
|
|
@ -0,0 +1,11 @@
|
||||||
|
package com.enjoy.shiyi.annaotaion;
|
||||||
|
|
||||||
|
import java.lang.annotation.*;
|
||||||
|
|
||||||
|
|
||||||
|
@Target({ElementType.FIELD})
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Documented
|
||||||
|
public @interface EnjoyAutowired {
|
||||||
|
String value() default "";
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
package com.enjoy.shiyi.annaotaion;
|
||||||
|
|
||||||
|
import java.lang.annotation.*;
|
||||||
|
|
||||||
|
|
||||||
|
@Target({ElementType.TYPE})
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Documented
|
||||||
|
public @interface EnjoyController {
|
||||||
|
String value() default "";
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
package com.enjoy.shiyi.annaotaion;
|
||||||
|
|
||||||
|
import java.lang.annotation.*;
|
||||||
|
|
||||||
|
|
||||||
|
@Target({ElementType.TYPE,ElementType.METHOD})
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Documented
|
||||||
|
public @interface EnjoyRequestMapping {
|
||||||
|
String value() default "";
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
package com.enjoy.shiyi.annaotaion;
|
||||||
|
|
||||||
|
import java.lang.annotation.*;
|
||||||
|
|
||||||
|
@Target({ElementType.PARAMETER})
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Documented
|
||||||
|
public @interface EnjoyRequestParm {
|
||||||
|
String value() default "";
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
package com.enjoy.shiyi.annaotaion;
|
||||||
|
|
||||||
|
import java.lang.annotation.*;
|
||||||
|
|
||||||
|
|
||||||
|
@Target({ElementType.TYPE})
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Documented
|
||||||
|
public @interface EnjoyService {
|
||||||
|
String value() default "";
|
||||||
|
}
|
|
@ -0,0 +1,32 @@
|
||||||
|
package com.enjoy.shiyi.controller;
|
||||||
|
|
||||||
|
import com.enjoy.shiyi.annaotaion.EnjoyAutowired;
|
||||||
|
import com.enjoy.shiyi.annaotaion.EnjoyController;
|
||||||
|
import com.enjoy.shiyi.annaotaion.EnjoyRequestMapping;
|
||||||
|
import com.enjoy.shiyi.annaotaion.EnjoyRequestParm;
|
||||||
|
import com.enjoy.shiyi.service.ShiyiService;
|
||||||
|
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
|
||||||
|
|
||||||
|
@EnjoyController
|
||||||
|
@EnjoyRequestMapping("/shiyi")
|
||||||
|
public class ShiyiController {
|
||||||
|
@EnjoyAutowired("ShiyiServiceImpl")
|
||||||
|
private ShiyiService shiyiService;
|
||||||
|
|
||||||
|
public void query(HttpServletRequest request, HttpServletResponse response, @EnjoyRequestParm("name") String name,
|
||||||
|
@EnjoyRequestParm("age") String age){
|
||||||
|
try {
|
||||||
|
PrintWriter pw = response.getWriter();
|
||||||
|
String resule = shiyiService.query(name,age);
|
||||||
|
pw.write(resule);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
package com.enjoy.shiyi.service;
|
||||||
|
|
||||||
|
|
||||||
|
public interface ShiyiService {
|
||||||
|
String query(String name,String age);
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
package com.enjoy.shiyi.service.impl;
|
||||||
|
|
||||||
|
import com.enjoy.shiyi.annaotaion.EnjoyService;
|
||||||
|
import com.enjoy.shiyi.service.ShiyiService;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@EnjoyService("ShiyiServiceImpl")
|
||||||
|
public class ShiyiServiceImpl implements ShiyiService {
|
||||||
|
@Override
|
||||||
|
public String query(String name, String age) {
|
||||||
|
|
||||||
|
return "name==="+name+" age==="+age;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,235 @@
|
||||||
|
package com.enjoy.shiyi.servlet;
|
||||||
|
|
||||||
|
import com.enjoy.shiyi.annaotaion.*;
|
||||||
|
import com.enjoy.shiyi.controller.ShiyiController;
|
||||||
|
|
||||||
|
import javax.servlet.ServletConfig;
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.ServletRequest;
|
||||||
|
import javax.servlet.ServletResponse;
|
||||||
|
import javax.servlet.http.HttpServlet;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.lang.annotation.Annotation;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
|
||||||
|
public class DispatcherServlet extends HttpServlet {
|
||||||
|
|
||||||
|
List<String> className = new ArrayList<String>();
|
||||||
|
|
||||||
|
Map<String, Object> beans = new HashMap<String,Object>();
|
||||||
|
Map<String, Object> handlerMap = new HashMap<String,Object>();
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(ServletConfig config){
|
||||||
|
//把所有的bean扫描-----> 扫描所有的class文件
|
||||||
|
scanPackage("com.enjoy"); //写到properties
|
||||||
|
|
||||||
|
doInstance();//根据全类名创建bean
|
||||||
|
|
||||||
|
doIoc(); //根据bean进行依赖注入
|
||||||
|
|
||||||
|
buildURLMapping(); //shiyi/query-----> method 建立映射关系
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void scanPackage(String basePackage){
|
||||||
|
URL url = this.getClass().getClassLoader().getResource("/"+basePackage.replaceAll("\\.","/"));
|
||||||
|
String fileStr = url.getFile();
|
||||||
|
|
||||||
|
File file = new File(fileStr);
|
||||||
|
|
||||||
|
String[] filesStr = file.list();
|
||||||
|
|
||||||
|
for(String path: filesStr){
|
||||||
|
File filePath = new File(fileStr + path);
|
||||||
|
|
||||||
|
if (filePath.isDirectory()){
|
||||||
|
scanPackage(basePackage+"."+path);
|
||||||
|
}else {
|
||||||
|
//加入LIST
|
||||||
|
className.add(basePackage+"."+filePath.getName()); //路径+class类名
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//根据扫描出来的LIST全类名,进行实例化
|
||||||
|
private void doInstance(){
|
||||||
|
if (className.size() <= 0){
|
||||||
|
System.out.println("包扫描失败....");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//list的所有class类,对这些类进行实例化
|
||||||
|
for (String className:className){
|
||||||
|
String cn = className.replace(".class","");
|
||||||
|
try {
|
||||||
|
Class<?> clazz = Class.forName(cn);
|
||||||
|
if (clazz.isAnnotationPresent(EnjoyController.class)){
|
||||||
|
Object instace = clazz.newInstance(); //创建控制类
|
||||||
|
|
||||||
|
EnjoyRequestMapping requestMapping = clazz.getAnnotation(EnjoyRequestMapping.class);
|
||||||
|
String rmvalue = requestMapping.value();
|
||||||
|
beans.put(rmvalue, instace);
|
||||||
|
}else if (clazz.isAnnotationPresent(EnjoyService.class)){
|
||||||
|
EnjoyService service = clazz.getAnnotation(EnjoyService.class);
|
||||||
|
Object instace = clazz.newInstance();
|
||||||
|
beans.put(service.value(), instace);
|
||||||
|
}else {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (InstantiationException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//把service注入到controller
|
||||||
|
public void doIoc(){
|
||||||
|
if (beans.entrySet().size() <= 0){
|
||||||
|
System.out.println("没有一个实例化类");
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Map.Entry<String,Object> entry:beans.entrySet()){
|
||||||
|
|
||||||
|
Object instace = entry.getValue();
|
||||||
|
Class<?> clazz = instace.getClass();
|
||||||
|
|
||||||
|
if (clazz.isAnnotationPresent(EnjoyController.class)){
|
||||||
|
Field[] fileds = clazz.getDeclaredFields();
|
||||||
|
for (Field filed : fileds) {
|
||||||
|
if (filed.isAnnotationPresent(EnjoyAutowired.class)){
|
||||||
|
EnjoyAutowired auto = filed.getAnnotation(EnjoyAutowired.class);
|
||||||
|
String key = auto.value();
|
||||||
|
//打开权限 才能设值
|
||||||
|
filed.setAccessible(true);
|
||||||
|
try {
|
||||||
|
//对当前成员变量进行射设值
|
||||||
|
filed.set(instace, beans.get(key));
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}else {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}else {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void buildURLMapping(){
|
||||||
|
if (beans.entrySet().size() <=0 ){
|
||||||
|
System.out.println("没有类的实例化------");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Map.Entry<String, Object> entry: beans.entrySet()){
|
||||||
|
Object instace = entry.getValue();
|
||||||
|
|
||||||
|
Class<?> clazz = instace.getClass();
|
||||||
|
if (clazz.isAnnotationPresent(EnjoyController.class)){
|
||||||
|
EnjoyRequestMapping requestMapping = clazz.getAnnotation(EnjoyRequestMapping.class);
|
||||||
|
String classPath = requestMapping.value();
|
||||||
|
|
||||||
|
Method[] methods = clazz.getMethods();
|
||||||
|
for (Method method : methods) {
|
||||||
|
if (method.isAnnotationPresent(EnjoyRequestMapping.class)){
|
||||||
|
EnjoyRequestMapping methodMapping = method.getAnnotation(EnjoyRequestMapping.class);
|
||||||
|
String methodPath = methodMapping.value();//方法的路径
|
||||||
|
|
||||||
|
handlerMap.put(classPath + methodPath, method);
|
||||||
|
}else {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}else {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||||
|
this.doPost(req,resp);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||||
|
//获取请求路径 /shiyi-mvc/shiyi/query
|
||||||
|
String url = req.getRequestURI();
|
||||||
|
String context = req.getContextPath(); //shiyi-mvc
|
||||||
|
String path = url.replace(context, ""); //shiyi/query
|
||||||
|
|
||||||
|
Method method = (Method) handlerMap.get(path);
|
||||||
|
|
||||||
|
//根据key=/shiyi到map去拿
|
||||||
|
ShiyiController instance = (ShiyiController) beans.get("/" + path.split("/")[1]);
|
||||||
|
|
||||||
|
Object arg[] = hand(req,resp,method);
|
||||||
|
try {
|
||||||
|
method.invoke(instance,arg);
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
} catch (InvocationTargetException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//这里不用策略模式,VIP才有策略模式
|
||||||
|
private static Object[] hand(HttpServletRequest request, HttpServletResponse response,Method method){
|
||||||
|
//拿到当前待执行方法有哪些参数
|
||||||
|
Class<?>[] paramClazzs = method.getParameterTypes();
|
||||||
|
//根据参数的个数,new 一个参数的数组,讲方法里的所有参数赋值到args来
|
||||||
|
Object[] args = new Object[paramClazzs.length];
|
||||||
|
|
||||||
|
int args_i = 0;
|
||||||
|
int index = 0;
|
||||||
|
for (Class<?> paramClazz : paramClazzs) {
|
||||||
|
if (ServletRequest.class.isAssignableFrom(paramClazz)){
|
||||||
|
args[args_i++] = request;
|
||||||
|
}
|
||||||
|
if (ServletResponse.class.isAssignableFrom(paramClazz)){
|
||||||
|
args[args_i++] = response;
|
||||||
|
}
|
||||||
|
//从0-3判断有没有RequestParam注解,很明显paramClazz为0和1时,不是
|
||||||
|
//当为2时为@RequestParam,需要解析
|
||||||
|
//[@com.enjoy.shiyi.annaotaion.EnjoyRequestMapping(value=name)]
|
||||||
|
Annotation[] paramAns = method.getParameterAnnotations()[index];
|
||||||
|
if (paramAns.length > 0){
|
||||||
|
for (Annotation paramAn : paramAns) {
|
||||||
|
if (EnjoyRequestParm.class.isAssignableFrom(paramAn.getClass())){
|
||||||
|
EnjoyRequestParm rp = (EnjoyRequestParm) paramAn;
|
||||||
|
//找到注解里的name和age
|
||||||
|
args[args_i++] = request.getParameter(rp.value());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
return args;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue