消息关闭
    暂无新消息!

java 框架搭建问题

问题作者 : 设计师t2017-08-28发布
No bean named 'deptService' is defined
Error creating bean with name 'deptAction' defined in URL [file:/D:/Users/Administrator/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp2/wtpwebapps/T_web/WEB-INF/classes/applicationContext-action.xml]: Cannot resolve reference to bean 'deptService' while setting bean property 'deptService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'deptService' is defined
Unable to instantiate Action, deptAction, defined for 'deptAction_list' in namespace '/sysadmin'Error creating bean with name 'deptAction' defined in URL [file:/D:/Users/Administrator/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp2/wtpwebapps/T_web/WEB-INF/classes/applicationContext-action.xml]: Cannot resolve reference to bean 'deptService' while setting bean property 'deptService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'deptService' is defined
File: org/springframework/beans/factory/support/DefaultListableBeanFactory.java
Line number: 698

是AOP自动注入的 
配置文件
applicationContext-action.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans    
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
http://www.springframework.org/schema/aop    
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd    
http://www.springframework.org/schema/tx    
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd    
http://www.springframework.org/schema/context    
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!-- 1.dataSource -->
<!-- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 
<property name="driverClass" value="com.mysql.jdbc.Driver" /> <property name="jdbcUrl" 
value="jdbc:mysql://127.0.0.1:3306/jk1201?characterEncoding=utf8" /> <property 
name="user" value="root" /> <property name="password" value="root" /> </bean> -->

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl"
value="jdbc:mysql://123.207.xxxxx:3306/shangchengtest" />
<property name="user" value="shangchengtest" />
<property name="password" value="pNC3AkC3X5Vqe0O" />
</bean>
<!-- 2.sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>



<!--Shiro安全框架产生代理子类的方式: 使用cglib方式 -->

<!-- 3.事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 4.txAdvice -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />

<tx:method name="get*" read-only="true" />
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>

<!-- 5.aop -->
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* com.Teng.Service.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" />
</aop:config>

<!--组装其它 配置文件 -->
<import resource="classpath*:applicationContext-action.xml"></import>
<import resource="classpath*:applicationContext-dao.xml"></import>
<import resource="classpath*:applicationContext-service.xml"></import>


</beans>


applicationContext-action.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans    
http://www.springframework.org/schema/beans/spring-beans.xsd    
http://www.springframework.org/schema/aop    
http://www.springframework.org/schema/aop/spring-aop.xsd    
http://www.springframework.org/schema/tx    
http://www.springframework.org/schema/tx/spring-tx.xsd    
http://www.springframework.org/schema/context    
http://www.springframework.org/schema/context/spring-context.xsd">

<bean id="loginAction" class="com.Teng.action.LoginAction" scope="prototype"></bean>
<bean id="homeAction" class="com.Teng.action.HomeAction" scope="prototype"></bean>

<bean id="deptAction" class="com.Teng.action.sysadmin.DeptAction"
scope="prototype">
<property name="deptService" ref="deptService"></property>
</bean>

</beans>


applicationContext-service.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:p="http://www.springframework.org/schema/p"  
xmlns:context="http://www.springframework.org/schema/context"   
xmlns:tx="http://www.springframework.org/schema/tx"  
xmlns:aop="http://www.springframework.org/schema/aop"  
xsi:schemaLocation="http://www.springframework.org/schema/beans    
http://www.springframework.org/schema/beans/spring-beans.xsd    
http://www.springframework.org/schema/aop    
http://www.springframework.org/schema/aop/spring-aop.xsd    
http://www.springframework.org/schema/tx    
http://www.springframework.org/schema/tx/spring-tx.xsd    
http://www.springframework.org/schema/context    
http://www.springframework.org/schema/context/spring-context.xsd">

<bean id="deptService" class="com.Teng.Service.Impl.DeptServiceImpl">
<property name="baseDao" ref="baseDao"></property>
</bean>

</beans>

......................

package com.Teng.action.sysadmin;

import com.Teng.Service.DeptService;
import com.Teng.action.BaseAction;
import com.Teng.domain.Dept;
import com.opensymphony.xwork2.ModelDriven;

import cn.TengHuang.utils.Page;

public class DeptAction extends BaseAction implements ModelDriven<Dept> {

/**
 * 
 */
private static final long serialVersionUID = 1L;
private Dept dept = new Dept();
public Dept getModel() {
// TODO Auto-generated method stub
return dept;
}

private Page page = new Page(); 
public Page getPage() {
return page;

public void setPage(Page page) {
this.page = page;
}

// 注入DeptService
private DeptService deptService; 
public void setDeptService(DeptService deptService) {
this.deptService = deptService;
}

public String list() throws Exception {
deptService.findDeptByPage("form Detp", page, null);
page.setUrl("deptAction_list");
return "list";
}

}






1个回答

︿ 2
<import resource="classpath*:applicationContext-action.xml"></import>
    <import resource="classpath*:applicationContext-dao.xml"></import>
    <import resource="classpath*:applicationContext-service.xml"></import>
换成

    <import resource="classpath*:applicationContext-dao.xml"></import>
    <import resource="classpath*:applicationContext-service.xml"></import>
<import resource="classpath*:applicationContext-action.xml"></import>
试试。