BaseService#initMapper初始化移到zheng-common,并且只初始化@BaseService注解的bean

This commit is contained in:
shuzheng 2017-02-11 23:57:06 +08:00
parent 7a289a285d
commit 5d5b81e950
19 changed files with 41 additions and 66 deletions

View File

@ -4,6 +4,7 @@ import com.zheng.cms.dao.mapper.CmsArticleMapper;
import com.zheng.cms.dao.model.CmsArticle;
import com.zheng.cms.dao.model.CmsArticleExample;
import com.zheng.cms.rpc.api.CmsArticleService;
import com.zheng.common.annotation.BaseService;
import com.zheng.common.base.BaseServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -16,6 +17,7 @@ import org.springframework.transaction.annotation.Transactional;
*/
@Service
@Transactional
@BaseService
public class CmsArticleServiceImpl extends BaseServiceImpl<CmsArticleMapper, CmsArticle, CmsArticleExample> implements CmsArticleService {
private static Logger _log = LoggerFactory.getLogger(CmsArticleServiceImpl.class);

View File

@ -4,6 +4,7 @@ import com.zheng.cms.dao.mapper.CmsCategoryMapper;
import com.zheng.cms.dao.model.CmsCategory;
import com.zheng.cms.dao.model.CmsCategoryExample;
import com.zheng.cms.rpc.api.CmsCategoryService;
import com.zheng.common.annotation.BaseService;
import com.zheng.common.base.BaseServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -16,6 +17,7 @@ import org.springframework.transaction.annotation.Transactional;
*/
@Service
@Transactional
@BaseService
public class CmsCategoryServiceImpl extends BaseServiceImpl<CmsCategoryMapper, CmsCategory, CmsCategoryExample> implements CmsCategoryService {
private static Logger _log = LoggerFactory.getLogger(CmsCategoryServiceImpl.class);

View File

@ -4,6 +4,7 @@ import com.zheng.cms.dao.mapper.CmsCommentMapper;
import com.zheng.cms.dao.model.CmsComment;
import com.zheng.cms.dao.model.CmsCommentExample;
import com.zheng.cms.rpc.api.CmsCommentService;
import com.zheng.common.annotation.BaseService;
import com.zheng.common.base.BaseServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -16,6 +17,7 @@ import org.springframework.transaction.annotation.Transactional;
*/
@Service
@Transactional
@BaseService
public class CmsCommentServiceImpl extends BaseServiceImpl<CmsCommentMapper, CmsComment, CmsCommentExample> implements CmsCommentService {
private static Logger _log = LoggerFactory.getLogger(CmsCommentServiceImpl.class);

View File

@ -4,6 +4,7 @@ import com.zheng.cms.dao.mapper.CmsPageMapper;
import com.zheng.cms.dao.model.CmsPage;
import com.zheng.cms.dao.model.CmsPageExample;
import com.zheng.cms.rpc.api.CmsPageService;
import com.zheng.common.annotation.BaseService;
import com.zheng.common.base.BaseServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -16,6 +17,7 @@ import org.springframework.transaction.annotation.Transactional;
*/
@Service
@Transactional
@BaseService
public class CmsPageServiceImpl extends BaseServiceImpl<CmsPageMapper, CmsPage, CmsPageExample> implements CmsPageService {
private static Logger _log = LoggerFactory.getLogger(CmsPageServiceImpl.class);

View File

@ -4,6 +4,7 @@ import com.zheng.cms.dao.mapper.CmsSettingMapper;
import com.zheng.cms.dao.model.CmsSetting;
import com.zheng.cms.dao.model.CmsSettingExample;
import com.zheng.cms.rpc.api.CmsSettingService;
import com.zheng.common.annotation.BaseService;
import com.zheng.common.base.BaseServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -16,6 +17,7 @@ import org.springframework.transaction.annotation.Transactional;
*/
@Service
@Transactional
@BaseService
public class CmsSettingServiceImpl extends BaseServiceImpl<CmsSettingMapper, CmsSetting, CmsSettingExample> implements CmsSettingService {
private static Logger _log = LoggerFactory.getLogger(CmsSettingServiceImpl.class);

View File

@ -4,6 +4,7 @@ import com.zheng.cms.dao.mapper.CmsTagMapper;
import com.zheng.cms.dao.model.CmsTag;
import com.zheng.cms.dao.model.CmsTagExample;
import com.zheng.cms.rpc.api.CmsTagService;
import com.zheng.common.annotation.BaseService;
import com.zheng.common.base.BaseServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -16,6 +17,7 @@ import org.springframework.transaction.annotation.Transactional;
*/
@Service
@Transactional
@BaseService
public class CmsTagServiceImpl extends BaseServiceImpl<CmsTagMapper, CmsTag, CmsTagExample> implements CmsTagService {
private static Logger _log = LoggerFactory.getLogger(CmsTagServiceImpl.class);

View File

@ -6,6 +6,7 @@ import com.zheng.cms.dao.model.CmsUser;
import com.zheng.cms.dao.model.CmsUserExample;
import com.zheng.cms.dao.model.UserVO;
import com.zheng.cms.rpc.api.UserService;
import com.zheng.common.annotation.BaseService;
import com.zheng.common.base.BaseServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -21,6 +22,7 @@ import org.springframework.transaction.annotation.Transactional;
*/
@Service
@Transactional
@BaseService
public class UserServiceImpl extends BaseServiceImpl<CmsUserMapper, CmsUser, CmsUserExample> implements UserService {
private static Logger _log = LoggerFactory.getLogger(UserServiceImpl.class);

View File

@ -1,42 +0,0 @@
package com.zheng.cms.rpc.service.listener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;
import java.lang.reflect.Method;
import java.util.Map;
/**
* spring容器初始化完成事件
* Created by shuzheng on 2017/1/7.
*/
public class ApplicationContextListener implements ApplicationListener<ContextRefreshedEvent> {
private static Logger _log = LoggerFactory.getLogger(ApplicationContextListener.class);
@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
// root application context
if (null == contextRefreshedEvent.getApplicationContext().getParent()) {
_log.info(">>>>> spring初始化完毕 <<<<<");
// spring初始化完毕后通过反射调用所有service的initMapper方法
Map<String, Object> services = contextRefreshedEvent.getApplicationContext().getBeansWithAnnotation(Service.class);
_log.info("===== 开始初始化service的initMapper方法 =====");
for (Object service : services.values()) {
_log.info(">>>>> {}.initMapper()", service.getClass().getName());
try {
Method initMapper = service.getClass().getMethod("initMapper");
initMapper.invoke(service);
} catch (Exception e) {
_log.error("初始化service的initMapper方法异常", e);
e.printStackTrace();
}
}
_log.info("===== 完成初始化service的initMapper方法 =====");
}
}
}

View File

@ -0,0 +1,13 @@
package com.zheng.common.annotation;
import java.lang.annotation.*;
/**
* 初始化继承BaseService的service
* Created by shuzheng on 2017/2/11.
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface BaseService {
}

View File

@ -42,8 +42,6 @@ public interface BaseService<Record, Example> {
int deleteByPrimaryKeys(String ids);
//void initMapper(Class clazz);
void initMapper();
}

View File

@ -240,11 +240,6 @@ public abstract class BaseServiceImpl<Mapper, Record, Example> implements BaseSe
return 0;
}
// @Override
// public void initMapper(Class clazz) {
// this.mapper = (Mapper) SpringContextUtil.getBean(clazz);
// }
@Override
public void initMapper() {
this.mapper = SpringContextUtil.getBean(getMapperClass());

View File

@ -1,10 +1,10 @@
package com.zheng.upms.rpc.service.listener;
package com.zheng.common.listener;
import com.zheng.common.annotation.BaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;
import java.lang.reflect.Method;
import java.util.Map;
@ -23,8 +23,7 @@ public class ApplicationContextListener implements ApplicationListener<ContextRe
if(null == contextRefreshedEvent.getApplicationContext().getParent()) {
_log.info(">>>>> spring初始化完毕 <<<<<");
// spring初始化完毕后通过反射调用所有service的initMapper方法
Map<String, Object> services = contextRefreshedEvent.getApplicationContext().getBeansWithAnnotation(Service.class);
_log.info("===== 开始初始化service的initMapper方法 =====");
Map<String, Object> services = contextRefreshedEvent.getApplicationContext().getBeansWithAnnotation(BaseService.class);
for(Object service : services.values()) {
_log.info(">>>>> {}.initMapper()", service.getClass().getName());
try {
@ -35,7 +34,6 @@ public class ApplicationContextListener implements ApplicationListener<ContextRe
e.printStackTrace();
}
}
_log.info("===== 完成初始化service的initMapper方法 =====");
}
}

View File

@ -6,6 +6,6 @@
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Spring容器初始化完成监听器 -->
<bean class="com.zheng.cms.rpc.service.listener.ApplicationContextListener"></bean>
<bean class="com.zheng.common.listener.ApplicationContextListener"></bean>
</beans>

View File

@ -1,5 +1,6 @@
package com.zheng.upms.rpc.service.impl;
import com.zheng.common.annotation.BaseService;
import com.zheng.common.base.BaseServiceImpl;
import com.zheng.upms.dao.mapper.UpmsOrganizationMapper;
import com.zheng.upms.dao.model.UpmsOrganization;
@ -17,6 +18,7 @@ import org.springframework.transaction.annotation.Transactional;
*/
@Service
@Transactional
@BaseService
public class UpmsOrganizationServiceImpl extends BaseServiceImpl<UpmsOrganizationMapper, UpmsOrganization, UpmsOrganizationExample> implements UpmsOrganizationService {
private static Logger _log = LoggerFactory.getLogger(UpmsOrganizationServiceImpl.class);

View File

@ -1,5 +1,6 @@
package com.zheng.upms.rpc.service.impl;
import com.zheng.common.annotation.BaseService;
import com.zheng.common.base.BaseServiceImpl;
import com.zheng.upms.dao.mapper.UpmsPermissionMapper;
import com.zheng.upms.dao.model.UpmsPermission;
@ -17,6 +18,7 @@ import org.springframework.transaction.annotation.Transactional;
*/
@Service
@Transactional
@BaseService
public class UpmsPermissionServiceImpl extends BaseServiceImpl<UpmsPermissionMapper, UpmsPermission, UpmsPermissionExample> implements UpmsPermissionService {
private static Logger _log = LoggerFactory.getLogger(UpmsPermissionServiceImpl.class);

View File

@ -1,5 +1,6 @@
package com.zheng.upms.rpc.service.impl;
import com.zheng.common.annotation.BaseService;
import com.zheng.common.base.BaseServiceImpl;
import com.zheng.upms.dao.mapper.UpmsRoleMapper;
import com.zheng.upms.dao.model.UpmsRole;
@ -17,6 +18,7 @@ import org.springframework.transaction.annotation.Transactional;
*/
@Service
@Transactional
@BaseService
public class UpmsRoleServiceImpl extends BaseServiceImpl<UpmsRoleMapper, UpmsRole, UpmsRoleExample> implements UpmsRoleService {
private static Logger _log = LoggerFactory.getLogger(UpmsRoleServiceImpl.class);

View File

@ -1,5 +1,6 @@
package com.zheng.upms.rpc.service.impl;
import com.zheng.common.annotation.BaseService;
import com.zheng.common.base.BaseServiceImpl;
import com.zheng.upms.dao.mapper.UpmsSystemMapper;
import com.zheng.upms.dao.model.UpmsSystem;
@ -17,6 +18,7 @@ import org.springframework.transaction.annotation.Transactional;
*/
@Service
@Transactional
@BaseService
public class UpmsSystemServiceImpl extends BaseServiceImpl<UpmsSystemMapper, UpmsSystem, UpmsSystemExample> implements UpmsSystemService {
private static Logger _log = LoggerFactory.getLogger(UpmsSystemServiceImpl.class);

View File

@ -1,5 +1,6 @@
package com.zheng.upms.rpc.service.impl;
import com.zheng.common.annotation.BaseService;
import com.zheng.common.base.BaseServiceImpl;
import com.zheng.upms.dao.mapper.UpmsUserMapper;
import com.zheng.upms.dao.model.UpmsUser;
@ -17,6 +18,7 @@ import org.springframework.transaction.annotation.Transactional;
*/
@Service
@Transactional
@BaseService
public class UpmsUserServiceImpl extends BaseServiceImpl<UpmsUserMapper, UpmsUser, UpmsUserExample> implements UpmsUserService {
private static Logger _log = LoggerFactory.getLogger(UpmsUserServiceImpl.class);

View File

@ -1,11 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Spring容器初始化完成监听器 -->
<bean class="com.zheng.upms.rpc.service.listener.ApplicationContextListener"></bean>
</beans>