spring getbean 问题,求助
刚学spring 手动装配bean会了,换成都是注解却不断有问题
public String addbook(){
Book book=new Book();
book.setId(id);
book.setName(name);
book.setAuthor(author);
ApplicationContext apt=new ClassPathXmlApplicationContext("applicationContext.xml");
basedao basedao=(xzt.servic.basedao) apt.getBean("basedao");
basedao.save(book);
为什么这样可以成功,换成了注解就不行呢?因为上面可以拿到getBean("basedao");,所以basedao应该没问题,应该是拿的时候出现问题?@Autowired?
@Autowired
protected basedao basedao;
public String addbook(){
Book book=new Book();
book.setId(id);
book.setName(name);
book.setAuthor(author);
basedao.save(book);
换成了这样就会报
java.lang.NullPointerException
xzt.action.bookaction.addbook(bookaction.java:94)
basedao:
@Service("basedao")
@Transactional
public class basedaoimpl implements basedao {
@Autowired
protected HibernateTemplate Template;
public HibernateTemplate getTemplate() {
return Template;
}
public void save(Object obj) {
// TODO Auto-generated method stub
getTemplate().save(obj);
}
空值错误,有谁可以给个注解的getben的例子呀
------解决方案--------------------轻量级java EE 企业应用,作者 李刚
@Component("axe")
public class SteelAxe
implements Axe
{
public SteelAxe()
{
System.out.println("Spring实例化依赖Bean:SteelAxe实例...");
}
public String chop()
{
return "钢斧砍柴真快!";
}
@Override
public String toString()
{
return "钢斧头";
}