81,090
社区成员
发帖
与我相关
我的任务
分享
@Service
public interface UserService {
public void transfer();
}
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
@Transactional(isolation=Isolation.READ_COMMITTED,propagation=Propagation.REQUIRED)
public void transfer() {
userDao.transfer();
}
}
<!--隐式地向 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)
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就行了。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添加注解 ApplicationContext ctx = new ClassPathXmlApplicationContext("ApplicationContext.xml");
UserService userService = ctx.getBean("userServiceImpl",UserService.class);
userService.transfer();
ctx.getBeansOfType(UserServiceImpl.class)
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)
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)
UserService userService = ctx.getBean("com.jetsen.user.UserServiceImpl", UserServiceImpl.class);