在spring和springboot项目main方法调用service方法
目录
1、在spring中调用service方法
main方法:
-
public class App2 {
-
public static void main(String[] args) {
-
//读取Springboot配置文件
-
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
-
//通过类型来获取类
-
BookService bookService = (BookService) ctx.getBean(BookService.class);
-
System.out.println(bookService);
-
//执行service方法
-
bookService.save();
-
}
-
}
application:
-
-
<beans xmlns="http://www.springframework.org/schema/beans"
-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-
xmlns:context="http://www.springframework.org/schema/context"
-
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">
-
<!--开启注解支持-->
-
<context:component-scan base-package="com.rk"></context:component-scan>
-
-
</beans>
bookservice:
-
-
public class BookServiceImpl implements BookService {
-
-
private BookDao bookDao;
-
-
public void save() {
-
System.out.println("book service save ...");
-
bookDao.save();
-
}
-
public void setBookDao(BookDao bookDao) {
-
this.bookDao = bookDao;
-
}
-
}
2、在springboot中调用service方法
编写一个SpringUtil:
-
package com.yum;
-
-
import org.springframework.beans.BeansException;
-
import org.springframework.context.ApplicationContext;
-
import org.springframework.context.ApplicationContextAware;
-
import org.springframework.stereotype.Component;
-
-
-
public class SpringUtil implements ApplicationContextAware {
-
private static ApplicationContext applicationContext = null;
-
-
-
-
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
-
if(SpringUtil.applicationContext == null){
-
SpringUtil.applicationContext = applicationContext;
-
}
-
-
}
-
-
//获取applicationContext
-
public static ApplicationContext getApplicationContext() {
-
return applicationContext;
-
}
-
-
//通过name获取 Bean.
-
public static Object getBean(String name){
-
return getApplicationContext().getBean(name);
-
-
}
-
-
//通过class获取Bean.
-
public static <T> T getBean(Class<T> clazz){
-
return getApplicationContext().getBean(clazz);
-
}
-
-
//通过name,以及Clazz返回指定的Bean
-
public static <T> T getBean(String name,Class<T> clazz){
-
return getApplicationContext().getBean(name, clazz);
-
}
-
}
这样就可以从容器中获取组件了
main方法:
-
-
-
public class TestCont {
-
public static void main(String[] args) {
-
SpringApplication.run(TestCont.class, args);
-
ApplicationContext context = SpringUtil.getApplicationContext();
-
LiveDataService liveDataService = context.getBean(LiveDataService.class);
-
List<String> list = liveDataService.CheackGmv();
-
System.out.println(list);
-
System.out.println("完成");
-
}
-
}
3、SpringBoot中给静态常量注入配置文件中的值
配置文件.yml:
-
aliyun:
-
oss:
-
endpoint: oss-cn-hangzhou.aliyuncs.com
-
accessKeyId: xxx
-
secret: xxx
-
bucketName: xx-filerk
工具类:
-
/**
-
* @author :Rk.
-
* @date : 2022/11/23
-
*/
-
-
public class ConstantOssPropertiesUtils implements InitializingBean {
-
-
-
private String endpoint;
-
-
-
private String accessKeyId;
-
-
-
private String secret;
-
-
-
private String bucket;
-
-
public static String EDNPOINT;
-
public static String ACCESS_KEY_ID;
-
public static String SECRECT;
-
public static String BUCKET;
-
-
-
public void afterPropertiesSet() throws Exception {
-
EDNPOINT=endpoint;
-
ACCESS_KEY_ID=accessKeyId;
-
SECRECT=secret;
-
BUCKET=bucket;
-
}
-
}
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgakicb
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01