• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

在spring和springboot项目main方法调用service方法

武飞扬头像
Rk..
帮助1

目录

1、在spring中调用service方法

2、在springboot中调用service方法

3、SpringBoot中给静态常量注入配置文件中的值

1、在spring中调用service方法

main方法:

  1.  
    public class App2 {
  2.  
    public static void main(String[] args) {
  3.  
    //读取Springboot配置文件
  4.  
    ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
  5.  
    //通过类型来获取类
  6.  
    BookService bookService = (BookService) ctx.getBean(BookService.class);
  7.  
    System.out.println(bookService);
  8.  
    //执行service方法
  9.  
    bookService.save();
  10.  
    }
  11.  
    }

application:

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <beans xmlns="http://www.springframework.org/schema/beans"
  3.  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.  
    xmlns:context="http://www.springframework.org/schema/context"
  5.  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
  6.  
    <!--开启注解支持-->
  7.  
    <context:component-scan base-package="com.rk"></context:component-scan>
  8.  
     
  9.  
    </beans>

bookservice:

  1.  
    @Service
  2.  
    public class BookServiceImpl implements BookService {
  3.  
    @Autowired
  4.  
    private BookDao bookDao;
  5.  
     
  6.  
    public void save() {
  7.  
    System.out.println("book service save ...");
  8.  
    bookDao.save();
  9.  
    }
  10.  
    public void setBookDao(BookDao bookDao) {
  11.  
    this.bookDao = bookDao;
  12.  
    }
  13.  
    }

2、在springboot中调用service方法

编写一个SpringUtil:

  1.  
    package com.yum;
  2.  
     
  3.  
    import org.springframework.beans.BeansException;
  4.  
    import org.springframework.context.ApplicationContext;
  5.  
    import org.springframework.context.ApplicationContextAware;
  6.  
    import org.springframework.stereotype.Component;
  7.  
     
  8.  
    @Component
  9.  
    public class SpringUtil implements ApplicationContextAware {
  10.  
    private static ApplicationContext applicationContext = null;
  11.  
     
  12.  
     
  13.  
    @Override
  14.  
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  15.  
    if(SpringUtil.applicationContext == null){
  16.  
    SpringUtil.applicationContext = applicationContext;
  17.  
    }
  18.  
     
  19.  
    }
  20.  
     
  21.  
    //获取applicationContext
  22.  
    public static ApplicationContext getApplicationContext() {
  23.  
    return applicationContext;
  24.  
    }
  25.  
     
  26.  
    //通过name获取 Bean.
  27.  
    public static Object getBean(String name){
  28.  
    return getApplicationContext().getBean(name);
  29.  
     
  30.  
    }
  31.  
     
  32.  
    //通过class获取Bean.
  33.  
    public static <T> T getBean(Class<T> clazz){
  34.  
    return getApplicationContext().getBean(clazz);
  35.  
    }
  36.  
     
  37.  
    //通过name,以及Clazz返回指定的Bean
  38.  
    public static <T> T getBean(String name,Class<T> clazz){
  39.  
    return getApplicationContext().getBean(name, clazz);
  40.  
    }
  41.  
    }
学新通

这样就可以从容器中获取组件了

main方法:

  1.  
    @SpringBootApplication
  2.  
    @MapperScan(basePackages = {"com.yum.mapper"})
  3.  
    public class TestCont {
  4.  
    public static void main(String[] args) {
  5.  
    SpringApplication.run(TestCont.class, args);
  6.  
    ApplicationContext context = SpringUtil.getApplicationContext();
  7.  
    LiveDataService liveDataService = context.getBean(LiveDataService.class);
  8.  
    List<String> list = liveDataService.CheackGmv();
  9.  
    System.out.println(list);
  10.  
    System.out.println("完成");
  11.  
    }
  12.  
    }

3、SpringBoot中给静态常量注入配置文件中的值

配置文件.yml:

  1.  
    aliyun: #自定义配置
  2.  
    oss:
  3.  
    endpoint: oss-cn-hangzhou.aliyuncs.com
  4.  
    accessKeyId: xxx
  5.  
    secret: xxx
  6.  
    bucketName: xx-filerk

 工具类:

  1.  
    /**
  2.  
    * @author :Rk.
  3.  
    * @date : 2022/11/23
  4.  
    */
  5.  
    @Component
  6.  
    public class ConstantOssPropertiesUtils implements InitializingBean {
  7.  
     
  8.  
    @Value("${aliyun.oss.endpoint}")
  9.  
    private String endpoint;
  10.  
     
  11.  
    @Value("${aliyun.oss.accessKeyId}")
  12.  
    private String accessKeyId;
  13.  
     
  14.  
    @Value("${aliyun.oss.secret}")
  15.  
    private String secret;
  16.  
     
  17.  
    @Value("${aliyun.oss.bucket}")
  18.  
    private String bucket;
  19.  
     
  20.  
    public static String EDNPOINT;
  21.  
    public static String ACCESS_KEY_ID;
  22.  
    public static String SECRECT;
  23.  
    public static String BUCKET;
  24.  
     
  25.  
    @Override
  26.  
    public void afterPropertiesSet() throws Exception {
  27.  
    EDNPOINT=endpoint;
  28.  
    ACCESS_KEY_ID=accessKeyId;
  29.  
    SECRECT=secret;
  30.  
    BUCKET=bucket;
  31.  
    }
  32.  
    }
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgakicb
系列文章
更多 icon
同类精品
更多 icon
继续加载