@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…
@Autowired注入Spring Bean,则当前类必须也是Spring Bean才能调用它,不能用new xxx()来获得对象,这种方式获得的对象无法调用@Autowired注入的Bean. 1.类1,加入Spring Pool public class PersonServiceImpl implements PersonService{ public void save(){ System.out.println("This is save for test spring")…
转载: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…
当我们在非Controller类中应用service的方法是会报空指针,如图: 这是因为Spring MVC普通类或工具类中调用service报空null的解决办法(调用service报java.lang.NullPointerException) 按上述步骤解决完自己的工具类后,你会发现项目运行后仍然报空指针此时你需要在applicationContext.xml 配置文件中添加一行配置文件 如图: 对自己工具类所在的包进行注解扫描,使Spring能够识别自己上面所配置的注解…
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.Validate; import org.apache.poi.ss.formula.functions.T; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.lang.reflect.*; import java.util.HashMap; import…
工具类里的一个静态方法需要调用dao查询数据库,用普通的spring注解注入一直报空指针异常,不能找到这个dao.参考的http://busing.iteye.com/blog/899322 的文章解决的,很实用.一开始注入bean写到service层的配置文件里了还是不行,然后就写到dao层的配置文件,最后解决,想不明白.不知道如果是静态方法调用service层的话是不是要写到service的配置文件中. 最后感谢博主 : http://busing.iteye.com/blog/899322…
因为平时在调用service层时都是在controller中,有配置扫描注入,spring会根据配置自动注入所依赖的服务层. 但因我们写的工具类不属于controller层,所以当所写接口需要调用服务层是,常常会为NULL. 代码如下: //关于工具类调用服务层的方法 @Autowired //注入service层 private VTcTbdwdmService vTcTbdwdmService; //在spring初始化之前,初始化一个静态类 private static MulCondit…
在写一个工具类的时候,因为要用到yml中的自定义参数,使用@Value发现值不能正常注入,都显示为null: yml文件中的自定义格式 调用工具类的时候不能new的方式 要使用@Autowired的方式注入进来, new会导致部分环境未加载,尽可能舍弃new的方式,交付spring管理 而工具类也是需要交给spring管理.需要在工具类上加上 @Component注解然后注意一下的是 在springframework下不能@Autowired静态变量 所以在变量上不能有 static 怎么扫描注…
/** * @author: jerry * @Email: * @Company: * @Action: 日志处理工具类 * @DATE: 2016-9-19 */ @Component//泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注 public class LogUtil { @Autowired//注意这里非静态 private AdminLogService logService; private static LogUtil logUtil; @PostConstru…