当前位置: 首页 > news >正文

创建一个网站流程网站技术支持 新锐网络

创建一个网站流程,网站技术支持 新锐网络,网站建站步骤,建网站需要有啥能力在实际开发中#xff0c;SpringMVC 负责扫描和加载 Controller 层的 Bean 对象#xff0c;而业务层和数据层等其他模块的 Bean 则由 Spring 框架负责扫描和加载。那么#xff0c;如何控制 Spring 仅加载除了 Controller 层之外的其他 Bean 呢#xff1f;为了解决这个问题SpringMVC 负责扫描和加载 Controller 层的 Bean 对象而业务层和数据层等其他模块的 Bean 则由 Spring 框架负责扫描和加载。那么如何控制 Spring 仅加载除了 Controller 层之外的其他 Bean 呢为了解决这个问题通常可以采取以下两种方案来避免 Spring 错误地加载 SpringMVC 的 Bean 直接指定在 Spring 的配置中精确设定扫描范围只加载特定的包例如 service 包和 dao 包。过滤排除将 Spring 的扫描范围设定为 com.it假设 groupId 为 com.it并排除 controller 包内的 Bean。 当然在实际开发中有时也会选择不区分 Spring 和 SpringMVC 的环境将它们加载到同一个环境中。这里创建一个入门版的 SpringMVC 项目进行演示创建过程可以参考 SpringMVC 快速入门。假设现在的 SpringMVC 配置类和启动配置类如下 Configuration ComponentScan(com.it.controller) public class SpringMvcConfig { }public class InitConfig extends AbstractDispatcherServletInitializer {// 用于加载 SpringMVC 容器配置Overrideprotected WebApplicationContext createServletApplicationContext() {AnnotationConfigWebApplicationContext ctx new AnnotationConfigWebApplicationContext();ctx.register(SpringMvcConfig.class);return ctx;}// 设置哪些请求归属 SpringMVC 处理Overrideprotected String[] getServletMappings() {// 设置所有的请求归属 SpringMVC 处理return new String[]{/};}// 加载 Spring 容器配置Overrideprotected WebApplicationContext createRootApplicationContext() {AnnotationConfigWebApplicationContext ctx new AnnotationConfigWebApplicationContext();ctx.register(SpringConfig.class);return ctx;} }方式 1直接指定 在 Spring 配置类中通过 ComponentScan 直接指定多个待扫描模块 Configuration ComponentScan({com.it.service, com.it.dao}) public class SpringConfig { }方式 2过滤排除 在 Spring 配置类中通过 ComponentScan 排除不扫描模块 Configuration ComponentScan(value com.it,excludeFilters ComponentScan.Filter(type FilterType.ANNOTATION,classes Controller.class) ) public class SpringConfig { }这里按照注解类型排除不需要扫描的模块说明一下ComponentScan 存在两个重要的属性 excludeFilters排除扫描路径中加载的 Bean需要指定类别type与具体项classesincludeFilters加载指定的 Bean需要指定类别type与具体项classes 测试 在 com.it 下创建一个 App 类观察运行后是否能获取到 Controller 层 Bean public class App {public static void main(String[] args) {AnnotationConfigApplicationContext ctx new AnnotationConfigApplicationContext(SpringConfig.class);System.out.println(ctx.getBean(UserController.class));} }测试前需要将 SpringMvcConfig 类上的 Configuration 注解先注释掉因为 SpringConfig 在设置扫描 com.it 包时如果包中某个类添加了 Configuration 注解那么这个类被扫描的同时该类 ComponentScan 指定的 com.it.controller 也会被扫描这样会影响 Bean 加载控制的测试效果。 优化 在实际开发中可以优化 InitConfig 类中加载 Spring 和 SpringMVC 容器配置的过程。通过继承 AbstractDispatcherServletInitializer 类的子类使得配置过程更加简洁 public class InitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {Overrideprotected Class?[] getRootConfigClasses() {return new Class[]{SpringConfig.class};}Overrideprotected Class?[] getServletConfigClasses() {return new Class[]{SpringMvcConfig.class};}Overrideprotected String[] getServletMappings() {return new String[]{/};} }
http://www.hkea.cn/news/14443476/

相关文章:

  • 长春网站制作培训做企业网站用什么
  • 天津建设银行招聘网站学校网站建设制度
  • 手机网站建设的图片北京做网站公司有哪些
  • 网站制作设计方案百度推广渠道
  • 中国移动官方网站官网后盾网原创实战网站建设教程1-15
  • 昆明网站建设公司猫咪科技南京自助建站模板
  • 设计网站推荐外网做太空泥的几分钟网站
  • 做网站必须先买域名吗网页代理网站
  • 俄语网站推广如何使用网站模板
  • 天津做网站找津坤科技wordpress 获取文章分类id
  • 海安网站优化在货源网站自己拿样 加盟 做代理 哪个比较好?
  • 网站建设公司douyanet南京建设工程招聘信息网站
  • 安徽金路建设集团有限公司网站竞价账户托管公司哪家好
  • 广东华迪工程建设监理公司网站ftp上传网站之后
  • 手机建网站需要多少钱谷歌做网站
  • php网站上传linux vps网站搬家命令
  • 岳阳企业网站定制开发新媒体运营的相关优势
  • 企业网站建设要注意广东广州有几个区
  • 网站建设的ci设计指的是什么同城同镇网站建设
  • asp钓鱼网站开发深圳创业园
  • 有没有做美食的小视频网站多人在线协作网站开发
  • 广告设计毕业设计全国seo搜索排名优化公司
  • 网站流量好难做西安建设网站制作
  • 网站建设与维护 实训东莞微网站建设报价
  • 琼海做网站公司wordpress 信用卡支付
  • 邮箱的官方网站注册建筑给排水识图教程久久建筑网
  • 快速做网站的方法网站修改文案
  • 成都市做网站的公司有没有安全一点的网站
  • 绵阳汽车网站制作wordpress能不能导入html
  • 美食个人网站设计作品做网站中山