springboot-同名class注入

by kevin 21. 二月 2024 14:30 >
springboot注入默认使用className作为注入的key,如果两个className一样,无法注入   添加配置CoreConfiguration package com.statcore; import com.xmeport.statcore.util.AnnotationBeanNameGenerator; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @ComponentScan(nameGenerator = AnnotationBeanNameGenerator.class) @MapperScan(value = "com.xmeport.statcore.mapper" ,nameGenerator = AnnotationBeanNameGenerator.class) public class CoreConfiguration { }   创建AnnotationBeanNameGenerator类 package com.statcore.util; import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanNameGenerator; import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.type.AnnotationMetadata; import org.springframework.util.StringUtils; import java.util.Map; import java.util.Set; public class AnnotationBeanNameGenerator implements BeanNameGenerator {     private static final String COMPONENT_ANNOTATION_CLASSNAME = "org.springframework.stereotype.Component";     @Override     public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {         if (definition instanceof AnnotatedBeanDefinition) {             String beanName = determineBeanNameFromAnnotation((AnnotatedBeanDefinition) definition);             if (StringUtils.hasText(beanName)) {                 // Explicit bean name found.                 return beanName;             }         }         return definition.getBeanClassName();     }     /**      * Derive a bean name from one of the annotations on the class.      *      * @param annotatedDef the annotation-aware bean definition      * @return the bean name, or {@code null} if none is found      */     protected String determineBeanNameFromAnnotation(AnnotatedBeanDefinition annotatedDef) {         AnnotationMetadata amd = annotatedDef.getMetadata();         Set<String> types = amd.getAnnotationTypes();         String beanName = null;         for (String type : types) {             AnnotationAttributes attributes = AnnotationAttributes.fromMap(amd.getAnnotationAttributes(type, false));             if (isStereotypeWithNameValue(type, amd.getMetaAnnotationTypes(type), attributes)) {                 Object value = attributes.get("value");                 if (value instanceof String) {                     String strVal = (String) value;                     if (StringUtils.hasLength(strVal)) {                         if (beanName != null && !strVal.equals(beanName)) {                             throw new IllegalStateException("Stereotype annotations suggest inconsistent " +                                     "component names: '" + beanName + "' versus '" + strVal + "'");                         }                         beanName = strVal;                     }                 }             }         }         return beanName;     }     /**      * Check whether the given annotation is a stereotype that is allowed      * to suggest a component name through its annotation {@code value()}.      *      * @param annotationType      the name of the annotation class to check      * @param metaAnnotationTypes the names of meta-annotations on the given annotation      * @param attributes          the map of attributes for the given annotation      * @return whether the annotation qualifies as a stereotype with component name      */     protected boolean isStereotypeWithNameValue(String annotationType,                                                 Set<String> metaAnnotationTypes, Map<String, Object> attributes) {         boolean isStereotype = annotationType.equals(COMPONENT_ANNOTATION_CLASSNAME) ||                 (metaAnnotationTypes != null && metaAnnotationTypes.contains(COMPONENT_ANNOTATION_CLASSNAME)) ||                 annotationType.equals("javax.annotation.ManagedBean") ||                 annotationType.equals("javax.inject.Named");         return (isStereotype && attributes != null && attributes.containsKey("value"));     } }

springboot移除url中的sessionid

by kevin 21. 二月 2024 14:26 >
1. 配置文件中添加 [sourcecode language='xml' padlinenumbers='true'] server.servlet.session.tracking-modes=COOKIE [/sourcecode] 2. 对每个请求的url判断,重新改写。 package com.ac; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @Component public class SessionUrlInterceptor extends HandlerInterceptorAdapter {     @Value("${sessionUrlEnable}")     private boolean sessionUrlEnable;     @Override     public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException, ServletException {         if (sessionUrlEnable && StringUtils.isNotBlank(request.getSession().getId())) {             String requestURI = request.getRequestURI();             String uriSession = ";jsessionid=" + request.getSession().getId();             LogUtils.logInfo("requestURI:" + requestURI + " sessionURI:" + uriSession);             if (requestURI.indexOf(uriSession) != -1) { //                LogUtils.logInfo("requestURI:" + requestURI + " Forward");                                requestURI = requestURI.replace(uriSession, "");                                request.getRequestDispatcher(requestURI).forward(request, response);                 return false;             }         }         return true;     } }

开发杂技 2021-07-18

by kevin 18. 七月 2021 10:10 >
1. thymeleaf js中取后台放在model中值的方式 首先,这个模式需要在<script></scipt>标签使用th:inline="javascript"。 <script th:inline="javascript"></script> 双中括号 [[ ]] 输出所需文本,对其文本内容中进行JS转义,使用引号将其括起来; 中括号小括号 [( )] 输出所需文本,不会转义字符串中的内容,不会在字符串两头加上引号。   2. Oracle在Linux中使用impdp和expdp导入导出数据碰到的一些几个问题 ora-39002,oracle对文件夹或者文件没读写的权限。 ora-31694,通过ftp上传下载的过程中,文件被改变,ftp使用二进制模式上传下载,解决这个问题。 ora-39151,已经存在的表或者没有数据的表,需要设置参数,table_exists_action=(APPEND,REPLACE,TRUNCATE)。   3.Springboot中使用getResourceAsStream()读取配置文件乱码 通过BufferedReader和InputStreamReader设置编码,如下图: