静态工具类中使用注解注入service】的更多相关文章

转载:http://blog.csdn.net/p793049488/article/details/37819121 一般需要在一个工具类中使用@Autowired 注解注入一个service.但是由于工具类方法一般都写成static,所以直接注入就存在问题. 注:Spring工厂要有这个bean. 使用如下方式可以解决: /** * */ package cn.ffcs.drive.common.util; import javax.annotation.PostConstruct; imp…
一般需要在一个工具类中使用@Autowired 注解注入一个service.但是由于工具类方法一般都写成static,所以直接注入就存在问题. 使用如下方式可以解决: /** * */ package cn.ffcs.drive.common.util; import javax.annotation.PostConstruct; import javax.servlet.http.HttpServletRequest; import org.slf4j.Logger; import org.s…
/** * @author: jerry * @Email: * @Company: * @Action: 日志处理工具类 * @DATE: 2016-9-19 */ @Component//泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注 public class LogUtil { @Autowired//注意这里非静态 private AdminLogService logService; private static LogUtil logUtil; @PostConstru…
静态属性不能直接注入,可以通过其set方法进行注入.(注意生成的set方法需要去掉static). 在工具类里直接注入RedisTemplate,两种方法: (1)使用@Autowired private static RedisTemplate redisTemplate; @Autowired public void setRedisTemplate(RedisTemplate redisTemplate) { JwtUtil.redisTemplate = redisTemplate; }…
在SpringMVC框架中,我们经常要使用@Autowired注解注入Service或者Mapper接口,我们也知道,在Controller层中注入service接口,在service层中注入其它的service接口或者mapper接口都是可以的,但是如果我们要在我们自己封装的一些类中或者说非controller普通类中使用@Autowired注解注入Service或者Mapper接口,直接注入是肯定注入不成功的,当我们遇到这样的问题,我们就要想办法解决了. //Component注解不用解释了…
有时候我们会出现无法用注解 @Autowired 注入bean的情况,这个时候可以 通过contextLoader获取 WebApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext(); BeanService beanService = (BeanService)ctx.getBean("beanID"); 这里的beanID为xml文件中对应的<bean id="beanID&qu…
在xml中 <bean id="messageUtil" class="org.ldd.ssm.hangyu.utils.MessageUtil" init-method="init" > </bean> 在MessageUtil.java中 public class MessageUtil { @Autowired private ChatMapper chatMapper; private static Message…
记录一下,防止忘记. 要求每次生成一个和数据库不重复的组队码,于是就想在工具类中加入service注入 方法1(红框是注意的地方)…
1. 使用@Component注解标记工具类MailUtil: 2. 使用@Autowired注入我们需要的bean: 3. 在工具类中编写init()函数,并使用@PostConstruct注解标记工具类,初始化Bean: @Component public class MailUtil { @Autowired private JavaMailSenderImpl javaMailSender; public static MailUtil mailUtil; @PostConstruct…
@Component //此处注解不能省却(0) 1 public class NtClient { 2 /** 3 * 日志 4 */ 5 private static String clazzName = NtClient.class.getName(); 6 /** 7 * 此处是要使用的service需要spring注入(1) 8 */ 9 @Autowired 10 private NotifyTimeService notifyTimeService; 11 private stat…
@Component// 1.将工具类声明为spring组件,这个必须不能忘 public class TestUtils { //2.自动注入 @Autowired private ItemService itemService; // 3.静态初使化当前类 public static TestUtils testUtils; // 4.在方法上加上注解@PostConstruct,这样方法就会在Bean初始化之后被Spring容器执行(注:Bean初始化包括,实例化Bean,并装配Bean的…
[转] 使用场景:在一个静态方法中,如何使用以下注入: @Autowired private ItemMapper itemMapper; @Component public class TestUtils { @Autowired private ItemService itemService; @Autowired private ItemMapper itemMapper; public static TestUtils testUtils; @PostConstruct public v…
当我们在非Controller类中应用service的方法是会报空指针,如图: 这是因为Spring MVC普通类或工具类中调用service报空null的解决办法(调用service报java.lang.NullPointerException) 按上述步骤解决完自己的工具类后,你会发现项目运行后仍然报空指针此时你需要在applicationContext.xml 配置文件中添加一行配置文件 如图: 对自己工具类所在的包进行注解扫描,使Spring能够识别自己上面所配置的注解…
Static那些事儿 Static关键字 被static修饰的变量成为静态变量(类变量) 作用:是一个修饰符,用于修饰成员(成员变量,成员方法) 1.被static修饰后的成员变量只有一份 2.当成员被static修饰之后,多了一种访问方式,除了可以被对象调用之外还可以被雷鸣直接调用(类名.静态成员) static关键字,修饰变量,只保存最后一个值,立即改,立即用. static关键字是一个修饰符说白了意思就是用static修饰成员以后,这个成员就变成大家共有的了 ,任意一个对象可以调用它并且也…
工具类中调用数据.但是由于工具类方法一般都写成static,所以直接注入就存在问题. 所以写成了这样: package com.rm.framework.core; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.stereotype…
Springboot中如果希望在Utils工具类中,使用到我们已经定义过的Dao层或者Service层Bean,可以如下编写Utils类: 1. 使用@Component注解标记工具类StatisticsUtils: 2. 使用@Autowired(@Autowired和@Resource的区别不再介绍)注入我们需要的bean: 3. 在工具类中编写init()函数,并使用@PostConstruct注解标记工具类,初始化Bean: public class StatisticsUtils {…
参考<Spring普通类获取并调用Spring service方法>,网址:https://blog.csdn.net/jiayi_0803/article/details/68924558 在Spring MVC中,Controller中使用service只需使用注解@Resource/@Autowired就行,但是一般类(即不使用@Controller注解的类)要用到service时,Spring中的Service通过new实例化的对象脱离了Spring容器的管理,获取不到注解的属性值,所…
场景 需求是在窗体加载完成后掉用工具类的方法,工具类中获取窗体的多个控件对象进行赋值. 注: 博客主页: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 获取编程相关电子书.教程推送与免费下载. 实现 新建一个窗体程序,在窗体Forn1中拖拽一个label控件和TextBox控件. 然后进入到窗体的代码中 在构造方法前声明静态类变量 public static Form1 form1 = null; 在构造方法中将当前窗体赋值给上面声…
HttpTool.java 该类为java源生态的http 请求工具,不依赖第三方jar包 ,即插即用. package kingtool; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOEx…
1. Arrays工具类中asList()方法的使用 public static <T> List<T> asList(T... a): 把数组转成集合 注意事项: 虽然可以把数组转成集合,但是集合的长度不能改变. 2. 代码示例: (1) package cn.itcast_03; import java.util.Arrays; import java.util.List; /* * public static <T> List<T> asList(T.…
AQS AQS(AbstractQueuedSynchronizer)是 java.util.concurrent的基础.J.U.C中宣传的封装良好的同步工具类Semaphore.CountDownLatch.ReentrantLock.ReentrantReadWriteLock.FutureTask等虽然各自都有不同特征,但是简单看一下源码,每个类内部都包含一个如下的内部类定义: abstract static class Sync extends AbstractQueuedSynchro…
与Arrays一样,Collections类中也有一些实用的static方法. (1) 排序操作 reverse(List list):反转指定List集合中元素的顺序 shuffle(List list):对List中的元素进行随机排序(洗牌) sort(List list):对List里的元素根据自然升序排序 sort(List list, Comparator c):自定义比较器进行排序 swap(List list, int i, int j):将指定List集合中i处元素和j出元素进行…
今天在公司看到同事写的代码,无意发现在判断字符串类型时,使用的是StringUtils工具类中的isEmpty()去判断如下所示 @RequestMapping(value = "/pub/feebasisinfo/combcost/list", method = RequestMethod.POST) public Result list(@RequestBody CombCostParam param) { printRequestParam(param); PageUtil<…
public interface XxxService { // a -> b void a(); void b(); } @Slf4j public class XxxServiceImpl implements XxxService { @Override public void a() { log.info("a()"); // 注意这样调用方法,b方法不是通过代理类去访问的 // 在Spring中,同一个类中一个方法去调用(不管有没有注解)另外一个有注解(比如@Async…
今天编写了个工具类需要用到service成和dao层的代码 如下: //第一步:需要将工具类注入到容器中 @Component public class RuleUtils { ​ //第二部注入 @Autowired private BsVehicleInfoServiceImpl bsVehicleInfoImpl; @Autowired private BsAlarmInfoMapper alarmInfoMapper; //第三步 建一个静态的本类 private static Rule…
在写一个工具类的时候,因为要用到yml中的自定义参数,使用@Value发现值不能正常注入,都显示为null: yml文件中的自定义格式 调用工具类的时候不能new的方式 要使用@Autowired的方式注入进来, new会导致部分环境未加载,尽可能舍弃new的方式,交付spring管理 而工具类也是需要交给spring管理.需要在工具类上加上 @Component注解然后注意一下的是 在springframework下不能@Autowired静态变量 所以在变量上不能有 static 怎么扫描注…
  记录:在实体类中加入@Component注解和@Autowired注解时Service不能注入成功. @Component //把普通pojo实例化到spring容器中 0 public class MyUtil{ // 这里是需要注入的Service ① @Autowired private MyService myService; private static MyUtil myUtil; //初始化 ② @PostConstruct public void init() { myUti…
场景:在ContextLoaderListener子类中加载job,为JobFactory的实现类声明@Component后,在ContextLoaderListener子类中为scheduler设置JobFactory.(主要解决的问题:在spring与quartz调用job时,job中无法读取注解类,实现注入) 步骤一: ContextLoaderListener子类中contextInitialized方法中代码如下: super.contextInitialized(event); ap…
  记录:在实体类中加入@Component注解和@Autowired注解时Service不能注入成功. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 @Component //把普通pojo实例化到spring容器中 0  public class MyUtil{      // 这里是需要注入的Service ①     @Autowired      private MyService myService;      private stat…
2018-07-08 获取数组中的最大值 某个数字在数组中第一次出现时的索引 制作说明书的过程 对工具类的使用 获取数组中的最大值 获取数字在数组中第一次出现的索引值 API的使用过程 Math…