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路徑加進來。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM