应用场景:再使用工具类时需要对数据库进行增删改查操作,我们需要在工具类中注入service类来调用其中的方法。

 

//把Judge类实例化到spring容器中
@Component
public class Judge{
	//注入service的类
	@Autowired
	private ZhangStatusService zhangStatusService;
	private static Judge JudgeService;
	
	public Judge() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	/* 被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器调用一次,
	类似于Servlet的inti()方法。被@PostConstruct修饰的方法会在构造函数之后,init()方法之前运行。*/
	@PostConstruct
	public void init() {
		System.out.println("实例化成功");
		JudgeService = this;
		JudgeService.zhangStatusService = this.zhangStatusService;
	}

	public static boolean compile(Integer id) throws Exception {
		//调用Service类的方法
		JudgeService.zhangStatusService.updateStatusJduge(id);
		
		}
	}
}

 

 

 

 

 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐