有没有大神帮忙看一看(Spring的xml中无法加载bean因为找不到类)

远来客 远来客 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
applicationContext.xml代码:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
<!-- 读取db.properties文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 配置c3p0连接池 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
<property name="jdbcUrl" value="${jdbc.jdbcUrl}" ></property>
<property name="driverClass" value="${jdbc.driverClass}" ></property>
<property name="user" value="${jdbc.user}" ></property>
<property name="password" value="${jdbc.password}" ></property>
</bean>

<!-- 将SessionFactory配置到spring容器中 -->
<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 将连接池注入到sessionFactory, hibernate会通过连接池获得连接 -->
<property name="dataSource" ref="dataSource" ></property>
<!-- 配置hibernate基本信息 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</prop>

<!-- 可选配置 -->
<prop key="hibernate.show_sql" >true</prop>
<prop key="hibernate.format_sql" >true</prop>
<prop key="hibernate.hbm2ddl.auto" >update</prop>
</props>
</property>

<!-- 引入orm元数据,指定orm元数据所在的包路径,spring会自动读取包中的所有配置 -->
<property name="packagesToScan" value="classpath:com.yang"></property>
</bean>

<!-- 配置Dao -->

<bean name="customerDao" class="com.yang.Dao.Impl.CustomerDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<!-- 配置Service -->

<bean name="customerService" class="com.yang.Service.Impl.CustomerServiceImpl">
<property name="cd" ref="customerDao"></property>
</bean>

<!-- 配置Action -->

<bean name="customerAction" class="com.yang.web.action.CustomerActoin" scope="prototype">
<property name="cs" ref="customerService"></property>
</bean>

</beans>


Struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
<!-- struts.objectFactory = spring 将action的创建交给spring容器
struts.objectFactory.spring.autoWire = name spring负责装配Action依赖属性 -->
<constant name="struts.objectFactory" value="spring"></constant>

<!-- 配置Action -->

<package name="crm" extends="struts-default" >

<global-exception-mappings>
<exception-mapping result="error" exception="java.lang.RuntimeException"></exception-mapping>
</global-exception-mappings>

<action name="CustomerAction_*" class="customerAction" method="{1}">
<result name="list" >/jsp/customer/list.jsp</result>
</action>

</package>

</struts>


DaoImpl代码


package com.yang.Dao.Impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

import com.yang.Dao.CustomerDao;
import com.yang.entity.Customer;

public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {

@Override
public List<Customer> getPageList(DetachedCriteria dc, int start, Integer pageSize) {

List<Customer> list = (List<Customer>) getHibernateTemplate().findByCriteria(dc, start, pageSize);

return list;
}

@Override
public Integer getTotalCount(DetachedCriteria dc) {
//设置查询的聚合函数,总记录数
dc.setProjection(Projections.rowCount());

List<Long> list = (List<Long>) getHibernateTemplate().findByCriteria(dc);

//清空之前设置的聚合函数
dc.setProjection(null);

if(list!=null&&list.size()>0){
Long count = list.get(0);
return count.intValue();
}else{
return null;
}

}

}


SeriviceImpl代码

package com.yang.Service.Impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.yang.Dao.CustomerDao;
import com.yang.Service.CustomerService;
import com.yang.entity.Customer;
import com.yang.utils.PageBean;


public class CustomerServiceImpl implements CustomerService {

private CustomerDao cd;

@Override
public PageBean getPageBean(DetachedCriteria dc, Integer currentPage, Integer pageSize) {
//1、调用Dao查询总记录数
Integer totalCount = cd.getTotalCount(dc);
//2、创建PageBean对象,计算总页数
PageBean pb = new PageBean(currentPage,totalCount,pageSize);
//3、调用Dao查询分页列表数据
List<Customer> list = cd.getPageList(dc, pb.getStart(), pb.getPageSize());
//4、列表数据放入pageBean中,并返回
pb.setList(list);
return pb;
}


public void setcd(CustomerDao cd) {
this.cd = cd;
}


}



Action代码

package com.yang.web.action;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.yang.Service.CustomerService;
import com.yang.entity.Customer;
import com.yang.utils.PageBean;

public class CustomerActoin extends ActionSupport implements ModelDriven<Customer> {

private Customer customer = new Customer();
private CustomerService cs;
private Integer currenPage;
private Integer pageSize;

public String list() throws Exception {
//封装离线查询对象
DetachedCriteria dc = DetachedCriteria.forClass(Customer.class);
//判断并封装参数
if(StringUtils.isNoneBlank(customer.getCust_name())){
dc.add(Restrictions.like("cust_name", "%"+customer.getCust_name()+"%"));
}
//1、调用Service查询分页数据(PageBean)
PageBean pb = cs.getPageBean(dc, currenPage, pageSize);
//2、将PageBean放入request域,转发到列表页面显示
ActionContext.getContext().put("pageBean", pb);
return "list";
}


@Override
public Customer getModel() {
return null;
}


public void setcs(CustomerService cs) {
this.cs = cs;
}

}
...全文

相关推荐

评论 16
收藏 已收藏
分享
返回顶部
远来客
Java EE 67,535 成员 225,843 内容