`

(转)CXF中使用Autowired注解无法注入bean的有关问题

 
阅读更多

转自:

http://www.myexception.cn/software-architecture-design/882646.html

 

CXF中使用Autowired注解无法注入bean的问题

之前学习了CXF,通过官方文档,学下来没什么大问题。今天用到实际项目中,在WebService的implementor中用@Autowired注入用@Component,@Resposrity,@Service注解标记的bean,都失败了,得到的都是null。google一番后找到了结果,只要改变一下配置文件写法即可。

 

implementor:

@WebService(endpointInterface = "demo.hw.server.HelloWorld", serviceName = "HelloWorld")
@Component(value="helloWorldImpl")
public class HelloWorldImpl implements HelloWorld {
	
	@Autowired
	private TestBean tBean;
	
    Map<Integer, User> users = new LinkedHashMap<Integer, User>();


    public String sayHi(String text) {
        System.out.println("sayHi called");
        System.out.println(tBean);
        if (tBean == null) {
        	return "Hello " + text;
		}else{
			return "Hello " + tBean.getValue();
		}
        
    }
}

 

 

 

原配置:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

	<context:component-scan base-package="demo" />

	<jaxws:endpoint id="helloWorld" implementor="demo.hw.server.HelloWorldImpl"
		address="/HelloWorld">
	</jaxws:endpoint>

</beans>

 

 

新配置:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

	<context:component-scan base-package="demo" />

	<jaxws:endpoint id="helloWorld" implementor="#helloWorldImpl"
		address="/HelloWorld">
	</jaxws:endpoint>
		
</beans>

 

 

主要区别是原本jaxws中的implementor属性是直接写实现类的fullClass,现在改成ref bean的方式,使用@Component注解标记实现类为一个bean,然后implementor属性用# + bean的名字来reference这个bean即可。如果你使用的配置文件的方式来配置bean,其写法也是大同小异的。

分享到:
评论

相关推荐

    使用CXF开发WebService

    同时,你可以使用Spring的`@Autowired`注解来注入依赖,如`UserDao`,以实现依赖注入。 总结起来,Apache CXF结合Spring提供了一种简洁、高效的方式来创建和消费Web服务。通过注解,我们可以轻松地将Java类和接口...

    cxf和springnvc整合

    首先,为了在 Spring 应用程序中使用 CXF,我们需要在项目的 `pom.xml` 文件中添加相应的 CXF 依赖。这些依赖包括 CXF 的核心组件、前端模块(如 JAX-WS 和 JAX-RS)、数据绑定模块以及传输模块。以下是一些常见的 ...

    CXF-Spring相关jar包

    服务所需的依赖项,如DAO对象、业务层接口等,可以通过@Autowired注解自动装配。 3. **AOP支持**:CXF服务的方法可以被Spring AOP拦截器环绕,实现事务管理、日志记录、性能监控等功能。只需简单地在Spring配置中...

    cxf+spring+client

    这样,我们可以在应用的其他部分通过@Autowired注解注入CXF客户端,方便地调用远程服务。 接下来,我们讨论Spring MVC。Spring MVC是Spring框架的一部分,专为构建Web应用程序设计。它提供了一个模型-视图-控制器...

    spring cxf rest demo

    通过这个项目,开发者可以学习如何使用Spring的`@Component`和`@Autowired`注解进行依赖注入,以及如何通过CXF的`@Path`,`@GET`等注解来定义REST服务。此外,还可以了解到如何在Spring中配置CXF,以及如何使用CXF的...

    CXF与Spring 2.5整合

    我们可以使用Spring的`@ContextConfiguration`和`@Autowired`注解,轻松地注入CXF的服务和客户端,进行服务功能的验证。 在实际项目中,整合CXF和Spring 2.5不仅能够提高开发效率,还能使项目结构更加清晰,降低...

    cxf多数据源配置

    5. ** CXF服务配置**:在CXF服务中,我们可以使用Spring的`@Autowired`注解注入对应的数据源,然后在服务方法中执行SQL查询或操作。也可以使用JdbcTemplate或JPA等工具进行数据操作。 6. **测试与监控**:最后,别...

    cxf与spring整合

    2. **注入服务代理**:在需要调用Web服务的类中,通过@Autowired注解注入服务代理。 3. **调用服务**:通过代理对象调用Web服务的方法。 **四、注意事项** 1. **JAXB绑定**:如果使用JAXB进行XML与Java对象之间的...

    CXF框架入门例子和与Spring整合的例子.zip

    2. **使用Spring注解**:可以通过Spring的@Service、@Autowired等注解,简化服务的配置和bean之间的依赖关系。 3. **启动CXF和Spring**:在Web应用的初始化阶段,通过Spring的DispatcherServlet加载配置,并启动CXF...

    CXF整合spring实现REST接口开发

    在`HelloWorldResource`类中,可以使用`@Autowired`注解注入其他Spring管理的bean。 ```java @Autowired private SomeService someService; @Path("/hello") public class HelloWorldResource { @GET @Produces...

    Spring+cxf配置接口+mybatis配置

    在Spring中,我们可以使用XML或Java配置来定义Bean,例如创建一个DAO接口的实现类,并通过@Autowired注解进行自动装配。此外,Spring还提供了声明式事务管理,只需要在配置中指定事务管理器,就可以在方法级别控制...

    csh框架+cxf+spring+hibernate+mysql 注解 annotation xml json

    在本架构中,注解被用来简化配置,例如在Spring中使用@Autowired自动注入依赖,或者在Hibernate中使用@Entity定义实体类。 6. **XML**:eXtensible Markup Language,是一种标记语言,常用于数据交换和配置文件。在...

    spring整合CXF开发webService接口所需的全部jar包

    - 使用JUnit进行单元测试,Spring的`@ContextConfiguration`和`@Autowired`注解可以帮助加载Spring上下文并注入依赖。 - 对Web服务进行端到端测试,可以使用CXF提供的测试工具,如`TestClient`。 7. **最佳实践**...

    CXF和Spring整合,并且添加拦截器

    这些类可以被Spring管理,通过@Autowired注解进行依赖注入。 ```java public interface MyService { String doSomething(String input); } @Service public class MyServiceImpl implements MyService { @...

    在spring里开发cxf客户端应用

    在Spring框架中开发CXF客户端应用是一个常见的...4. 在需要使用Web服务的地方,通过@Autowired注解注入客户端bean。 通过这种方式,即使没有服务端API,我们也能便捷地构建出与远程Web服务通信的Spring客户端应用。

    CXF结合spring的配置和开发流程2

    使用Spring的`@Service`注解标记服务实现,以便于通过@Autowired进行依赖注入。在CXF配置中,`serviceClass`属性应设置为服务接口的全限定名。 为了在web应用中部署CXF,我们需要在`web.xml`中配置CXF的Servlet: ...

    SSH整合cxf webservice实例

    首先,确保Struts2的配置文件中引入了CXF插件,然后在Action中通过Spring的@Autowired注解来注入CXF服务。 ```java @Autowired private HelloWorldService helloWorldService; public String execute() { ...

    CXF2.7+Spring3 Java WebService 集成用例

    在这个集成案例中,我们利用Spring 3的注解来声明服务的实现类和Bean定义,例如`@WebService`,`@Component`,`@Autowired`等,这些注解减少了XML配置的需求。 5. **创建Java WebService**: 使用CXF的`@...

    maven 搭建springMVC+cxf+mybatis整合项目

    例如,Spring可以管理Bean的生命周期,通过@Autowired注解自动装配依赖,同时通过AOP实现事务管理,确保数据的一致性。 总的来说,这个项目旨在展示如何使用Maven将SpringMVC、CXF和MyBatis集成到一起,形成一个...

    spring-cxf 日志记录配置.zip

    - 使用`@Autowired`注解在需要日志的地方注入Logger。 3. **CXF日志配置**: - CXF通过`cxf.xml`配置文件控制日志行为。你可以配置`&lt;cxf:bus&gt;`元素下的`&lt;cxf:features&gt;`或`&lt;cxf:properties&gt;`标签来控制日志级别和...

Global site tag (gtag.js) - Google Analytics