栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring环境配置

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring环境配置

Spring环境配置 构建Spring环境的两种方式
  1. 通过@SpringBootApplication加载配置文件,构建spring上下文环境

    ​ 核心 : 构建 ApplicationContext , 然后通过 ApplicationContext 获取bean

    @SpringBootApplication(scanbasePackages ={
            "com.pachira.xkz.hh"
    })
    public class Mainhh {
        public static void main(String[] args) throws InterruptedException {
    
    
            ApplicationContext context = SpringApplication.run(Mainhh.class, args);
            SpringManager manager = context.getBean(SpringManager.class);
            for(int i=0;i<1000;i++) {
                ArchiveNode node = new ArchiveNode();
                node.setName(" " + i );
                System.out.println(node.toString());
                manager.doIt(node);
            }
            System.out.println("执行成功");
        }
    }
    
  2. 通过 AnnotationConfigApplicationContext,和spring环境文件springConfig 配置 ApplicationContext

SpringConfig.java 配置spring环境

package com.pachira.xkz.hh;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.FileSystemResource;

@Configuration
@ComponentScan(basePackages = {
        "com.pachira.xkz.hh"
})
public class SpringConfig {

    @Bean
    public static PropertySourcesPlaceholderConfigurer createPlaceHolderCOnfigure() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Bean
    public SpringManager getManager() {
        return new SpringManager();
    }

    @Bean
    public ArchiveDisPactcher getDispatcher() {
        return new ArchiveDisPactcher();
    }

//有配置文件的情况   SpringConfig要有Path属性用来设置配置文件
//
//    @Bean
//    public static PropertySourcesPlaceholderConfigurer createPlaceholderConfigurer() {
//        PropertySourcesPlaceholderConfigurer configurer =
//                new PropertySourcesPlaceholderConfigurer();
//        configurer.setLocation(new FileSystemResource(configPath));
//        configurer.setFileEncoding("UTF-8");
//        return configurer;
//    }

}

    public static void main(String[] args) {
        ApplicationContext context  = new AnnotationConfigApplicationContext(SpringConfig.class);

        SpringManager manager = context.getBean(SpringManager.class);

        for(int i=0;i<1000;i++) {
            ArchiveNode node = new ArchiveNode();
            node.setName("** " + i);
            manager.doIt(node);
        }

        System.out.println("执行成功");

    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/573059.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号