spring开发, @Service注解的bean初始化失败

ironpearl 2015-06-06 05:15:46
service接口
@Service
public interface UserService {
public void transfer();
}


service实现类
@Service
public class UserServiceImpl implements UserService {

@Autowired
private UserDao userDao;

@Override
@Transactional(isolation=Isolation.READ_COMMITTED,propagation=Propagation.REQUIRED)
public void transfer() {
userDao.transfer();
}
}


ApplicationContext配置文件片段
<!--隐式地向 Spring 容器注册 -->
<context:component-scan base-package="com.jetsen.user"/>
<!-- 注解式事务配置 -->
<tx:annotation-driven transaction-manager="txManager"/>
<!--读取配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />


<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="${initialSize}"></property>
<!-- 连接池最大数量 -->
<property name="maxTotal" value="${maxTotal}"></property>
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="${maxIdle}"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${minIdle}"></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWaitMillis" value="${maxWaitMillis}"></property>
</bean>

<!-- 事务管理器 -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

<bean id="userDao" class="com.jetsen.user.dao.UserDao">
<property name="dataSource" ref="dataSource" />
</bean>


测试运行类
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ctx = new ClassPathXmlApplicationContext("ApplicationContext.xml");
UserService userService = ctx.getBean("userServiceImpl",UserServiceImpl.class);
userService.transfer();
}


但运行是报错,如下
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@4e4dc532: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,dataSource,txManager,userDao,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'userServiceImpl' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:575)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1111)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:276)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1123)
at com.jetsen.user.UserTest.main(UserTest.java:14)
...全文
1569 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
程序永无止境 2018-01-12
  • 打赏
  • 举报
回复
main方法启动不了注解的
独家de记忆 2018-01-12
  • 打赏
  • 举报
回复
引用 5 楼 ironpearl 的回复:
[quote=引用 3 楼 z345434645 的回复:] @service("userServiceImpl")这样用 而且我觉得你得接口也是没必要注解的,因为实例化都是用的实现类,多余了,可能会使代码出问题。
@service("userServiceImpl"),你这个只是给他指定一个别名,不指定时它就默认这个值。 试了根据类型取bean时,改成接口的类型就OK了。
ApplicationContext ctx = new ClassPathXmlApplicationContext("ApplicationContext.xml");
		UserService userService = ctx.getBean(UserService.class);
		userService.transfer();
但这样又来了个问题,如果有两个实现类时,这样取就报错,找到了两个类
Exception in thread "main" org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.jetsen.user.service.UserService] is defined: expected single matching bean but found 2: userServiceImpl,userServiceImpl2
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:290)
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1127)
	at com.jetsen.user.UserTest.main(UserTest.java:14)
[/quote] 接口上面的注解去掉,只需要注解impl就行了。
qq965672903 2018-01-12
  • 打赏
  • 举报
回复
若果你定义了id 那就采用
UserService userService = ctx.getBean("userServiceImpl",UserService.class);
如果你没有定义id 那就采用
UserService userService = ctx.getBean(UserService.class);
还有一点就是你的userserviceImpl类里自动装配了UserDao,你就得首先得声明这个userDao,方式两种一个是在xml中定义<bean id="xxx' class="xxx"></bean>另一个是在userDAo添加注解
龙千囍 2017-04-08
  • 打赏
  • 举报
回复
我的包名写错了在xml文件中配置的包扫描没起作用就一直报这个错改好后就没有问题了
ironpearl 2015-06-08
  • 打赏
  • 举报
回复
引用 6 楼 Inhibitory 的回复:
如果 id 和 name 都不指定,那么 Spring 容器会为 Bean 生成一个 id,生成规则: 按 Bean 定义的顺序,在类的全路径名加上 #序号 (序号从 0 开始,第一个 bean 的别名是类的全路径名,所以可以使用类的全路径名来获取),如: com.xtuer.beans.User#0 (com.xtuer.beans.User) com.xtuer.beans.User#1 com.xtuer.beans.User#2 com.xtuer.beans.User#3
不知你这样写法,你那边经过测试是否可行?? @Service如果不指定value值,将默认以类名(首字母为小写)作为别名。 以下这种写法OK,但不解为啥getbean时指定为UserService接口,而指定为userServiceImpl就报错
		ApplicationContext ctx = new ClassPathXmlApplicationContext("ApplicationContext.xml");
		UserService userService = ctx.getBean("userServiceImpl",UserService.class);
		userService.transfer();
潘少博 2015-06-08
  • 打赏
  • 举报
回复
ctx.getBeansOfType(UserServiceImpl.class)
Inhibitory 2015-06-08
  • 打赏
  • 举报
回复
我是在 XML 里定义的 Bean,没有给定 id and name,用@Service的方式没注意过。
Inhibitory 2015-06-07
  • 打赏
  • 举报
回复
如果 id 和 name 都不指定,那么 Spring 容器会为 Bean 生成一个 id,生成规则: 按 Bean 定义的顺序,在类的全路径名加上 #序号 (序号从 0 开始,第一个 bean 的别名是类的全路径名,所以可以使用类的全路径名来获取),如: com.xtuer.beans.User#0 (com.xtuer.beans.User) com.xtuer.beans.User#1 com.xtuer.beans.User#2 com.xtuer.beans.User#3
ironpearl 2015-06-07
  • 打赏
  • 举报
回复
引用 3 楼 z345434645 的回复:
@service("userServiceImpl")这样用 而且我觉得你得接口也是没必要注解的,因为实例化都是用的实现类,多余了,可能会使代码出问题。
@service("userServiceImpl"),你这个只是给他指定一个别名,不指定时它就默认这个值。 试了根据类型取bean时,改成接口的类型就OK了。
ApplicationContext ctx = new ClassPathXmlApplicationContext("ApplicationContext.xml");
		UserService userService = ctx.getBean(UserService.class);
		userService.transfer();
但这样又来了个问题,如果有两个实现类时,这样取就报错,找到了两个类
Exception in thread "main" org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.jetsen.user.service.UserService] is defined: expected single matching bean but found 2: userServiceImpl,userServiceImpl2
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:290)
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1127)
	at com.jetsen.user.UserTest.main(UserTest.java:14)
ironpearl 2015-06-07
  • 打赏
  • 举报
回复
引用 2 楼 Inhibitory 的回复:
上面的是错的。 如果没有定义 bean 的 id,用类的全路径名才可以取到 bean,如下
UserService userService = ctx.getBean("com.jetsen.user.UserServiceImpl", UserServiceImpl.class);
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'com.jetsen.user.service.UserServiceImpl' is defined
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:575)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1111)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:276)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1123)
	at com.jetsen.user.UserTest.main(UserTest.java:14)
邹邹wl 2015-06-06
  • 打赏
  • 举报
回复
@service("userServiceImpl")这样用 而且我觉得你得接口也是没必要注解的,因为实例化都是用的实现类,多余了,可能会使代码出问题。
Inhibitory 2015-06-06
  • 打赏
  • 举报
回复
上面的是错的。 如果没有定义 bean 的 id,用类的全路径名才可以取到 bean,如下
UserService userService = ctx.getBean("com.jetsen.user.UserServiceImpl", UserServiceImpl.class);
Inhibitory 2015-06-06
  • 打赏
  • 举报
回复
试试 UserService userService = ctx.getBean("userService",UserServiceImpl.class);

81,090

社区成员

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

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