我正在尝试为简单的Java应用程序创建非xml配置。这是我的文件: 这是我的servlet:
package com.abcd.noxml;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
@WebServlet("/Ctrlr")
public class Ctrlr extends HttpServlet {
@Autowired TeaService teaService;
private static final long serialVersionUID = 1L;
public Ctrlr() {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws {
String brand = request.getParameter("brand");
teaService.teaServe(brand);
}
}
这是我拥有的jsp
<body>
<form action="Ctrlr" method="POST">
<input type="text" name="brand" />
<input type="submit" value="submit" />
</form>
</body>
这是我的配置文件
package com.abcd.noxml;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Autowired
TeaService teaService;
@Bean
public TeaService teaService()
{
return new TeaServiceImpl();
}
}
当我尝试在我的servlet中使用@Autowire创建一个bean时,它返回null。 请建议如何解决这个问题。如果这是一个重复的问题,请将我重定向到现有链接。谢谢