Skip to content

Commit

Permalink
cola components
Browse files Browse the repository at this point in the history
  • Loading branch information
fulan.zjf committed Nov 14, 2020
1 parent 29619db commit 9d1aa68
Show file tree
Hide file tree
Showing 66 changed files with 1,815 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.alibaba.cola</groupId>
<artifactId>cola-normal-component-archetype</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>1.0.0</version>
<packaging>jar</packaging>

<name>cola-normal-component-archetype</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.alibaba.cola</groupId>
<artifactId>cola-starter-component-archetype</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>1.0.0</version>
<packaging>jar</packaging>

<name>cola-starter-component-archetype</name>
Expand Down
4 changes: 2 additions & 2 deletions cola-components/cola-component-catchlog-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
<name>cola-component-catchlog-starter</name>
<groupId>com.alibaba.cola</groupId>
<artifactId>cola-component-catchlog-starter</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>1.0.0</version>
<packaging>jar</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.boot.version>2.2.6.RELEASE</spring.boot.version>
<cola.version>1.0.0-SNAPSHOT</cola.version>
<cola.version>1.0.0</cola.version>
</properties>

<dependencies>
Expand Down
7 changes: 7 additions & 0 deletions cola-components/cola-component-domain-starter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## 作用
主要提供了@Entity注解,该注解将Spring的Bean的scope定义为prototype,因为Domain Entity是有状态的,不能
进行多线程共享,所以必须是多实例的。

另外提供了DomainFactory辅助类,帮助应用创建Domain Entity。


47 changes: 47 additions & 0 deletions cola-components/cola-component-domain-starter/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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>

<name>cola-component-domain-starter</name>
<groupId>com.alibaba.cola</groupId>
<artifactId>cola-component-domain-starter</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.boot.version>2.2.6.RELEASE</spring.boot.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>${spring.boot.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>${spring.boot.version}</version>
<scope>provided</scope>
</dependency>
<!-- 测试包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring.boot.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.alibaba.cola.domain;

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

/**
* ApplicationContextHelper
*
* @author Frank Zhang
* @date 2020-11-14 1:58 PM
*/
@Component
public class ApplicationContextHelper implements ApplicationContextAware {
private static ApplicationContext applicationContext;

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

public static <T> T getBean(Class<T> targetClz) {
T beanInstance = null;
//优先按type查
try {
beanInstance = (T)applicationContext.getBean(targetClz);
} catch (Exception e) {
}
//按name查
if (beanInstance == null) {
String simpleName = targetClz.getSimpleName();
//首字母小写
simpleName = Character.toLowerCase(simpleName.charAt(0)) + simpleName.substring(1);
beanInstance = (T)applicationContext.getBean(simpleName);
}
if (beanInstance == null) {
throw new RuntimeException("Component " + targetClz + " can not be found in Spring Container");
}
return beanInstance;
}

public static Object getBean(String claz) {
return ApplicationContextHelper.applicationContext.getBean(claz);
}

public static <T> T getBean(String name, Class<T> requiredType) {
return ApplicationContextHelper.applicationContext.getBean(name, requiredType);
}

public static <T> T getBean(Class<T> requiredType, Object... params) {
return ApplicationContextHelper.applicationContext.getBean(requiredType, params);
}

public static ApplicationContext getApplicationContext() {
return applicationContext;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.alibaba.cola.domain;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

/**
* @ Description :
* @ Author : Frank Zhang
* @ CreateDate : 2020/11/09
* @ Version : 1.0
*/
@Configuration
public class DomainAutoConfiguration {

@Bean
@ConditionalOnMissingBean(ApplicationContextHelper.class)
public ApplicationContextHelper initCatchLogAspect() {
return new ApplicationContextHelper();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.alibaba.cola.domain;

/**
* DomainFactory
*
* @author Frank Zhang
* @date 2019-01-03 2:41 PM
*/
public class DomainFactory {

public static <T> T create(Class<T> entityClz){
return ApplicationContextHelper.getBean(entityClz);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.alibaba.cola.domain;

import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.lang.annotation.*;

/**
* Entity, Entity Object is prototype and is not thread-safe
*
* @author Frank Zhang
* @date 2019-01-03 2:53 PM
*/
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Component
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public @interface Entity {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration = com.alibaba.cola.domain.DomainAutoConfiguration
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.alibaba.cola.domain;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* Application
*
* @author Frank Zhang
* @date 2020-11-10 3:58 PM
*/
@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);

Customer customer = DomainFactory.create(Customer.class);

System.out.println("Customer purchase power score : " + customer.getPurchasePowerScore());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.alibaba.cola.domain;

import com.alibaba.cola.domain.Entity;

import javax.annotation.Resource;

/**
* Customer
*
* @author Frank Zhang
* @date 2020-11-14 2:43 PM
*/
@Entity
public class Customer {
private String name;

private Integer age;

@Resource
private PurchasePowerGateway purchasePowerGateway;

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Long getPurchasePowerScore(){
return purchasePowerGateway.getScore();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.alibaba.cola.domain;

import org.springframework.stereotype.Component;

/**
* PurchasePowerGateway
*
* @author Frank Zhang
* @date 2020-11-14 2:45 PM
*/
@Component
public class PurchasePowerGateway {

public Long getScore(){
return 96L;
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
<charset>utf8</charset>
</encoder>
</appender>

<!--rootLogger是默认的logger-->
<root level="INFO">
<!--定义了两个appender,日志会通过往这两个appender里面写-->
<appender-ref ref="CONSOLE"/>
</root>

<!--应用日志-->
<!--这个logger没有指定appender,它会继承root节点中定义的那些appender-->
<logger name="com.alibaba.cola.domain" level="DEBUG"/>

</configuration>
7 changes: 7 additions & 0 deletions cola-components/cola-component-dto/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## 作用
制定了DTO的相关规范,一是,为了复用;二是,使得应用层面的Logging和异常处理AOP成为可能。

关于Logging和异常处理的更多信息,请参考cola-component-catchlog-starter。



2 changes: 1 addition & 1 deletion cola-components/cola-component-dto/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<name>cola-component-dto</name>
<groupId>com.alibaba.cola</groupId>
<artifactId>cola-component-dto</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>1.0.0</version>
<packaging>jar</packaging>

<properties>
Expand Down
9 changes: 9 additions & 0 deletions cola-components/cola-component-exception/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## 作用
制定了Exception的相关规范,一是,为了复用;二是,使得应用层面的Logging和异常处理AOP成为可能。

实际上,对于应用系统而言,只有三种类型的异常:
1. BizException:业务异常,有明确的业务语义,不需要记录Error日志,不需要Retry
2. SysException:已知的系统异常,需要记录Error日志,可以Retry
3. Exception:未知的其它异常,需要完整的Error Stack日志,可以Retry


2 changes: 1 addition & 1 deletion cola-components/cola-component-exception/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<name>cola-component-exception</name>
<groupId>com.alibaba.cola</groupId>
<artifactId>cola-component-exception</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>1.0.0</version>
<packaging>jar</packaging>

<properties>
Expand Down
9 changes: 9 additions & 0 deletions cola-components/cola-component-extension-starter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## 作用
该组件继承了老Cola Framework中的扩展点功能,旨在通过统一的扩展形式来支撑业务的变化。

## 原理
https://blog.csdn.net/significantfrank/article/details/100074716

## 使用介绍
参看测试代码`com.alibaba.cola.extension.test.ExtensionTest`

Loading

0 comments on commit 9d1aa68

Please sign in to comment.