SpringCloud改造老Spring项目

改造老Spring项目,其实是可以直接保留Spring下面的各类XML文件的,个人觉得还有XML文件不美观,也缺少改造的作用。下面介绍的我0配置文件改造。

1、各类属性

  在Spring中需要在配置文件注入的<property name="cookieName" value="${cookie.name}"/> ,这些属性首先要在yml文件中定义,之后可以在类中使用 @Value实现,如:  

@Value("${cookie.name}") 
private String cookieName

 

2、外部接口

  需要在配置文件中注入的外部接口,在配置文件中一般是这样配置的。

    <bean id="userWebService" class="com.aa.bb.impl.UserWebServiceImpl">
        <property name="url" value="${user.url}"/>
        <property name="token" value="${user.token}"/>
    </bean>

  我们声明一个类用于存放这类外部接口,例如BeanConfig,使用@Configuration注解。外部接口实现方式。至于集成的其他技术也可以这么实现,比如:memcached、mongodb、redis、oss、cache配置、Interceptor类等等。

   @Bean
    public UserWebService createUserWebService(){
        UserWebService userWebService= new UserWebService ();
        userWebService.setUrl(url);
        userWebService.setToken(token);
        return userWebService;
    }

 

3. 拦截器

@Configuration
public class InterceptorConfig extends WebMvcConfigurerAdapter {

    @Autowired
    private Interceptor1 interceptor1;
    @Autowired
    private Interceptor2 interceptor2;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        String[] interceptor1_includePatterns = {"/**"};     //需要拦截的地址
        String[] interceptor1_excludePatterns = {"/static/**",  //不拦截的地址
                "/api/**",
                "/vip/**",
                "/sc/**"};
        MappedInterceptor interceptor1_mappedInterceptor = new MappedInterceptor(interceptor1_includePatterns , interceptor1_excludePatterns , interceptor1);

        registry.addInterceptor(interceptor1_mappedInterceptor );
        registry.addInterceptor(interceptor2);
        super.addInterceptors(registry);
    }

 

4、增加注解

  项目中controller、service、manager、dao、mapper各层的类都要增加对应的数据,如:@RestController、@Service、@Component、@Repository

 

5、事务

  如果老项目有自定义的事务类,全部改造成使用@Transactional注解的方式不太现实,可以把老事务类拷过来,声明成@Component 继续使用。

6、 Mybatis mapper文件

  mybatis-spring-boot-starter使用的mybatis版本如果与老项目中的不同。对于datetime类型的字段,不能使用createDate != ''判断是否为空。需要删除这样的代码。

7、页面传到后台的日期不会自动转成Java的Date类型,需要增加如下代码:

  @Bean
    public Converter<String, Date> addDateConvert() {
        return new Converter<String, Date>() {
            private static final String dateFormat = "yyyy-MM-dd HH:mm:ss";
            private static final String shortDateFormat = "yyyy-MM-dd";
            @Override
            public Date convert(String source) {
                if (StringUtils.isBlank(source)) {
                    return null;
                }
                source = source.trim();
                try {
                    if (source.contains("-")) {
                        SimpleDateFormat formatter;
                        if (source.contains(":")) {
                            formatter = new SimpleDateFormat(dateFormat);
                        } else {
                            formatter = new SimpleDateFormat(shortDateFormat);
                        }
                        Date dtDate = formatter.parse(source);
                        return dtDate;
                    } else if (source.matches("^\\d+$")) {
                        Long lDate = new Long(source);
                        return new Date(lDate);
                    }

                } catch (Exception e) {
                    log.error("addDateConvertException ", e);
                    throw new RuntimeException(String.format("parser %s to Date fail", source));
                }
                throw new RuntimeException(String.format("parser %s to Date fail", source));
            }
        };
    }

 

8、XML引入

  如果还有xml文件不方便一起改造,还是可以保留的。首先先把xml文件复制到resources文件夹下。然后在启动类或者在使用@Configuration注解的类上,使用@ImportResource("classpath:*****")把xml路径加进来。

posted @ 2019-03-29 11:55  闲人鹤  阅读(1018)  评论(0编辑  收藏  举报