当前位置: 首页 > 面试题库 >

Spring3依赖注入不适用于m子

邵博艺
2023-03-14
问题内容

我无法在CustomerServiceImpl服务类内的引用变量中注入customerDao对象。

这是我的mule_flow.mflow文件

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:http="http://www.mulesoft.org/schema/mule/http"
    xmlns:cxf="http://www.mulesoft.org/schema/mule/cxf"
    xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" version="CE-3.3.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd 
http://www.mulesoft.org/schema/mule/cxf http://www.mulesoft.org/schema/mule/cxf/current/mule-cxf.xsd 
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd 
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd ">


<flow name="helloService" doc:name="helloService">
<http:inbound-endpoint exchange-pattern="request-response"
address="http://localhost:8081/hello" doc:name="HTTP" />
<cxf:simple-service serviceClass="com.proj.pos.demo.HelloWorld"
doc:name="SOAP" />
<component class="com.proj.pos.demo.ServiceAImplService"
doc:name="Java" />
</flow>

<flow name="addCustomer" doc:name="addCustomer">
<http:inbound-endpoint exchange-pattern="request-response"
address="http://localhost:8081/pos/addCustomer" doc:name="HTTP" />
<cxf:simple-service serviceClass="com.proj.pos.webservice.interfac.CustomerService"
doc:name="SOAP" />
<component class="com.proj.pos.webservice.implementation.CustomerServiceImpl"
doc:name="Java" />
</flow>

<spring:beans>
 <spring:import resource="classpath:spring-mule.xml"/>
</spring:beans>
</mule>

这是我的Spring-mule.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:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd" >


        <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />
<property name="username" value="test" />
<property name="password" value="test" />
</bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="datasource" />
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
        <property name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
                <prop key="hibernate.connection.release_mode">auto</prop>
            </props>
        </property>
    </bean>

    <tx:annotation-driven />
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

     <bean id="customerDao" class="com.proj.pos.dao.implementation.CustomerDaoImpl">
    <property name="sessionFactory">
    <ref local="sessionFactory"/>
    </property>
    </bean>

    <bean id="customerService" scope="prototype" class="com.proj.pos.webservice.implementation.CustomerServiceImpl">
    <property name="customerDao" >
    <ref local="customerDao"/>
    </property>
    </bean>
 </beans>

这是我的CustomerServiceImpl.java

package com.proj.pos.webservice.implementation;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.proj.pos.dao.interfac.CustomerDao;
import com.proj.pos.entity.Customer;
import com.proj.pos.webservice.interfac.CustomerService;
@WebService(endpointInterface = "com.proj.pos.webservice.interfac.CustomerService",
        serviceName = "CustomerService")
public class CustomerServiceImpl implements CustomerService {

    public CustomerDao getCustomerDao() {
        return customerDao;
    }
    public void setCustomerDao(CustomerDao customerDao) {
        this.customerDao = customerDao;
    }
    @Autowired
    private CustomerDao customerDao;//not getting populated.tried removing autowired as well.
    @Transactional
    @WebMethod(operationName="addCustomer")
    @WebResult(name="addCustomerResult")  
    @Override
    public Customer addCustomer(@WebParam(name="customerId")long customerId,@WebParam(name="fname")String fname,@WebParam(name="lname")String lname,@WebParam(name="age")long age,@WebParam(name="dateOfBirth")String dateOfBirth,@WebParam(name="address")String address) {
        Customer customer=new Customer(customerId, fname, lname, age, dateOfBirth, address);
        customer.setCustomerId(customerDao.persist(customer));//throws NUllPointerException here as customerDao is null..
         return customer;
    }

}

知道为什么DI不起作用吗?


问题答案:

您可能缺少了几件事:

  1. 您应该在要让Spring看起来带有注释的类的位置扫描软件包。在您的情况下,应将以下xml代码段添加到您的Mule配置中<context:component-scan base-package="com.proj.pos.webservice" />
  2. 您应该将组件声明为spring对象而不是组件,因此应替换<component class="com.proj.pos.webservice.implementation.CustomerServiceImpl" doc:name="Java" /><component><spring-object bean="customerService"/></component>


 类似资料:
  • 两个无状态EJB及其远程接口。EJB1被注入EJB2 > EJB2还使用一些可选包(在其清单中声明) WebLogic应用服务器(10.3.3) 两个EJB被打包成两个单独的JAR文件 如果将两个JAR文件打包到一个EAR文件中并部署,则依赖注入工作。但是如果我单独部署它们,即使我首先部署了EJB1并在Weblogic(com.xxx.EJB1#com.xxx.layer1中验证了全局JNDI名称

  • 我的应用程序正在部署到IBM WebSphere上。我有一个简单的服务,我想知道依赖注入在这种情况下是如何工作的。 它失败,出现以下错误: [错误]CWWKZ0002E:启动应用程序my app时发生异常。例外消息是:com。国际商用机器公司ws。容器服务状态StateChangeException:com。国际商用机器公司ws。cdi。CDIException:com。国际商用机器公司wsspi

  • 在React中,想做依赖注入(Dependency Injection)其实相当简单。请看下面这个例子: // Title.jsx export default function Title(props) { return <h1>{ props.title }</h1>; } // Header.jsx import Title from './Title.jsx'; export defa

  • 依赖注入 Dependency Injection is a strong mechanism, which helps us easily manage dependencies of our classes. It is very popular pattern in strongly typed languages like C# and Java. 依赖注入是一个很强大的机制,该机制可以帮

  • 简介 Hyperf 默认采用 hyperf/di 作为框架的依赖注入管理容器,尽管从设计上我们允许您更换其它的依赖注入管理容器,但我们强烈不建议您更换该组件。 hyperf/di 是一个强大的用于管理类的依赖关系并完成自动注入的组件,与传统依赖注入容器的区别在于更符合长生命周期的应用使用、提供了 注解及注解注入 的支持、提供了无比强大的 AOP 面向切面编程 能力,这些能力及易用性作为 Hyper

  • 出自维基百科 Wikipedia: 依赖注入是一种允许我们从硬编码的依赖中解耦出来,从而在运行时或者编译时能够修改的软件设计模式。 这句解释让依赖注入的概念听起来比它实际要复杂很多。依赖注入通过构造注入,函数调用或者属性的设置来提供组件的依赖关系。就是这么简单。