目录
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:
<?xml version="1.0" encoding="UTF-8"?>
<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:
@Service
public class BookServiceImpl implements BookService {
@Autowired
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;
@Component
public class SpringUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext = null;
@Override
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方法:
@SpringBootApplication
@MapperScan(basePackages = {"com.yum.mapper"})
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
*/
@Component
public class ConstantOssPropertiesUtils implements InitializingBean {
@Value("${aliyun.oss.endpoint}")
private String endpoint;
@Value("${aliyun.oss.accessKeyId}")
private String accessKeyId;
@Value("${aliyun.oss.secret}")
private String secret;
@Value("${aliyun.oss.bucket}")
private String bucket;
public static String EDNPOINT;
public static String ACCESS_KEY_ID;
public static String SECRECT;
public static String BUCKET;
@Override
public void afterPropertiesSet() throws Exception {
EDNPOINT=endpoint;
ACCESS_KEY_ID=accessKeyId;
SECRECT=secret;
BUCKET=bucket;
}
}