1).在spring框架中运行动态代理,出现:NoSuchBeanDefinitionException:no bean named 'XXXX' available问题。
原因是:
1.下面代码中getBean函数中的类名首字母没有小写,因为类交给spring管理的时候一般默认使用小写的类名作为bean的id;
2、如果类名全部是大写字母,Spring会直接使用类名作为id,所以类名必须规范使用驼峰结构,这样有利于在代码中统一处理;
3、Component注解后使用value指定名称,这样也会影响bean的获取。例如:@Component(value="CalureImpl");
4、类名命名不规范,使用$等特殊字符;
5、直接使用类接口的名称获取实现类的bean,并且接口没有使用Component注解;
6、项目中存在重名的类也会影响bean的获取。
public class Client {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("otherBean.xml");
StudentDao studentDao = context.getBean("studentDaoIml",StudentDao.class); //StudentDao.class必须加啊
studentDao.delete(1);
}
}