Spring3依赖注入不与骡一起使用 [英] Spring3 Dependency Injection not working with mule
本文介绍了Spring3依赖注入不与骡一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的mule_flow.mflow文件
<?xml version =1.0encoding =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/corexmlns:doc =http://www.mulesoft.org/schema/mule/documentation xmlns:spring =http://www.springframework.org/schema/beansversion =CE-3.3.0xmlns: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 =helloServicedoc:name =helloService>
address =http:// localhost:8081 / hellodoc: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 =addCustomerdoc:name =addCustomer>
address =http:// localhost:8081 / pos / addCustomerdoc: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.0encoding =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-co ntext.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 =datasourceclass =org.springframework.jdbc.datasource.DriverManagerDataSource>
< property name =driverClassNamevalue =oracle.jdbc.OracleDriver/>
< property name =urlvalue =jdbc:oracle:thin:@localhost:1521:xe/>
< property name =usernamevalue =test/>
< property name =passwordvalue =test/>
< / bean>
< bean id =sessionFactory
class =org.springframework.orm.hibernate3.LocalSessionFactoryBean>
< property name =dataSourceref =datasource/>
< property name =configLocation>
< value> classpath:hibernate.cfg.xml< / value>
< / property>
< property name =configurationClass>
< value> org.hibernate.cfg.AnnotationConfiguration< / value>
< / property>
< property name =hibernateProperties>
<道具>
< 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>
< /道具>
< / property>
< / bean>
< tx:annotation-driven />
< bean id =transactionManager
class =org.springframework.orm.hibernate3.HibernateTransactionManager>
< property name =sessionFactoryref =sessionFactory/>
< / bean>
< bean id =customerDaoclass =com.proj.pos.dao.implementation.CustomerDaoImpl>
< property name =sessionFactory>
< ref local =sessionFactory/>
< / property>
< / bean>
< bean id =customerServicescope =prototypeclass =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实现CustomerService {
public CustomerDao getCustomerDao(){
return customerDao;
}
public void setCustomerDao(CustomerDao customerDao){
this.customerDao = customerDao;
}
@Autowired
私人CustomerDao customerDao; //没有得到populated.tried删除自动装配。
@Transactional
@WebMethod(operationName =addCustomer)
@WebResult(name =addCustomerResult)
@Override
public Customer addCustomer(@WebParam(name =customerId)long customerId,@ WebParam(name =fname)字符串fname,@ WebParam(name =lname)字符串lname,@ WebParam(name =age)long age,@ WebParam(name = dateOfBirth)字符串dateOfBirth,@ WebParam(name =address)字符串地址){
Customer customer = new Customer(customerId,fname,lname,age,dateOfBirth,address);
customer.setCustomerId(customerDao.persist(customer)); //在此处抛出NUllPointerException,因为customerDao为null ..
return customer;
}
}
为什么DI不工作?
解决方案
您可能会错过几件事:
- 您应扫描想要弹出的包以查看注释类。在你的情况下,你应该添加下面的XML片段到你的mule配置
< context:component-scan base-package =com.proj.pos.webservice/>
- 您应该将组件声明为弹簧对象而不是组件,因此您应该替换
< component class =com.proj.pos.webservice。 implementation.CustomerServiceImpl
with
doc:name =Java/>< component>< spring-object bean =customerService/><<< ; / component>
I am unable to inject customerDao object in reference variable inside CustomerServiceImpl service class.
Here is my mule_flow.mflow file
<?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>
Here is my Spring-mule.xml file
<?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>
Here is my 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;
}
}
Any idea why DI not working?
解决方案
You are probably missing a couple of things:
- You should scan the packages where you want spring to look fo annotated classes. In your case you should add the following xml snippet to your mule config
<context:component-scan base-package="com.proj.pos.webservice" />
- You should declare your component as a spring object and not as a component, so you should replace
<component class="com.proj.pos.webservice.implementation.CustomerServiceImpl" doc:name="Java" />
with<component><spring-object bean="customerService"/></component>
这篇关于Spring3依赖注入不与骡一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文