首页 > 其他分享 >controller加载控制与业务bean加载控制

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

时间:2023-02-26 13:33:06浏览次数:34  
标签:bean springframework controller context org import com itheima 加载

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 {
}

标签:bean,springframework,controller,context,org,import,com,itheima,加载
From: https://www.cnblogs.com/lin513/p/17156549.html

相关文章