spring 用了@@Component和@Resource注入 为什么属性还是空的
这是我的web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>SpringMailPhoneService</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:/bean.xml </param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list></web-app>
<?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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config /> <context:component-scan base-package="com.ums.presys" /> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"> </property> <property name="url" value="jdbc:oracle:thin:@localhost:1521:LEAKEY"> </property> <property name="username" value="scott"></property> <property name="password" value="tiger"></property> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <!--============================== 定时操作 ============================== --> <bean id="SchedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="searchAndSendTrigger" /> </list> </property> </bean> <bean id="searchAndSendScheduler" class="org.springframework.scheduling.quartz.JobDetailBean"> <property name="jobClass"> <value>com.ums.presys.scheduler.SearchAndSend</value> </property> </bean> <bean id="searchAndSendTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="searchAndSendScheduler"></property> <property name="cronExpression"> <value>10 17 03 * * ?</value> </property> </bean>
@Componentpublic class SearchAndSend extends QuartzJobBean{ private MessageService messageService; @Resource public void setMessageService(MessageService messageService) { this.messageService = messageService; } @Override protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException { messageService.sendMessage(); }}
@Componentpublic class MessageService { private MessageDao messageDao; @Resource public void setMessageDao(MessageDao messageDao) { this.messageDao = messageDao; } public void sendMessage(){ List<Message> listMessages = messageDao.selectMessages(); for (Message message : listMessages) { System.out.println(message); } }}