Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
694 views
in Technique[技术] by (71.8m points)

dependency injection - JBoss 6 + Spring 3.0.5 + JAX-WS/CXF

We've got our project running on JBoss 6 with Spring 3.0.5. Everything went smooth until we tried to implement some Web Services with JAX-WS. If I try doing some simple WS (like adding 2 numbers), it just works - I put annotations and add annotated class as a servlet. But things are getting harder if I try to have my JAX-WS classes populated with dependencies.

Here's my code:

@WebService(name = "principal")
public class PrincipalWebService extends SpringBeanAutowiringSupport {

    @Autowired
    private PrincipalManager manager;

    @WebMethod
    public int add(int a, int b) {
        return a + b;
    }

    @WebMethod
    public Principal getById(int i) {
            return manager.getById(i);
    }

}

Add method works, but getById fails with NPE. I've been debugging SpringBeanAutowiringSupport and it looks like ContextLoader.getCurrentWebApplicationContext() returns null. It means SpringBeanAutowiringSupport constructor is being called before context is initialized.

I've been trying following CXF instructions on running app with Spring. I don't have this code right now, but I've registered PrincipalWebService as a bean, created spring file to configure CXF and added this bean via it's ID as an endpoint. It went good on Jetty, but failed on JBoss. I've been receiving different kinds of exceptions depending on how I configure CXF, but root cause was the same - JBoss 6 CXF version is compiled against Spring 2.5, so we had library inconsistency.

Does anyone has any ideas of getting IoC working for Jax-ws services on JBoss 6?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ok, I've found a workaround. All we need to do is to move dependency injection to @PostConstruct method:

@PostConstruct
public void init() {
    SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...