Skip to content

Commit

Permalink
spring-source
Browse files Browse the repository at this point in the history
  • Loading branch information
lilin409546297 committed Dec 11, 2018
0 parents commit b52c65b
Show file tree
Hide file tree
Showing 60 changed files with 1,474 additions and 0 deletions.
56 changes: 56 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?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.pinnet</groupId>
<artifactId>spring-source</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>com.github.junrar</groupId>
<artifactId>junrar</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.4.9.v20180320</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>9.4.9.v20180320</version>
</dependency>
</dependencies>

</project>
19 changes: 19 additions & 0 deletions src/main/java/com/pinnet/aop/AspectBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.pinnet.aop;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class AspectBean {
private static final Map<Class, Object> map = new ConcurrentHashMap<Class, Object>();

public static Object getBean(Class clazz) {
return map.get(clazz);
}

public static void init() {
AspectExcuteChild child = new AspectExcuteChild();
AspectInvoke aspectInvoke = new AspectInvoke(child);
AspectExcuteParent proxy = (AspectExcuteParent) aspectInvoke.getProxy();
map.put(AspectExcuteParent.class, proxy);
}
}
15 changes: 15 additions & 0 deletions src/main/java/com/pinnet/aop/AspectExcuteChild.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.pinnet.aop;

public class AspectExcuteChild implements AspectExcuteParent{

public void test() {
System.out.println("test");
}

// public static void main(String[] args) {
// AspectBean.init();
// AspectExcuteParent aspectExcuteParent = (AspectExcuteParent) AspectBean.getBean(AspectExcuteParent.class);
// aspectExcuteParent.test();
//
// }
}
7 changes: 7 additions & 0 deletions src/main/java/com/pinnet/aop/AspectExcuteParent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.pinnet.aop;

public interface AspectExcuteParent {

void test();

}
26 changes: 26 additions & 0 deletions src/main/java/com/pinnet/aop/AspectHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.pinnet.aop;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class AspectHandler {

@Before("execution(* *.*(..))")
public void before() {
System.out.println("before");
}

@After("execution(* *.*(..))")
public void after() {
System.out.println("after");
}

@AfterThrowing("execution(* *.*(..))")
public void AfterThrow() {
System.out.println("throw");
}

}
25 changes: 25 additions & 0 deletions src/main/java/com/pinnet/aop/AspectInvoke.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.pinnet.aop;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class AspectInvoke implements InvocationHandler {

private Object object;

public AspectInvoke(Object object) {
this.object = object;
}

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("before");
Object invoke = method.invoke(object, args);
System.out.println("after");
return invoke;
}

public Object getProxy() {
return Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(), object.getClass().getInterfaces(), this);
}
}
17 changes: 17 additions & 0 deletions src/main/java/com/pinnet/aop/AspectTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.pinnet.aop;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AspectTest {

public void test() {
System.out.println("test");
}

public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
AspectTest aspectTest = (AspectTest) context.getBean("aspectTest");
aspectTest.test();
}
}
58 changes: 58 additions & 0 deletions src/main/java/com/pinnet/aop/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.pinnet.aop;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Test {

public static void main(String[] args) throws IOException, ClassNotFoundException {
List<Class> clazzs = findClass("com.pinnet");
System.out.println(clazzs);
}

/**
* 提供直接调用的方法
* @param packageName
* @return
* @throws IOException
* @throws ClassNotFoundException
*/
public static List<Class> findClass(String packageName) throws IOException, ClassNotFoundException {
return findClass(packageName, new ArrayList<>());
}

/**
*
* @param packageName
* @param clazzs
* @return
* @throws ClassNotFoundException
* @throws IOException
*/
private static List<Class> findClass(String packageName, List<Class> clazzs) throws ClassNotFoundException, IOException {
//将报名替换成目录
String fileName = packageName.replaceAll("\\.", "/");
//通过classloader来获取文件列表
File file = new File(Thread.currentThread().getContextClassLoader().getResource(fileName).getFile());
File[] files = file.listFiles();
for (File f:files) {
//如果是目录,这进一个寻找
if (f.isDirectory()) {
//截取路径最后的文件夹名
String currentPathName = f.getAbsolutePath().substring(f.getAbsolutePath().lastIndexOf(File.separator)+1);
//进一步寻找
findClass(packageName+"."+currentPathName, clazzs);
} else {
//如果是class文件
if (f.getName().endsWith(".class")) {
//反射出实例
Class clazz = Thread.currentThread().getContextClassLoader().loadClass(packageName+"."+f.getName().replace(".class",""));
clazzs.add(clazz);
}
}
}
return clazzs;
}
}
26 changes: 26 additions & 0 deletions src/main/java/com/pinnet/aware/TestApplicationContextAware.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.pinnet.aware;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
* EmbeddedValueResolverAware/ResourceLoaderAware/ApplicationEventPublisherAware/MessageSourceAware/ApplicationContextAware类似
*/
public class TestApplicationContextAware implements ApplicationContextAware {

private ApplicationContext context;

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.context = applicationContext;
}

public ApplicationContext getContext() {
return context;
}

public void setContext(ApplicationContext context) {
this.context = context;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.pinnet.aware;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;

import javax.annotation.PostConstruct;

public class TestApplicationEventPublisherAware implements ApplicationEventPublisherAware {

private ApplicationEventPublisher publisher;

@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.publisher = applicationEventPublisher;
}

public ApplicationEventPublisher getPublisher() {
return publisher;
}

public class TestEvent extends ApplicationEvent {

private Object object;

public TestEvent(Object source, Object object) {
super(source);
this.object = object;
}

public Object getObject() {
return object;
}
}

@PostConstruct
public void test() {
publisher.publishEvent(new TestEvent(this, "test"));
}
}
31 changes: 31 additions & 0 deletions src/main/java/com/pinnet/aware/TestAware.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.pinnet.aware;

import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.*;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.GenericTypeResolver;
import org.springframework.core.OrderComparator;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.util.Assert;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.*;
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class TestAware {

public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-aware.xml");
}


}
20 changes: 20 additions & 0 deletions src/main/java/com/pinnet/aware/TestBeanAware.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.pinnet.aware;

import org.springframework.beans.factory.BeanNameAware;

/**
* BeanNameAware/BeanClassLoaderAware/BeanFactoryAware类似
*/
public class TestBeanAware implements BeanNameAware{

private String beanName;
@Override
public void setBeanName(String beanName) {
System.out.println(beanName);
this.beanName = beanName;
}

public String getBeanName() {
return beanName;
}
}
26 changes: 26 additions & 0 deletions src/main/java/com/pinnet/aware/TestEmbeddedValueResolverAware.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.pinnet.aware;

import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.util.StringValueResolver;

import javax.annotation.PostConstruct;

public class TestEmbeddedValueResolverAware implements EmbeddedValueResolverAware {

private StringValueResolver stringValueResolver;

@Override
public void setEmbeddedValueResolver(StringValueResolver stringValueResolver) {
this.stringValueResolver = stringValueResolver;
}

public String getProperties (String name) {
String elName = "${"+ name +"}";
return stringValueResolver.resolveStringValue(elName);
}

@PostConstruct
public void test() {
System.out.println(getProperties("name"));
}
}
Loading

0 comments on commit b52c65b

Please sign in to comment.