永发信息网

java spring+jpa @service 注解不起作用 我在业务层用了@service 注解 测试类里面得不到相应的Bean,求解释

答案:1  悬赏:20  手机版
解决时间 2021-02-27 11:08
  • 提问者网友:箛茗
  • 2021-02-26 23:18
测试代码(关键部分):
public static void setUpBeforeClass() throws Exception {
try {
ApplicationContext act = new ClassPathXmlApplicationContext(
"applicationContext.xml");
productSortService = (ProductSortService) act
.getBean("ProductSortServiceImpl");
} catch (RuntimeException e) {
e.printStackTrace();
}
}

配置文件:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">


class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">


class="org.springframework.orm.jpa.JpaTransactionManager">
ref="entityManagerFactory" />




bean代码:

@Service
@Transactional
public class ProductSortServiceImpl extends DaoSupport implements
ProductSortService {

}
最佳答案
  • 五星知识达人网友:山有枢
  • 2021-02-27 00:40
productSortService = (ProductSortService) act.getBean("ProductSortServiceImpl");
改为productSortService = (ProductSortService) act.getBean("productSortServiceImpl");
spring的注解@service默认的名字是驼峰命名法,你也可以指定bean的名字@service(“beanName”)。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯