@Autowired 使用问题

半支烟z 2010-02-27 10:03:52
@Component("testhost")
public class Testhost {

public void toprint()
{
System.out.println("Testhost of toprint");
}

}
----------------------------
@Component("testss")
public class Testss
{
@Autowired
public Testhost th;
public void toth()
{

th.toprint();
}
}

--------------------------------
public class Main01 {

public static void main(String[] args) {
Testss tss=new Testss();
tss.toth();
}

}

、、、、、、、、、、、、、错误
Exception in thread "main" java.lang.NullPointerException
at aop.test.Testss.toth(Testss.java:14)
at aop.test.Main01.main(Main01.java:10)





应该怎么用呢?
...全文
114 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
mamaipi 2010-03-01
  • 打赏
  • 举报
回复
引用 2 楼 yb1682003 的回复:
在spring的环境当中是需要spring的方式加载才可以调用的。

应该使用ApplicationContext context=new ......
再通过context.getBean()

等去找到bean才可以操作你的方法


楼哂上的说的对. 需要手动加载spring的配置文件拿到bean后才可以调用..
半支烟z 2010-02-28
  • 打赏
  • 举报
回复
当写到jsp文件里的时候就没有问题,那好像是配置文件没有起到作用
那怎么导入到配置文件呢?
yb1682003 2010-02-28
  • 打赏
  • 举报
回复
在spring的环境当中是需要spring的方式加载才可以调用的。

应该使用ApplicationContext context=new ......
再通过context.getBean()

等去找到bean才可以操作你的方法

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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