spring 使用注解,属性无法注入

xiangxg 2014-06-20 03:06:48
ssh框架整合。今天碰到了个奇怪的问题,先上代码
appilcationContx.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">


<!-- 注解的自动扫描,表示组件(如:@controler,@Service,@Repository,@Resource等)的扫描 ,注册到spring容器中-->
<context:component-scan base-package="com.softel.ele"></context:component-scan>

<!-- 创建由spring提供的sessionFactory,这是spring整合hibernate的核心 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>

<!-- 创建事务管理器,由spring负责创建 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 使用注解的形式管理事务 -->
<tx:annotation-driven transaction-manager="txManager"/>

</beans>


package com.softel.ele.serviceImpl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.softel.ele.dao.IElecTestDao;
import com.softel.ele.domain.ElecTest;
import com.softel.ele.service.IElecTestService;
@Service(IElecTestService.SERVICE_NAME)
@Transactional(readOnly=true)
public class ElecTestServiceImpl implements IElecTestService {
@Resource(name=IElecTestDao.SERVICE_NAME)
private IElecTestDao elecTestDao;
/**
* isolalation事务隔离级别
* propagation事务的传播行为
* @param el
*/
@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)
public void save(ElecTest el){
elecTestDao.save(el);
}
}



struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 修改访问链接的后缀名 -->
<constant name="struts.action.extension" value="do"></constant>
<!-- 设置开发模式,开发时输出更多的错误信息 -->
<constant name="struts.devMode" value="true"></constant>
<!-- 修改ui主题为简单主题 -->
<constant name="struts.ui.theme" value="simple"></constant>
<package name="system" extends="struts-default" namespace="/system">
<action name="elecTextAction_*" class="com.softel.ele.action.ELecTestAction" method="{1}">
<result name="success">/system/textAdd.jsp</result>
</action>

</package>
</struts>


package com.softel.ele.action;


import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;
import com.softel.ele.domain.ElecTest;
import com.softel.ele.service.IElecTestService;
@SuppressWarnings("serial")
@Controller("eLecTestAction")
@Scope(value="prototype")
public class ELecTestAction extends BaseAction implements ModelDriven<ElecTest>{
private ElecTest elecTest = new ElecTest();
@Resource(name=IElecTestService.SERVICE_NAME)
private IElecTestService elecTestService;
@Override
public ElecTest getModel() {
// TODO Auto-generated method stub
return elecTest;
}

public String save(){
elecTestService.save(elecTest);
return SUCCESS;
}




}



启动tomcat一切正常,用debug执行到ELecTestAction的save()方法时,发现elecTestService为null,
也就是
@Resource(name=IElecTestService.SERVICE_NAME)
private IElecTestService elecTestService;
属性没有注入,谢谢各位帮忙看下问题啊。

如果用下面这种方式,是行的通的
package test;

import java.util.Date;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.softel.ele.domain.ElecTest;
import com.softel.ele.service.IElecTestService;

public class TestService {
@Test
public void testSaveElecText(){
//加载类路径下的beans.xml
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取spring容器中的bean的id节点
IElecTestService elecTextService = (IElecTestService) ac.getBean(IElecTestService.SERVICE_NAME);
//保存
ElecTest elecText = new ElecTest();
elecText.setTextName("测试Service名称");
elecText.setTextDate(new Date());
elecText.setTextRemark("测试Service备注");
elecTextService.save(elecText);
}
}

...全文
2538 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
yeshihua 2016-10-17
  • 打赏
  • 举报
回复
他spring.xml 没有开启 annotion 机制 他只有 扫描包的配置 没有配置<context:annotation-config />
b0b045 2016-09-06
  • 打赏
  • 举报
回复
你这种垃圾居然还有脸来提问,呵呵,找到问题了就来一句“我勒个去,终于知道原因”,人类的渣渣,简称。。
SeanLiew 2016-06-29
  • 打赏
  • 举报
回复
求问楼主怎么解决的?我也遇到了这个问题
popezong 2016-02-25
  • 打赏
  • 举报
回复
引用 4 楼 xiangxg88 的回复:
我勒个去,终于知道原因
为什么啊
l564203965 2014-11-26
  • 打赏
  • 举报
回复
楼主怎么解决的啊 我也遇到这种问题了
浪里花 2014-06-21
  • 打赏
  • 举报
回复
com.softel.ele这里改成com.softel.ele.*没有扫描到子包下面的类
xiangxg 2014-06-20
  • 打赏
  • 举报
回复
我勒个去,终于知道原因
xiangxg 2014-06-20
  • 打赏
  • 举报
回复
web.xml
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <filter>
  	<filter-name>struts2</filter-name>
  	<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>struts2</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  <welcome-file-list>
  	<welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
xiangxg 2014-06-20
  • 打赏
  • 举报
回复
引用 1 楼 w3226327 的回复:
service没有加@Component
我加了@Service的啊,这个效果一样啊
桃子_ 2014-06-20
  • 打赏
  • 举报
回复
service没有加@Component

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧