Spring注解疑问,谢谢,@Autowired 注释private List as;就自动绑定到a所有的实现类到List没有,谢谢

lyhang801 2014-03-12 04:33:36
下面代码无法依赖注入实际的类,请各位大神能否帮忙看看,谢谢,测试代码如下:
public interface HelloBeanInf {
public void test();
}
然后两个实现类
@Service
public class HelloBean implements HelloBeanInf{
private String helloWord = "ssss";

public void test(){
System.out.println(helloWord);
}
}


@Service
public class HelloBean1 implements HelloBeanInf{
private String helloWord = "aaaaa";

public void test(){
System.out.println(helloWord);
}
}
配置文件
bean.xml
<context:annotation-config/>

主类进行调用的
public class SpringTest {

private HelloBeanInf helloBean;

@Autowired
private HelloBeanInf[] helloBeanInfs;//List<HelloBeanInf> helloBeanInfs;

public HelloBeanInf[] getHelloBeanInfs() {
return helloBeanInfs;
}


public void setHelloBeanInfs(HelloBeanInf[] helloBeanInfs) {
this.helloBeanInfs = helloBeanInfs;
}

public static void main(String[] args) {
// TODO Auto-generated method stub
org.springframework.core.io.Resource res = new ClassPathResource("com/cec/bean.xml");
BeanFactory factory = new XmlBeanFactory(res);
SpringTest springTest = new SpringTest();
for (HelloBeanInf t : springTest.getHelloBeanInfs()) {
((HelloBeanInf) t).test();
}

}

报null,依赖没有注入,请问是什么原因没有注入呢,谢谢
...全文
330 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
benluobo 2014-03-12
  • 打赏
  • 举报
回复
解决办法: 第一步 @service("springTest") public class SpringTest { 第二步: ApplicationContext res = new FileSystemXmlApplicationContext( "com/cec/bean.xml"); Test springTest = (Test) res.getBean("springTest") for (HelloBeanInf t : springTest.getHelloBeanInfs()) { ((HelloBeanInf) t).test(); }
benluobo 2014-03-12
  • 打赏
  • 举报
回复
org.springframework.core.io.Resource res = new ClassPathResource("com/cec/bean.xml"); BeanFactory factory = new XmlBeanFactory(res); SpringTest springTest = new SpringTest(); 这样new出来有什么用?

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧