# oxygen
**Repository Path**: ozinne/oxygen
## Basic Information
- **Project Name**: oxygen
- **Description**: 一个轻量级Java框架,包含ioc、aop、配置管理、定时任务、缓存、密码加密、异常处理、Jdbc等
- **Primary Language**: Java
- **License**: Apache-2.0
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 38
- **Created**: 2018-10-10
- **Last Updated**: 2021-11-02
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# oxygen
light framework
## Documentation
a light framework base on Java
oxygen-core
- aop base on cglib
- provide cache manager
- config manager, support `${attrs.key:defaultValue}`
- crypto
- exception manager, i18n friendly
- ioc
- scheduled job
```
├─ src/main
│─ java/.../core //oxygen-core
│ │- aop //aop module
│ │- cache //cache module
│ │- config //config module
│ │- constant //constant
│ │- convert //type convert module
│ │- crypto //crypto module
│ │- domain //domain module
│ │- exception //exception module
│ │- io //io module
│ │- ioc //ioc module
│ │- job //scheduled job module
│ │- scan //class scan module
│ │- util //util module
│ │- Bootstrap.java //the class to start framework
│ └─ Plugin.java //the interface of plugin
└─ resources/META-INF/services
└─ ...core.Plugin //Plugin service configuration file
```
oxygen-jdbc
- simple only base on jdk
- support multi-datasource
- base on `sql` (sql is the DSL of database. It is very natural and elegant. Less is more)
```
├─ src/main
│─ java/.../jdbc //oxygen-jdbc
│ │- config //datasource config
│ │- handler //data handler
│ │- interceptor //jdbc execute interceptor
│ │- Jdbc.java //the class used to operate database
│ │- JdbcException.java //jdbc excption
│ └─ JdbcPlugin.java //jdbc plugin
└─ resources/META-INF/services
│- ...handler.ColumnHandler //columnHandler service configuration file
└─ ...core.Plugin //add jdbcPlugin
```
## Features
* light and simple to use
* user `ServiceLoader` to load plugins and easy to extend
## Install
Add dependencies to your pom.xml:
```
vip.justliveoxygen-core${oxygen.version}vip.justliveoxygen-jdbc${oxygen.version}
```
## Quick start
### Common Response
we use `Resp` as response
```
// success response with code 00000
Resp.success(Object obj);
// error response with default code 99999
Resp.error(String msg);
// error response with custom code
Resp.error(String code, String msg);
```
### Exceptions
we use `Exceptions` to throw a runtime exception
```
// create instance of ErrorCode
ErrorCode err = Exceptions.errorCode(String module, String code);
ErrorCode err = Exceptions.errorMessage(String module, String code, String message);
// throw an unchecked exception wrapped
throw Exceptions.wrap(Throwable e);
throw Exceptions.wrap(Throwable e, String code, String message);
throw Exceptions.wrap(Throwable e, ErrorCode errorCode, Object... arguments);
// throw a business exception with none stack trace
throw Exceptions.fail(ErrorCode errCode, Object... params);
throw Exceptions.fail(String code, String message, Object... params);
// throw a fault with stack trace
throw Exceptions.fault(ErrorCode errCode, Object... params);
throw Exceptions.fault(String code, String message, Object... params);
throw Exceptions.fault(Throwable e, ErrorCode errCode, Object... params);
throw Exceptions.fault(Throwable e, String code, String message, Object... params)
```
### IOC
you can use IOC container with annotation
```
// add scan packages property in config file
main.class.scan=com.xxx.xxx,com.aaa.bbb
// use @Configuration and @Bean
@Configuration
public class Conf {
@Bean
Inter noDepBean() {
return new NoDepBean();
}
}
// use @Bean and @Inject
@Bean("depBean")
public class DepBean implements Inter {
private final NoDepBean noDepBean;
@Inject
public DepBean(NoDepBean noDepBean) {
this.noDepBean = noDepBean;
}
...
}
// get bean at runtime
Inter inter = BeanStore.getBean("depBean", Inter.class);
```
### AOP
we can use aop with annotations
```
// define aop method
@Before(annotation = Log.class)
public void log(Invocation invocation) {
...
}
// target method
@Log
public void print() {
...
}
```
### Scheduled Job
`@Scheduled` that marks a method to be scheduled.
Exactly one of the onApplicationStart(), cron(), fixedDelay(), or fixedRate() attributes must be specified.
```
// Creates and executes a periodic action that becomes enabled first after the given initial delay,
// and subsequently with the given delay between the termination of one execution and the commencement of the next.
@Scheduled(fixedDelay = "500")
public void run1() {
...
}
// Creates and executes a periodic action that becomes enabled first after the given initial delay,
// and subsequently with the given period
@Scheduled(fixedRate = "600")
public void run2() {
...
}
// Schedule the specified cron task and run in async mode when application started
@Scheduled(cron = "0/5 * * * * ?", onApplicationStart = true, async = true)
public void run3() {
...
}
```
### Cache
There are two ways to use the cache
- `JCache.cache()` Get the cache and then call the api
- `@Cacheable` Use annotation to add cache on method
```
// use cache api
Cache cache = JCache.cache(cacheName);
T value = cache.get(key, clazz);
cache.set(key, value, duration, timeUnit);
...
// use annotation
@Cacheable
public Object method() {
...
}
@Cacheable(key = "args[0]", duration = 10, timeUnit = TimeUnit.MINUTES)
public Object method(Object arg0, Object arg1) {
...
}
```
### Jdbc
#### basic
- multi-datasource
- crud using `Jdbc`
- handler ResultSet by yourself using `ResultHandler`
- start and close transaction
```
// use it alone
...
// add primary datasource
Jdbc.addPrimaryDataSource(DataSource dataSource)
// add mutli datasource
Jdbc.addDataSource(String name, DataSource dataSource)
// crud
T Jdbc.query(String sql, Class clazz, Object... params)
List Jdbc.queryForList(String sql, Class clazz, Object... params)
Map Jdbc.queryForMap(String sql, Object... params)
List