同步操作将从 justlive1/oxygen 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
light framework
a light framework base on Java
oxygen-core
${attrs.key:defaultValue}
├─ 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
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
oxygen-web
ServletContainerInitializer
to auto startupweb.xml
@Router
to mark a router class@Mapping
to mark a request handler method@Param,@HeaderParam,@CookieParam,@PathParam
to mark the parameter where to fetch, @Param
is defaultMap<String,Object>
and object class user defined├─ src/main
│─ java/.../web
│ │- handler // parameter hander
│ │- http // http parse
│ │- mapping // url mapping and paratemer mapping
│ │- router // an example
│ │- view // view resolver
│ │- DefaultWebAppInitializer.java // default web app initializer
│ │- DispatcherServlet.java // dispathcer
│ │- WebAppInitializer.java // interface of web app initializer
│ │- WebContainerInitializer.java // support of web container initializer
│ └─ WebPlugin.java // web plugin
└─ resources/META-INF/services
│- ...ServletContainerIntializer // servlet3.0
│- ...core.Plugin // add web plugin
│- ...ParamHandler // add paramhander services
│- ...RequestParse // add request parser services
└─ ...ViewResolver // add view resolver services
ServiceLoader
to load plugins and easy to extendAdd dependencies to your pom.xml:
<!-- core, include ioc, aop, config manager, crypto encoder, exceptions, scheduled job, cache and so on-->
<dependency>
<groupId>vip.justlive</groupId>
<artifactId>oxygen-core</artifactId>
<version>${oxygen.version}</version>
</dependency>
<!-- jdbc, can be used alone -->
<dependency>
<groupId>vip.justlive</groupId>
<artifactId>oxygen-jdbc</artifactId>
<version>${oxygen.version}</version>
</dependency>
<!-- web -->
<dependency>
<groupId>vip.justlive</groupId>
<artifactId>oxygen-web</artifactId>
<version>${oxygen.version}</version>
</dependency>
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);
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)
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);
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
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() {
...
}
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
ResultHandler<T>
// 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<T> clazz, Object... params)
List<T> Jdbc.queryForList(String sql, Class<T> clazz, Object... params)
Map<String, Object> Jdbc.queryForMap(String sql, Object... params)
List<Map<String, Object>> Jdbc.queryForMapList(String sql, Object... params)
// you can handler resultset by yourself
T Jdbc.query(String sql, ResultSetHandler<T> handler, Object... params)
int Jdbc.update(String sql, Object... params)
// start primary datasource transaction
Jdbc.startTx()
// start named datasource transaction
Jdbc.startTx(String dataSourceName)
// close primary datasource transaction
Jdbc.closeTx()
// close named datasource transaction
Jdbc.closeTx(String dataSourceName)
// use oxygen-core, only need to write configuration file
// multi datasource names
datasource.multi=a
// primary datasource
datasource.logSql=true
datasource.driverClassName=org.h2.Driver
datasource.url=jdbc:h2:mem:test;DB_CLOSE_DELAY=-1
datasource.username=sa
datasource.password=sa
// datasource named by a
datasource.a.driverClassName=org.h2.Driver
datasource.a.url=jdbc:h2:mem:a;DB_CLOSE_DELAY=-1
datasource.a.username=sa
datasource.a.password=sa
ColumnHandler
META-INF/services/vip.justlive.oxygen.jdbc.handler.ColumnHandler
file and add the class namepublic class MyColumnHandler implements ColumnHandler {
@Override
public boolean supported(Class<?> type) {
...
}
@Override
public Object fetch(ResultSet rs, int index) throws SQLException {
...
}
}
// add or update META-INF/services/vip.justlive.oxygen.jdbc.handler.ColumnHandler file and add class name
xxx.xxx.MyColumnHandler
JdbcInterceptor
Jdbc.addJdbcInterceptor
// embed print sql interceptor
@Slf4j
public class LogSqlJdbcInterceptor implements JdbcInterceptor {
@Override
public void before(String sql, List<Object> params) {
if (log.isDebugEnabled()) {
log.debug("execute sql: {} -> params: {}", sql, params);
}
}
}
// add interceptor
Jdbc.addJdbcInterceptor(JdbcInterceptor interceptor)
@Router @Mapping @Param...
to mark router class, request handle method and parameterView
to render or redirect views,return json when the return type is not void
or View
Request.current(),Response.current()
to get request or response in one thread// use @Router
@Router("/common")
public class CommonRouter {
// mark request path and request method, default is all
@Mapping(value = "/localDate",method = HttpMethod.GET)
public Resp localDate() {
return Resp.success(
LocalDate.now().plusDays(offset).atStartOfDay(ZoneOffset.systemDefault()).toInstant()
.toEpochMilli());
}
// view render
@Mapping("/index")
public View index() {
View view = new View();
view.setPath("/index.jsp");
return view;
}
// view redirect
@Mapping("/view")
public View index() {
View view = new View();
view.setPath("/index");
view.setRedirect(true);
return view;
}
// handle by yourself, the example is download
@Mapping("download")
public void download(HttpServletResponse resp) throws IOException {
resp.setCharacterEncoding("utf-8");
resp.setContentType("application/octet-stream;charset=utf-8");
resp.setHeader("Content-disposition", "attachment;filename=xx.txt");
Files.copy(new File("xxx.txt"), resp.getOutputStream());
}
}
// get Request Response
Request.current()
Response.current()
ViewResolver
ServiceLoader
or WebPlugin.addViewResolver
to add// an example in system
public class DefaultViewResolver implements ViewResolver {
@Override
public boolean supported(Object data) {
// redirect
if (data != null && data.getClass() == View.class && ((View) data).isRedirect()) {
return true;
}
// null or not view, return json
return data == null || data.getClass() != View.class;
}
@Override
public void resolveView(HttpServletRequest request, HttpServletResponse response, Object data) {
try {
if (data != null && data.getClass() == View.class) {
View view = (View) data;
String redirectUrl = view.getPath();
if (!redirectUrl.startsWith(Constants.HTTP_PREFIX) && !redirectUrl
.startsWith(Constants.HTTPS_PREFIX)) {
redirectUrl = request.getContextPath() + view.getPath();
}
response.sendRedirect(redirectUrl);
} else {
response.getWriter().print(JSON.toJSONString(data));
}
} catch (IOException e) {
throw Exceptions.wrap(e);
}
}
}
only need implementWebAppInitializer
and the system can auto load
public class MyWebAppInitializer implements WebAppInitializer {
@Override
public void onStartup(ServletContext context) {
...
}
@Override
public int order() {
...
}
}
E-mail: qq11419041@163.com
QQ: 1106088328
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。