SpringMVC中bean的加载控制

简介: SpringMVC中bean的加载控制

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


SpringMVC相关bean(表现层bean)


Spring控制的bean

业务bean(Service)

功能bean(DateSource等)


SpringMVC相关bean加载控制

SpringMVC加载的bean对应的包均在com.itheima.controller包内


Spring相关bean加载控制

方式一:Spring加载的bean设定扫描范围为com.itheima,排除controller包内的bean

方式二:Spring加载的bean设定扫描范围为精准扫描,例如service包、dao包等

方式三:不区分Spring与SpringMVC的环境,加载到同一个环境中


方式一:

名称:@ComponentScan

类型:类注解

范例:

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
@Configuration
//设置spring配置类加载bean时的过滤规则,当前要求排除掉表现层对应的bean
//excludeFilters属性:设置扫描加载bean时,排除的过滤规则
//type属性:设置排除规则,当前使用按照bean定义时的注解类型进行排除
//classes属性:设置排除的具体注解类,当前设置排除@Controller定义的bean
@ComponentScan(value="com.itheima",
    excludeFilters = @ComponentScan.Filter(
        type = FilterType.ANNOTATION,
        classes = Controller.class
    )
)
public class SpringConfig {
}

属性:

excludeFilters:排除扫描路径中加载的bean,需要指定类别(type)与具体项(classes)

includeFilters:加载指定的bean,需要指定类别(type)与具体项(classes)


方式三:

bean的加载格式:

public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer {
    protected WebApplicationContext createServletApplicationContext() {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(SpringMvcConfig.class);
        return ctx;
    }
    protected WebApplicationContext createRootApplicationContext() {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(SpringConfig.class);
        return ctx;
    }
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
}

简化开发:

//web配置类简化开发,仅设置配置类类名即可
public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{SpringConfig.class};
    }
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{SpringMvcConfig.class};
    }
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
}
目录
相关文章
|
Java Spring
Spring整合SpringMVC时避免Spring加载两次bean的配置方法
Spring整合SpringMVC时避免Spring加载两次bean的配置方法
springmvc注入类 NoUniqueBeanDefinitionException: No qualifying bean of type [] is defined: expected single错误
在springmvc中注入服务时用@Service 当有两个实现类时都标明@Service后则会出现异常: nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.james.dao.impl.BaseDao] is defined: expected single matching bean but found 2:  这是因为都标明了@Service会自动注入,这时会导致不清楚实际运行时实例化哪一类。
1652 0
|
JSON Java 数据格式
SpringMVC 使用验证框架 Bean Validation(下)
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/catoop/article/details/51284638 本文接上一篇《SpringMVC 使用验证框架 Bean Validation(上)》: 四、Controller 普通参数验证与视图错误信息的展示 对于 form 表单提交绑定到对象的验证方式,上面已经介绍了。
1053 0
|
4月前
|
设计模式 前端开发 JavaScript
Spring MVC(一)【什么是Spring MVC】
Spring MVC(一)【什么是Spring MVC】
|
1月前
|
Java Apache vr&ar
springmvc报错 nested exception is org.mybatis.spring.MyBatisSystemException:
springmvc报错 nested exception is org.mybatis.spring.MyBatisSystemException:
16 0
|
6月前
|
前端开发 Java Go
Spring MVC 和 Spring Boot 的区别
Spring MVC 和 Spring Boot 的区别
110 0
|
9月前
|
Java Spring
springmvc中spring提供的中文乱码解决问题
可以解决浏览器的乱码问题
44 0
|
4月前
|
设计模式 前端开发 Java
Spring Boot之Spring MVC的工作原理 以及使用eclipse开发Spring MVC的Web应用实战(附源码)
Spring Boot之Spring MVC的工作原理 以及使用eclipse开发Spring MVC的Web应用实战(附源码)
45 0
|
4月前
|
设计模式 前端开发 Java
8:Spring MVC-Java Spring
8:Spring MVC-Java Spring
72 0