-
Notifications
You must be signed in to change notification settings - Fork 294
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
18 changed files
with
382 additions
and
2 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
24 changes: 24 additions & 0 deletions
24
src/main/java/org/byron4j/cookbook/javacore/anno/jsr250/PostConstructDemo.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,24 @@ | ||
package org.byron4j.cookbook.javacore.anno.jsr250; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.ToString; | ||
|
||
import javax.annotation.PostConstruct; | ||
|
||
@Setter | ||
@Getter | ||
@ToString | ||
public class PostConstructDemo { | ||
|
||
private int age; | ||
|
||
@PostConstruct | ||
public void setAge(){ | ||
age = 10; | ||
} | ||
|
||
public static void main(String[] args){ | ||
System.out.println(new PostConstructDemo()); | ||
} | ||
} |
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
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
49 changes: 49 additions & 0 deletions
49
src/main/java/org/byron4j/cookbook/javacore/dynamicproxy/jdkproxy/MyInterceptor.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,49 @@ | ||
package org.byron4j.cookbook.javacore.dynamicproxy.jdkproxy; | ||
|
||
import java.lang.reflect.InvocationHandler; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Proxy; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* 通用模拟AOP环绕JDK动态代理工具类 | ||
* @param <T> | ||
*/ | ||
public class MyInterceptor<T> implements InvocationHandler { | ||
|
||
private T t; | ||
|
||
public MyInterceptor(T t) { | ||
this.t = t; | ||
} | ||
|
||
@Override | ||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { | ||
System.out.println("before method call : " + method.getName()); | ||
Object result = method.invoke(t, args); | ||
System.out.println("after method call : " + method.getName()); | ||
return result; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public static <T> T getProxy(T t, Class<? super T> interfaceType) { | ||
MyInterceptor handler = new MyInterceptor(t); | ||
return (T) Proxy.newProxyInstance(interfaceType.getClassLoader(), | ||
new Class<?>[]{interfaceType}, handler | ||
); | ||
} | ||
|
||
|
||
/** | ||
* 测试 | ||
* @param args | ||
*/ | ||
public static void main(String[] args) { | ||
List<String> list = MyInterceptor.getProxy(new ArrayList<>(), List.class); | ||
list.add("one"); | ||
list.add("two"); | ||
System.out.println(list); | ||
list.remove("one"); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/org/byron4j/cookbook/javacore/http/BasicHttpServerExample.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,26 @@ | ||
package org.byron4j.cookbook.javacore.http; | ||
|
||
import com.sun.net.httpserver.HttpContext; | ||
import com.sun.net.httpserver.HttpExchange; | ||
import com.sun.net.httpserver.HttpServer; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.net.InetSocketAddress; | ||
|
||
public class BasicHttpServerExample { | ||
public static void main(String[] args) throws IOException { | ||
HttpServer server = HttpServer.create(new InetSocketAddress(8500), 0); | ||
HttpContext context = server.createContext("/"); | ||
context.setHandler(BasicHttpServerExample::handleRequest); | ||
server.start(); | ||
} | ||
|
||
private static void handleRequest(HttpExchange exchange) throws IOException { | ||
String response = "Hi there!"; | ||
exchange.sendResponseHeaders(200, response.getBytes().length);//response code and length | ||
OutputStream os = exchange.getResponseBody(); | ||
os.write(response.getBytes()); | ||
os.close(); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/org/byron4j/cookbook/javacore/http/BasicHttpServerExample2.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,49 @@ | ||
package org.byron4j.cookbook.javacore.http; | ||
|
||
import com.sun.net.httpserver.*; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.net.InetSocketAddress; | ||
import java.net.URI; | ||
|
||
/** | ||
* org.springframework.remoting.jaxws.SimpleHttpServerJaxWsServiceExporter#buildHttpContext(javax.xml.ws.Endpoint, java.lang.String) | ||
*/ | ||
public class BasicHttpServerExample2 { | ||
public static void main(String[] args) throws IOException { | ||
HttpServer server = HttpServer.create(new InetSocketAddress(8500), 0); | ||
HttpContext context = server.createContext("/example"); | ||
context.setHandler(BasicHttpServerExample2::handleRequest); | ||
server.start(); | ||
} | ||
|
||
private static void handleRequest(HttpExchange exchange) throws IOException { | ||
URI requestURI = exchange.getRequestURI(); | ||
printRequestInfo(exchange); | ||
String response = "This is the response at " + requestURI; | ||
exchange.sendResponseHeaders(200, response.getBytes().length); | ||
OutputStream os = exchange.getResponseBody(); | ||
os.write(response.getBytes()); | ||
os.close(); | ||
} | ||
|
||
private static void printRequestInfo(HttpExchange exchange) { | ||
System.out.println("-- headers --"); | ||
Headers requestHeaders = exchange.getRequestHeaders(); | ||
requestHeaders.entrySet().forEach(System.out::println); | ||
|
||
System.out.println("-- principle --"); | ||
HttpPrincipal principal = exchange.getPrincipal(); | ||
System.out.println(principal); | ||
|
||
System.out.println("-- HTTP method --"); | ||
String requestMethod = exchange.getRequestMethod(); | ||
System.out.println(requestMethod); | ||
|
||
System.out.println("-- query --"); | ||
URI requestURI = exchange.getRequestURI(); | ||
String query = requestURI.getQuery(); | ||
System.out.println(query); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/org/byron4j/cookbook/javacore/log/ch1/MyClass.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,24 @@ | ||
package org.byron4j.cookbook.javacore.log.ch1; | ||
|
||
import java.util.logging.Logger; | ||
|
||
/** | ||
* JCL:java.util.logging.Logger | ||
*/ | ||
public class MyClass { | ||
private static Logger LOGGER ; | ||
|
||
static { | ||
|
||
System.setProperty("java.util.logging.config.file", "F:\\217my_optLogs\\001系统相关\\系统设计\\007\\CookBook\\src\\main\\resources\\logging.properties"); | ||
|
||
//must initialize loggers after setting above property | ||
LOGGER = Logger.getLogger(MyClass.class.getName()); | ||
} | ||
|
||
public static void main(String[] args) { | ||
System.out.println("main method starts"); | ||
LOGGER.info("info msg"); | ||
LOGGER.warning("warning msg"); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/org/byron4j/cookbook/javacore/spi/DefaultView.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,27 @@ | ||
package org.byron4j.cookbook.javacore.spi; | ||
|
||
import javafx.geometry.Insets; | ||
import javafx.scene.Node; | ||
import javafx.scene.control.Label; | ||
import javafx.scene.control.TextField; | ||
import javafx.scene.layout.HBox; | ||
|
||
public class DefaultView implements View { | ||
@Override | ||
public String getName() { | ||
return "Default View"; | ||
} | ||
|
||
@Override | ||
public Node getView() { | ||
Label label = new Label("The label"); | ||
label.setPadding(new Insets(5, 10, 0, 0)); | ||
TextField text = new TextField(); | ||
|
||
HBox hbox = new HBox(); | ||
hbox.setPadding(new Insets(50, 50, 50, 50)); | ||
hbox.getChildren().add(label); | ||
hbox.getChildren().add(text); | ||
return hbox; | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
src/main/java/org/byron4j/cookbook/javacore/spi/UiApplication.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,75 @@ | ||
package org.byron4j.cookbook.javacore.spi; | ||
|
||
import javafx.application.Application; | ||
import javafx.geometry.Rectangle2D; | ||
import javafx.scene.Group; | ||
import javafx.scene.Scene; | ||
import javafx.scene.control.*; | ||
import javafx.scene.layout.BorderPane; | ||
import javafx.stage.Screen; | ||
import javafx.stage.Stage; | ||
|
||
import java.util.ServiceLoader; | ||
|
||
public class UiApplication extends Application { | ||
|
||
private ServiceLoader<View> views; | ||
private BorderPane mainBorderPane; | ||
|
||
@Override | ||
public void init() { | ||
loadViews(); | ||
|
||
} | ||
|
||
private void loadViews() { | ||
views = ServiceLoader.load(View.class); | ||
} | ||
|
||
@Override | ||
public void start(Stage stage) throws Exception { | ||
Screen screen = Screen.getPrimary(); | ||
Rectangle2D bounds = screen.getVisualBounds(); | ||
|
||
stage.setX(bounds.getMinX()); | ||
stage.setY(bounds.getMinY()); | ||
stage.setWidth(bounds.getWidth()); | ||
stage.setHeight(bounds.getHeight()); | ||
stage.setTitle("Ui Application"); | ||
|
||
mainBorderPane = new BorderPane(); | ||
|
||
mainBorderPane.setTop(createMenuBar()); | ||
|
||
|
||
Scene scene = new Scene(new Group()); | ||
scene.setRoot(mainBorderPane); | ||
|
||
stage.setScene(scene); | ||
stage.show(); | ||
} | ||
|
||
private MenuBar createMenuBar() { | ||
MenuBar menuBar = new MenuBar(); | ||
Menu viewMenu = new Menu("Views"); | ||
menuBar.getMenus().add(viewMenu); | ||
|
||
ToggleGroup toggleGroup = new ToggleGroup(); | ||
|
||
views.forEach(v -> { | ||
RadioMenuItem item = new RadioMenuItem(v.getName()); | ||
item.setToggleGroup(toggleGroup); | ||
item.setOnAction(event -> { | ||
Label label = new Label(v.getName()); | ||
mainBorderPane.setLeft(label); | ||
mainBorderPane.setCenter(v.getView()); | ||
}); | ||
viewMenu.getItems().add(item); | ||
}); | ||
return menuBar; | ||
} | ||
|
||
public static void main(String[] args) { | ||
launch(args); | ||
} | ||
} |
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,9 @@ | ||
package org.byron4j.cookbook.javacore.spi; | ||
|
||
import javafx.scene.Node; | ||
|
||
public interface View { | ||
String getName(); | ||
|
||
Node getView(); | ||
} |
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,30 @@ | ||
package org.byron4j.cookbook.yaml; | ||
|
||
import org.yaml.snakeyaml.Yaml; | ||
|
||
import java.io.FileInputStream; | ||
import java.net.URL; | ||
import java.util.Map; | ||
|
||
public class Yaml1 { | ||
public static void main(String[] args) { | ||
try { | ||
Yaml yaml = new Yaml(); | ||
URL url = Yaml1.class.getClassLoader().getResource("/application.yml"); | ||
if (url != null) { | ||
//获取test.yaml文件中的配置数据,然后转换为obj, | ||
Object obj =yaml.load(new FileInputStream("/application.yml")); | ||
System.out.println(obj); | ||
//也可以将值转换为Map | ||
Map map =(Map)yaml.load(new FileInputStream(url.getFile())); | ||
System.out.println(map); | ||
//通过map我们取值就可以了. | ||
|
||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
|
||
} | ||
} |
1 change: 1 addition & 0 deletions
1
src/main/resources/META-INF/services/org.byron4j.cookbook.javacore.spi.DefaultView
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 @@ | ||
org.byron4j.cookbook.javacore.spi.DefaultView |
Oops, something went wrong.