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)

解决方案 »

  1.   

    试试 UserService userService = ctx.getBean("userService",UserServiceImpl.class);
      

  2.   

    上面的是错的。
    如果没有定义 bean  的 id,用类的全路径名才可以取到 bean,如下
    UserService userService = ctx.getBean("com.jetsen.user.UserServiceImpl", UserServiceImpl.class);
      

  3.   

    @service("userServiceImpl")这样用
    而且我觉得你得接口也是没必要注解的,因为实例化都是用的实现类,多余了,可能会使代码出问题。
      

  4.   


    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)
      

  5.   


    @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)
      

  6.   

    如果 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
      

  7.   


    不知你这样写法,你那边经过测试是否可行??
    @Service如果不指定value值,将默认以类名(首字母为小写)作为别名。以下这种写法OK,但不解为啥getbean时指定为UserService接口,而指定为userServiceImpl就报错
    ApplicationContext ctx = new ClassPathXmlApplicationContext("ApplicationContext.xml");
    UserService userService = ctx.getBean("userServiceImpl",UserService.class);
    userService.transfer();
      

  8.   

    我是在 XML 里定义的 Bean,没有给定 id and name,用@Service的方式没注意过。
      

  9.   

    ctx.getBeansOfType(UserServiceImpl.class)
      

  10.   

    我的包名写错了在xml文件中配置的包扫描没起作用就一直报这个错改好后就没有问题了
      

  11.   

    main方法启动不了注解的
      

  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添加注解  
      

  13.   


    @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)接口上面的注解去掉,只需要注解impl就行了。