您的位置:首页 > 编程语言 > Java开发

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

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

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

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> 
<!--引入配置属性文件 -->
<context:property-placeholder location="classpath:config.properties" />

<!--自动扫描含有@Service将其注入为bean -->
<context:component-scan base-package="com.ly.bs.service" />
<!-- class annotation related... start -->
<context:component-scan base-package="com.ly.bs.redis" />
<context:component-scan base-package="com.ly.bs.job"/>
<!-- class annotation related... end -->
<context:annotation-config />

<bean id="dwrService" class="com.ly.bs.controller.AjaxController"/>

<bean id="quartzJob" class="com.ly.bs.job.TestJob">
<!--  <property name="userDataService" value="com.ly.bs..service.impl.UserDataServiceImpl">
</property> -->
</bean>
<!-- 定义调用对象和调用对象的方法 -->
<bean id="myJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject">
<ref bean="quartzJob"/>
</property>
<!-- 调用类中的方法 -->
<property name="targetMethod">
<value>send</value>
</property>
</bean>

<!-- <bean name="myJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
指定具体的job类
<property name="jobClass" value="com.ly.bs.controller.ScannerJobController" />
<property name="jobClass" value="quartzJob" />
指定job的名称
<property name="name" value="myJob" />
指定job的分组
<property name="group" value="jobs" />
必须设置为true,如果为false,当没有活动的触发器与之关联时会在调度器中删除该任务
<property name="durability" value="true"/>
指定spring容器的key,如果不设定在job中的jobmap中是获取不到spring容器的
<property name="applicationContextJobDataKey" value="applicationContext"/>
</bean> -->
<!-- 	<bean name="myJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
指定具体的job类
<property name="jobClass" value="com.ly.bs.job.ScannerJob" />
指定job的名称
<property name="name" value="myJob" />
指定job的分组
<property name="group" value="jobs" />
必须设置为true,如果为false,当没有活动的触发器与之关联时会在调度器中删除该任务
<property name="durability" value="true"/>
指定spring容器的key,如果不设定在job中的jobmap中是获取不到spring容器的
<property name="applicationContextJobDataKey" value="applicationContext"/>
</bean> -->

<!-- 定义触发器 -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="myJobDetail" />
<!-- 每一分钟执行一次 -->
<property name="cronExpression" value="0/15 * * * * ?" />
</bean>

<!-- 定义触发器 -->
<!-- 演示:一个job可以有多个trigger; -->
<!-- <bean id="cronTrigger2" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="myJobDetail" />
每一分钟执行一次
<property name="cronExpression" value="0 */1 * * * ?" />
</bean> -->

<!-- 定义调度器 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger" />
<!--   <ref bean="cronTrigger2" /> -->
</list>
</property>
</bean>

</beans>


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<UserData> 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<UserData> selectByPage(Page page) {
log.info("page:"+page);
return userDataMapper.selectByPage(page);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: