controller加载控制与业务bean加载控制

1.因功能的不同,如何避免Spring错误加载到SpringMVC的bean——加载Spring控制的bean的时候排除掉SpringMVC控制的bean。

 

package com.itheima.config;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;

public class ServletInitConfig extends AbstractDispatcherServletInitializer {

    protected WebApplicationContext createServletApplicationContext() {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(springMvcConfig.class);
        return ctx;
    }

    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    protected WebApplicationContext createRootApplicationContext() {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(springConfig.class);
        return ctx;
    }
}
package com.itheima.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.itheima.controller")
public class springMvcConfig {
}
package com.itheima.config;

import com.itheima.controller.UserController;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;

@Configuration
//@ComponentScan({"com.itheima.dao","com.itheima.service"})
@ComponentScan(value = "com.itheima",//排除controller中的bean
    excludeFilters = @ComponentScan.Filter(
            type = FilterType.ANNOTATION,
            classes = UserController.class
    )

    )
public class springConfig {
}
posted @ 2023-02-26 13:30  会秃头的小白  阅读(35)  评论(0编辑  收藏  举报