Skip to content

Commit

Permalink
增加电子书
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron4j committed May 31, 2019
1 parent a1dbbd9 commit ab4ccf1
Show file tree
Hide file tree
Showing 18 changed files with 382 additions and 2 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,12 @@
<version>20.0</version>
</dependency>

<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.19</version>
</dependency>

</dependencies>


Expand Down
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ public Object intercept(Object o, Method method, Object[] args, MethodProxy meth

public static void main(String[] args){
CglibProxy cglibProxy = new CglibProxy();
BookStore instance = (BookStore)cglibProxy.getInstance(new BookStore());
Object obj = cglibProxy.getInstance(new BookStore());
System.out.println("cglib代理出来的对象:" + obj);
BookStore instance = (BookStore)obj;
instance.buyBook();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class JDKDynamicProxy implements InvocationHandler {

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("=================================================\n查看代理类:" + proxy.getClass().getName());
System.out.println("=================================================\n查看代理类:" + proxy.getClass().toGenericString());

Object invoke = null;

Expand Down
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");
}
}
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();
}
}
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 src/main/java/org/byron4j/cookbook/javacore/log/ch1/MyClass.java
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 src/main/java/org/byron4j/cookbook/javacore/spi/DefaultView.java
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 src/main/java/org/byron4j/cookbook/javacore/spi/UiApplication.java
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);
}
}
9 changes: 9 additions & 0 deletions src/main/java/org/byron4j/cookbook/javacore/spi/View.java
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();
}
30 changes: 30 additions & 0 deletions src/main/java/org/byron4j/cookbook/yaml/Yaml1.java
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();
}


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.byron4j.cookbook.javacore.spi.DefaultView
Loading

0 comments on commit ab4ccf1

Please sign in to comment.