spring注入静态对象属性时,因为虚拟机类加载问题,直接在属性上使用@Autowired 是不可以的。需要在属性对应的set方法上@Autowired,并且,set方法不能定义为static。
1.创建静态对象属性对应的类
package com.bluej.springj.service.impl; import org.springframework.stereotype.Service; import com.bluej.springj.service.LogService; @Service("logService") public class LogServiceImpl implements LogService { public void doLog() { System.out.println("LogServiceImpl.doLog"); } }