错误提示:NoSuchBeanDefinitionException
提示:NoSuchBeanDefinitionException:找不到指定的bean
可能存在原因:
1.区分动态代理与静态代理以及cglb动态代理的区别
2.可能是项目jdk对相关相关注解的支持不够,@Override报错(我当时就出现了,实现相关的接口的时候,一直提示@Override错误。后来看了一下jdk,默认jdk版本1.5。可能maven工程限制,或者是eclipse中默认配置。这个时候就需要修改了。。。。。一堆配置的修改,这里就不一一说明了。后续文章中将发布相关的修改)
首先看看下面两种代理:
Spring中能够识别的动态代理方式:
1.jdk动态代理:
存在接口时:(那么Spring就能自动识别为jdk的动态代理)
那么使用的时候:
1.需要调用相关的接口,不能直接调用实现类
2.调用的时候:可以自动注入相关的接口,或者从Spring的ioc容器中获取到相关的接口(测试实现)
3.在通过接口获取相关的方法
注意:存在接口的实现类,只需要将实现类注入Spring,接口不需要注入。调用的时候,调用相关的接口,就可实现相关的功能。
☆:后续的事务的管理也需要注意:
若存在接口:
<aop:pointcut expression="execution(* *..*Service.*(..))" id="txPointcut"/>对相关的接口实现事务切入
若不存在接口:
<aop:pointcut expression="execution(* *..*ServiceImp.*(..))" id="txPointcut"/>对相关的实现类实现事务的切入
2.cglb动态代理:
如果不存在相关的接口,那么Spring就能够自动的识别,并调用cglb动态代理去实现。此时调用的时候
1.自动装配相关的实现类
2.从ioc容器中获取实现类(Test测试实现)
3.不存在接口,所以就没接口可以调了
原因:
可能存在的原因1:(这是我存在的原因:当然只是一个参考)
1.因为我实现了相关的接口,然后Spring自动识别的是jdk的动态代理。然后我有又接调用了相关的实现类,所以就一直提示没有注入bean
解决办法:
(阅读上面的jdk动态代理与cglb动态代理)
所以:调用的时候,装配相关的接口,然后再去实现相关的方法
下面的也可以参考一下:
1.修改maven插件的jdk版本
2.修改项目中的jdk版本
3.修改eclipse中jdkl的版本
4.通过接口获取实例,不能通过实现类获取相关的实例
错误代码:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.atguigu.survey.component.service.m.UserServiceImpl] is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:318)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:985)
at com.myproperties.test.userTest.test1(userTest.java:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:69)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:292)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
实例:
修改后的代码:错误代码将会备注提示
service:接口:
接口不需要注入spring
package com.atguigu.survey.component.service.i;
import com.atguigu.survey.entities.entities.User;
public interface UserService {
public Integer regist(User user) throws Exception;
}
serviec实现类:
package com.atguigu.survey.component.service.m;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.atguigu.survey.component.dao.i.UserMapper;
import com.atguigu.survey.component.service.i.UserService;
import com.atguigu.survey.entities.entities.User;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public Integer regist(User user) throws Exception {
int insert = userMapper.insert(user);
// 事务回滚策略
// 正确配置声明式事务,运行期异常:java.lang.RuntimeException,自动回滚;
// int j = 10 / 0 ;
// 文件查找不到异常,是属于编译期异常。框架,对于这种类型的异常,是不自动回滚事务的。
// 如果希望任何异常都需要回滚事务:可以修改事务回滚策略
// <tx:method name="regist*" rollback-for="java.lang.Exception"/>
//FileInputStream fis = new FileInputStream("abc.log");
return insert;
}
}
handler调用:
package com.atguigu.survey.component.handler.guest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.atguigu.survey.component.service.i.UserService;
import com.atguigu.survey.entities.entities.User;
@Controller
@RequestMapping(value="/guest")
public class UserHandler {
@Autowired
private UserService userService;//这里需要调用的相关接口,如果直接调用实现类,那么就会报错
@RequestMapping(value="/user/regiest")
public String userRegiest(User user){
try {
Integer regist = userService.regist(user);
System.out.println(regist);
} catch (Exception e) {
e.printStackTrace();
}
return "/index.jsp";
}
}
Test调用
@Test
public void test2() throws Exception {
ApplicationContext ioc = new ClassPathXmlApplicationContext(
"spring-context.xml");
UserService bean = ioc.getBean(UserService.class);//这里需要调用的相关接口,如果直接调用实现类,那么就会报错
Integer regist = bean.regist(new User(null, "u", "11", 1));
System.out.println(regist);
}