AutoWired与Qualifier
@AutoWired:Spring会自动创建实现类对象,并且将实现类对象赋值给dao,如果项目中该接口只有一个实现类,可以使用AutoWired;但是,接口有两个或者两个以上的实现类,此时就会抛异常
@Qualifier:可以手动指定将哪一个实现类的对象赋值给IPersonDao接口
两个必须合在一起使用
@Repository
public class PersonDaoImpl implements IPersonDao {
public boolean findByUserNameAndPassword ( Person person) {
return true ;
}
}
@Repository
public class StudentDaoImpl implements IPersonDao {
public boolean findByUserNameAndPassword ( Person person) {
return true ;
}
}
@Service
public class PersonServiceImpl implements IPersonService {
@Autowired
@Qualifier ( "personDaoImpl" )
private IPersonDao dao;
Spring和web结合
< dependency>
< groupId> jstl</ groupId>
< artifactId> jstl</ artifactId>
< version> 1.2</ version>
< scope> provided</ scope>
</ dependency>
< dependency>
< groupId> javax.servlet</ groupId>
< artifactId> javax.servlet-api</ artifactId>
< version> 3.1.0</ version>
< scope> provided</ scope>
</ dependency>
< dependency>
< groupId> javax.servlet.jsp</ groupId>
< artifactId> javax.servlet.jsp-api</ artifactId>
< version> 2.2.1</ version>
< scope> provided</ scope>
</ dependency>
@WebServlet ( "/login" )
public class LoginServlet extends HttpServlet {
private static final Logger logger = LoggerFactory. getLogger ( LoginServlet. class ) ;
protected void doPost ( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet ( request, response) ;
}
protected void doGet ( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username= request. getParameter ( "username" ) ;
String password= request. getParameter ( "password" ) ;
logger. debug ( username) ;
logger. debug ( password) ;
ClassPathXmlApplicationContext context= new ClassPathXmlApplicationContext ( "applicationContext.xml" ) ;
IPersonService personService = ( IPersonService) context. getBean ( "personServiceImpl" ) ;
Person person = new Person ( username, password) ;
boolean flag = personService. login ( person) ;
logger. debug ( flag+ " doGet " ) ;
response. getWriter ( ) . println ( flag? "success" : "error" ) ;
}
}
代码优化一
PersonServiceImpl service = ( PersonServiceImpl) context. getBean ( "PersonServiceImppl" ) ;
代码优化二
ServletContext有什么用 支持多个Servlet共享数据且整个项目只有一个实例
ContextLoaderListener介绍
ContextLoaderListener是什么 Spring编写了一个监听器,该监听器监听ServletContext对象的创建
作用: 会自动读取contextConfigLocation配置,并创建IOC容器,将IOC容器保存在ServletContext中
如何使用 依赖spring-web 在web.xml中配置监听器
pom.xml
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-web</ artifactId>
< version> 5.2.9.RELEASE</ version>
</ dependency>
web.xml
< context-param>
< param-name> contextConfigLocation</ param-name>
< param-value> classpath:applicationContext.xml</ param-value>
</ context-param>
< listener>
< listener-class> org.springframework.web.context.ContextLoaderListener</ listener-class>
</ listener>
LoginServlet
WebApplicationContext context = ( WebApplicationContext) getServletContext ( ) . getAttribute ( WebApplicationContext. ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) ;