问答详情
源自:4-2 Spring Bean装配之Autowired注解说明-1

No bean named 'injectionServiceImpl' available

InjectionServiceImp:

@Service

public class InjectionServiceImpl implements InjectionService {

@Autowired

private InjectionDAO injectionDAO;

public void save(String arg) {

System.out.println("service接收参数:"+arg);

arg=arg+""+this.hashCode();

injectionDAO.save(arg);

}

InjectionDAOImpl:

@Repository

public class InjtctionDAOImpl implements InjectionDAO {


public void save(String arg) {

System.out.println("保存数据"+arg);

}

}

Test:

public class Test {


public static void main(String[] args) {

ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("spring-inject.xml");

InjectionService service=(InjectionService)ctx.getBean("injectionServiceImpl");

service.save("这是要保存的数据");


}

这样还是会找不到bean怎么回事?

提问者:慕瓜5041502 2017-06-07 22:11

个回答

  • 别跑
    2017-06-12 10:45:38

    问题不在这里,问题在于spring-inject.xml中 component-scan的 base-package路径和@Service所在路径不一致

  • 慕瓜5041502
    2017-06-08 19:52:37

    不是找不到xml,如果找不到就会提示FileNoFoundException了

  • qq_面对疾风吧_04374065
    2017-06-08 14:25:05

    是不是xml文件的位置 试试classpath:spring-inject.xml