Spring的XML配置的bean调用注解方式的bean

最近在研究spring的定时器quartz,定时器的使用没什么问题,但是现在我有一个需求,就是监控数据库中某个表中数据变化,采用mybatis,访问数据库的时候,Service层采用注解形式,而定时器相关的bean采用XML配置。因此,就会遇到Spring的XML配置的bean调用注解方式的bean问题。

第一次的时候,我讲quartz的配置文件与spring的配置文件分开存放,运行程序后,总是显示Job类中注入的Service对象为空,上网查询原因,有些人说quartz自己管自己的bean,没有放在spring容器中,因此没法注入,于是我想到一个死办法,就是讲quartz的配置放到spring配置文件中,试过之后,就可以访问。




	
	

	
	
	
	
	
	
	
	
	  
	
	
           
	
        
        
            
            
                
            
            
            
                send
            
        
	 
	

	
	
	
		
		
		
	
	
	
	
	
	
	
	
	    
	        
	            
	          
	        
	    
	


package com.ly.bs.job;

import org.springframework.beans.factory.annotation.Autowired;

import com.ly.bs.service.UserDataService;

public class TestJob {
	 
	 @Autowired
	 private UserDataService userDataService;
	 
	 public void send() {
         System.out.println("我的一个任务测试。helloWord!");
         if(userDataService!=null){
        	 System.out.println(userDataService.selectUserDataById(1));
         }
	 }
}

package com.ly.bs.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.ly.bs.bean.Page;
import com.ly.bs.dao.UserDataMapper;
import com.ly.bs.entities.UserData;
import com.ly.bs.service.UserDataService;

@Service("userDataService")
public class UserDataServiceImpl implements UserDataService{
	
	@Autowired
	private UserDataMapper userDataMapper;
	
	Logger log=LoggerFactory.getLogger(UserDataServiceImpl.class);
	
	public int insert(UserData userData) {
		// TODO Auto-generated method stub
		return userDataMapper.insert(userData);
	}
	
	//@Cacheable("selectUserDataById")
	public UserData selectUserDataById(int id) {
		// TODO Auto-generated method stub
		log.debug("selectUserDataById:"+id);
		return userDataMapper.selectUserDataById(id);
	}

	public List selectAllUserData() {
		// TODO Auto-generated method stub
		return userDataMapper.selectAllUserData();
	}

	public int update(UserData userData) {
		// TODO Auto-generated method stub
		return userDataMapper.update(userData);
	}

	public int deleteUserDataById(int id) {
		// TODO Auto-generated method stub
		return userDataMapper.deleteUserDataById(id);
	}

	@Override
	public List selectByPage(Page page) {
		log.info("page:"+page);
		return userDataMapper.selectByPage(page);
	}
	
	
}


你可能感兴趣的:(springmvc,mybatis)