这是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 WebApplicationContext
log4j: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.
HansonQ
相关分类