不知道做了什么,@Autowired注解都无效了,求大神指教

拼搏成会抓羊的灰太狼 2016-05-25 07:17:30
以下是测试项目中的代码:
@Component
public class vbnm extends HttpServlet
{
public vbnm()
{
System.out.println("注入");
}

@Autowired
p person;

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
System.out.println("------------------------------");
person.say();
System.out.println("------------------------------");
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{

}
}

接口:public interface p
{
public void say();
}

实现类:@Component
public class Person implements p
{

private String name;

public Person()
{
System.out.println("注入ioc容器");
}

/**
* @return the name
*/
public String getName()
{
return name;
}

/**
* @param name
* the name to set
*/
public void setName(String name)
{
this.name = name;
}

public void say()
{
System.out.println("hello " + name);
}

}
spring配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">

<context:component-scan base-package="de.sheng"></context:component-scan>

<context:annotation-config />


</beans>
...全文
347 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Kai Havterz 2020-03-06
  • 打赏
  • 举报
回复
你是原生态的servlet容器和springioc容器吗?需要在servlet 初始化init()方法将两个容器关联起来: public void init() { //加载spring ioc 容器,获取该容器的所有bean WebApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(this.getServletContext()); studentService=(IStudentService) context.getBean("studentServiceImpl"); }
  • 打赏
  • 举报
回复
不是扫描路径的问题,文件可以正常扫描到,构造函数中设置一个输出 也会正常输出
  • 打赏
  • 举报
回复
都无效了是什么鬼?注入不进去了?是不是扫描的路径改错了

67,513

社区成员

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

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