小弟近来自学spring mvc,网上充斥着各种版本,各有不同,自己照猫画虎,弄的不伦不类。运行后能找到页面,但是不能在控制器里加业务逻辑,因为articleService为null,不知是哪个环节出了问题,我在spring里加了注入,是否还需要在java类中加注解,就是那个articleService和articleDao。
代码如下:
dao层
public class ArticleDao extends HibernateDaoSupport implements IArticleDao {
public List<Article> loadAllArticles() {
return (List<Article>)getHibernateTemplate().loadAll(Article.class);
}
}
service
public class ArticleService {
private IArticleDao articleDao;
public List<Article> loadAllArticles() {
System.out.println("lai l ");
return articleDao.loadAllArticles();
}
public void setArticleDao(IArticleDao articleDao) {
this.articleDao = articleDao;
}
}
controller
@Controller
public class ListArticleController {
private ArticleService articleService;
@RequestMapping(value="/login")
public String test() {
System.out.println(null==articleService);
// List<Article> articles = articleService.loadAllArticles();
// ModelAndView mav = new ModelAndView();
// mav.addObject(articles);
// return mav;
return "listarticle";
}
public void setArticleService(ArticleService articleService) {
this.articleService = articleService;
}
}
applicationContext-dao.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
default-autowire="byName">
<!-- DAO配置于此 -->
<bean id="articleDao" class="fcs.demo.dao.ArticleDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="articleService" class="fcs.demo.service.ArticleService" >
<property name="articleDao" ref="articleDao"></property>
</bean>
<!-- 数据源 -->
<!-- JNDI数据源 -->
<!--
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="${datasource.jndi.name}"/>
</bean>
-->
<!-- JDBC数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${datasource.jdbc.driverClassName}" />
<property name="url" value="${datasource.jdbc.url}" />
<property name="username" value="${datasource.jdbc.username}" />
<property name="password" value="${datasource.jdbc.password}" />
</bean>
<!-- 使用Annotation映射的sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
<prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>fcs.demo.model.Article</value>
<value>fcs.demo.model.Author</value>
</list>
</property>
</bean>
<!-- 事务管理器,此处为Hibernate的事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" />
</beans>
applicationContext.xml如下:
<?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"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:config.properties</value>