涉及到的类包括:
1. ITestService.java
2. TestServiceImp.java
3. autoWiringService.java
以及Spring配置文件:
4.autoWiring.xml
对应的源码为:
1. ITestService.java
package com.z.test.annotation;
/**
* Created by Z on 2016/11/16.
*/
public interface ITestService {
public void annoTest();
}
2.TestServiceImp.java
package com.z.test.annotation;
import org.springframework.stereotype.Service;
/**
* Created by Z on 2016/11/16.
* 如何引入注解
*/
@Service("testServiceImp")
public class TestServiceImp implements ITestService {
public void annoTest(){
System.out.println("注解调用");
}
}
3.autoWiringService.java
package com.z.test.annotation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.annotation.Resource;
/**
* Created by Z on 2016/11/16.
*/
public class AutoWiringService {
@Resource(name ="testServiceImp")
public ITestService iTestService;
public void autoTest(){
iTestService.annoTest();
}
public static void main(String[] args){
ApplicationContext context =new ClassPathXmlApplicationContext("autowiring.xml");
AutoWiringService autoW=(AutoWiringService)context.getBean("autoWiringService");
autoW.autoTest();
}
}
使用了@Service 和 @Resource注解。目录结构:
在AutoWiringService运行主方法进行测试时,总是有错误如下:
猜测是否是无法读取xml文件?还是哪里没有配置好?