为了账号安全,请及时绑定邮箱和手机立即绑定

问一下spring的问题

问一下spring的问题

漫之旅 2017-09-20 18:25:42
 这是spring的配置<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans                      http://www.springframework.org/schema/beans/spring-beans.xsd                     http://www.springframework.org/schema/context                      http://www.springframework.org/schema/context/spring-context.xsd                     http://www.springframework.org/schema/tx                      http://www.springframework.org/schema/tx/spring-tx.xsd                      http://www.springframework.org/schema/aop                      http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 给FACTORY 提供数据源的配置 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">  <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>  <property name="url" value="jdbc:mysql://localhost:3306/newsystem"></property>  <property name="username" value="root"></property>  <property name="password" value="123456"></property>   </bean>  <!-- 将MYBATIS的SqlSessionFactory 交给SPRING IOC容器管理,factory的生命周期受容器的控制 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  <property name="dataSource" ref="dataSource"></property>  <!-- <property name="configLocation" value="classpath:MyBatis-Configuration.xml"></property>-->   </bean> <!-- 配置dao --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>  <property name="basePackage" value="com.news.mapper"></property> </bean>  <!-- Service --> <bean id="commentsService" class="service.CommentsService"/> <bean id="newsService" class="service.NewsService"/> <bean id="newsUsersService" class="service.NewsUsersService"/> <bean id="topicService" class="service.TopicService"/>  <!-- 该 BeanPostProcessor 将自动起作用,对标注 @Autowired 的 Bean 进行自动注入 -->    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> </beans>我是用注解方式是用spring创建对象这里的两个对象topicService和newsService调试出都是空的,这是为什么?package jsp;import java.io.IOException;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import org.springframework.beans.factory.annotation.Autowired;import com.news.pojo.News;import com.news.pojo.Topic;import service.NewsService;import service.TopicService;import yewu.Execute;public class indexJsp implements Execute{ @Autowired private TopicService topicService; @Autowired private NewsService newsService;   public void setTopicService(TopicService topicService) {  this.topicService = topicService; }  public void setNewsService(NewsService newsService) {  this.newsService = newsService; } @Override public void execute(HttpServletRequest request, HttpServletResponse response)   throws ServletException, IOException {  request.setCharacterEncoding("UTF-8");  HttpSession session=request.getSession();  if(request.getParameter("login")==null){    }else if(request.getParameter("login").equals("0")){   session.removeAttribute("name");  }  List<News> list5;  List<News> list6;    int limit1;  if(request.getParameter("page")!=null){   limit1=Integer.parseInt(request.getParameter("page"));  }else{   limit1=0;  }  int limit2=10;  if(request.getParameter("ntid")!=null){   list5=newsService.jingqueNews(Integer.parseInt(request.getParameter("ntid")),limit1,limit2);   list6=newsService.jingqueNews(Integer.parseInt(request.getParameter("ntid")));  }else{   list5=newsService.jingqueNews(1,limit1,limit2);   list6=newsService.jingqueNews(1);  }  int limitMax=list6.size();  List<News> list1=newsService.guoleiNews();   List<News> list3=newsService.yuleNews();   List<News> list2=newsService.guojiNews();  List<Topic> list=topicService.chaxun();   request.setAttribute("list1", list1);  request.setAttribute("list2", list2);  request.setAttribute("list3", list3);  request.setAttribute("list", list);  request.setAttribute("list5", list5);  request.setAttribute("limit1", limit1);  request.setAttribute("limitMax", limitMax);  request.getRequestDispatcher("index.jsp").forward(request, response);  } }spring的框架日志显示是启动成功的九月 20, 2017 6:15:05 下午 org.apache.catalina.core.ApplicationContext log信息: Initializing Spring root WebApplicationContextlog4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).log4j:WARN Please initialize the log4j system properly.log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
查看完整描述

1 回答

已采纳
?
HansonQ

TA贡献223条经验 获得超56个赞

看下这个对象  indexJsp  是否交由Spring管理

查看完整回答
反对 回复 2017-09-21
  • 漫之旅
    漫之旅
    为什么indexJsp也要交由spring管理啊,这里indexJsp不用交给spring管理也不会影响到他这个类里面的那两个封装属性啊,我这边是已经进入到index这个类了,所以indexJsp应该是不会影响到里面的两个属性吧,对于注解依赖来说是不需要的吧,如果是配置依赖的话才需要吧?
  • HansonQ
    HansonQ
    控制反转。就是将类的创建交给Spring indexJSP如果依赖别的类,它必须要由Spring来负责依赖注入。
  • 漫之旅
    漫之旅
    那这样indexJsp交给spring管理的话,那如果在servlet创建jsp对象,那难道也要把servlet交给spring管理吗?我的indexJsp是在servlet里new出来的
点击展开后面7
  • 1 回答
  • 0 关注
  • 1106 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信