Spring Application Context Load Order - java

On my web.xml I have a "springmvc" servlet declaration (which has a corresponding springmvc-servlet.xml)
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/myapp/*</url-pattern>
</servlet-mapping>
I also have my usual applicationContext.xml file.
Which one gets loaded first? The springmvc-servlet.xml or the applicationContext.xml?
The reason I'm asking this is whenever I place the <mvc:annotation-driven/> element in the applicationContext.xml, I get a Severe Context error. But when I put that element in the springmvc-servlet.xml, my web app runs fine.
Any ideas why?
On another web-app, I have the <mvc:annotation-driven/> inside the applicationContext.xml and it runs fine.
Addendum:
I do notice that the presence of aop:config poses conflict against mvc:annotation-driven

the applicationContext.xml context is parent to the dispatcher-servlet.xml context. I don't know whether this means it is loaded first, but it does not matter in your case:
<mvc:annotation-driven /> must be in the dispatcher-servlet.xml, because it belongs to the web-part of the application.

I solved my problem!
It turns out it has nothing to do with the load order or where the <mvc:annotation-driven/> is declared.
I tried deploying my web-app on another Tomcat and to my surprise there's a stack trace in the localhost log. I had a hint by trial and error that the conflict is with <aop:config/>. But what particular conflict?
Then I saw this error in the log file:
java.lang.ClassCastException: org.aspectj.weaver.ResolvedType$Array cannot be cast to org.aspectj.weaver.ReferenceType
So we have a cast exception. I googled that exact error above and found this: Spring 3: adding causes ClassCastException
It appears the thread starter and I have the same exact issue. So I downloaded the aspectj-1.6.10.jar but I was still missing a class. Then it turns out it should be the aspectjweaver-1.6.9
I was still using a very old aspectjweaver. It didn't have any version on its name. Problem solved. Case closed.
By the way as a bonus, I've manually unrolled the <mvc:annotation-driven/> element to its equivalent xml declaration:
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="order" value="0" />
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="validator" ref="validator" />
</bean>
</property>
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
<bean class="org.springframework.http.converter.StringHttpMessageConverter" />
<bean class="org.springframework.http.converter.FormHttpMessageConverter" />
<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter" />
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
<bean id="conversion-service" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" />
They're exactly the same when you declare the <mvc:annotation-driven/> based on what I've researched.
Thanks to everybody who helped me out.

Except for web.xml there is no predefined order.
This happens:
web.xml is loaded by the servlet engine, this triggers the load of all defined servlets, filters, listeners,
the ContextLoaderListener loads the
root application context XML, this
might include a bean definition for a
LocalSessionFactoryBean, triggering
the load of all Hibernate mapping XML
files
the DispatcherServlet loads the web
application context XML
Study the web.xml to determine the order in each case.
see also:
link

You probably have to add the mvc namespace to the application context:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
>
(other namespaces stripped)

Related

Multi module Spring-Hibernate-Maven web application, spring not resolving entities/daos

I've got a strange problem which I think is Spring related. I'm developing a Maven multi module application structured as follow:
DataModule (includes generale Hibernate entities and daos)
ServiceModule (includes specific Hibernate entities and daos) - depends on DataModule
Web (gwt web module) - depends on ServiceModule
In DataModule I've a classic Spring-Hibernate xml configuration, consisting of (relevant code):
ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans ..>
<!-- Auto scan the components -->
<context:annotation-config />
<context:component-scan base-package="com.x.dataModule" >
<context:include-filter type="regex"
expression="com.x.dataModule.dao.*" />
</context:component-scan>
<!-- Import resources -->
<import resource="applicationContext-hibernate.xml" />
<import resource="applicationContext-dataSource.xml" />
</beans>
applicationContext-hibernate.xml
<beans ..>
<bean id="defaultLobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler" />
<!-- Hibernate session factory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean ">
<property name="packagesToScan" value="com.x.dataModel.model.**.*"></property>
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
<property name="lobHandler" ref="defaultLobHandler" />
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.query.substitutions">true 1, false 0</prop>
<prop key="hibernate.connection.useUnicode">true</prop>
<prop key="hibernate.connection.charSet">UTF8</prop>
</props>
</property>
</bean>
<!-- Transaction Manager -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="persistenceExceptionTranslationPostProcessor"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
</beans>
Hibernate.cfg is actually empty, since I aim to avoid explicit mapping due the use of packagesToScan property. applicationContext-dataSource is not relevant since it contains ds configuration.
The ServiceModule relies on the previous module.
applicationContext.xml
<beans ...>
<!-- Auto scan the components -->
<context:include-filter type="regex"
expression="com.x.serviceModule.dao.*" />
<context:include-filter type="regex"
expression="com.x.serviceModule.manager.*" />
<!-- Import resources -->
<context:property-override location="classpath:override.properties"/>
<import resource="classpath*:/applicationContext-hibernate.xml" />
<import resource="classpath*:/applicationContext-dataSource.xml" />
</beans>
The file override.properties contains the following entry, to modify the definition of packagesToScan
sessionFactory.packagesToScan=com.x.*.model.**.*
At this point everything works just fine. The serviceManagers (singletons) are loaded from the factory as
ApplicationContext appContext = new ClassPathXmlApplicationContext(
"applicationContext.xml").getBean(..);
And everything is behaving as expected.
Last part, the Web Module. If I import the ServiceModule and test it as a Java application, again everything is fine. However, if I start the module as a web application, it fails complaining that it's not able to load entities/daos defined in the DataModule.
Relevant files in Web Module:
WEB-INF/spring
applicationContext.xml: nothing here other than the import of applicationContext-security.xml (residing in the same directory)
web.xml
<!-- Spring context -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
WEB-INF/spring/applicationContext.xml
</param-value>
</context-param>
<!-- Creates the Spring Container -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>
The only way to avoid the problem is to declare explicitly the entities mapping and daos definition in the DataModule xml files.
My question: is there a way to avoid such declarations, and rely entirely on the component/package scan also in Web Module?
I'm not really sure but looks like that somehow the spring web context is conflicting with my other spring context. Otherwise, I don't understand why everything is fine as Java application, but fails as web.
Also (not sure if relevant) it seems that the Session Factory bean is istantiated twice during the process (shouldn't it be a singleton?).
Spring version is 3.1.1 Release, Hibernate 3.6.0 Final (4.0.1 for commons-annotations). Tomcat 7 for deploy.
I didn't provide the Java code of entities/daos/managers since I don't think it's relevant for the question; however, I can provide it if it's of any help. Thanks a lot

Error occur when I start project that using DWR3.0 RC2 with Spring

I introduce DWR into my project these days. My project is using Spring 4, I was trying to integrate DWR with Spring by using annotation approch. Below are my code for integration work.
An error shown as below occured when I stratup my project on the Tomcat server.
the error:
java.lang.NoSuchMethodError: org.springframework.util.ClassUtils.forName(Ljava/lang/String;)Ljava/lang/Class;
at org.directwebremoting.spring.DwrAnnotationPostProcessor.getBeanDefinitionClass(DwrAnnotationPostProcessor.java:96)
at org.directwebremoting.spring.DwrAnnotationPostProcessor.postProcessBeanFactory(DwrAnnotationPostProcessor.java:52)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPos
Processors(PostProcessorRegistrationDelegate.java:265)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:177)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:609)
web.xml
<servlet>
<servlet-name>dwr-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
dwr-dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.directwebremoting.org/schema/spring-dwr
http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="com.xdfaint.webapp.apps.*.action" />
<dwr:configuration />
<dwr:annotation-config id="dwrAnnotationConfig" />
<dwr:url-mapping />
<dwr:controller id="dwrController" debug="true" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="order" value="1" />
</bean>
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
<property name="order" value="2" />
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/core/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
I haven't found any solution yet. Then I try to use Spring 3.2 instead, the startup successed with no error any more. I am not sure if there are problems when Spring 4 meets DWR3. Can any body help? Thx~
DWR haven't updated for a long time, and I am not using it anymore. But it is still great helpful that worth to read source codes for studying, like concept of how rest api binding, object operating. About object operating, I found some useful functions like setObject(), was using massively when I did a frontend components encapsulation work. Both setObject function codes and using demo list below, with it I can init a filed in a any nested level of the object, even if there are number of parents object of that new field hasn't been declared yet.
In Spring 4 there is no method forName in ClassUtils that only accepts String as an argument. As you can see from the JavaDoc here that method was already deprecated in Spring 3
It seems that DWR has not yet upgraded their code base to support Spring 4. Seems like you'll have to wait for that to happen (although the project seems to be non-active so I wouldn't count on that happening any time soon).

Property 'dataSource' is required Error in java (Spring)

I am developing an web application in Java (Spring)
My java file is as,
try
{
JdbcTemplate jt = new JdbcTemplate(dataSource);
System.out.println("Connection ....."+jt.toString());
Connection conn;
Statement st;
conn =DriverManager.getConnection(jt.toString());
conn = (Connection) jt.getDataSource();
st=conn.createStatement();
System.out.println("Connection created....."+st);
}
catch (Exception e) {
System.out.println("Error Found...."+ e.getMessage());
System.out.println("Strack Trace....."+e.getStackTrace());
}
My spring-servlet.xml file is as,
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/cjbranchdb" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<bean id="JdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource"><ref bean="dataSource"/></property>
</bean>
But it gets an error as,
Error Found: Property 'dataSource' is required.
Strack Trace: [Ljava.lang.StackTraceElement;#7948dd
Here, I want to make a connection in Java file and pass it to the another variable as Jasper Report.
Please help, How to fix this issue?
I am guessing you are completely new to Java, JEE, Spring and JDBC. As I have stated in my comment, it is hard to answer your question, if what you are doing in there is incorrect in its base. I will try to go through few topics and hopefully also pin point where your current issue is.
Spring app structure
You need to be sure to correctly structure your project:
src
main
java - directory for Java sources
in/mmali/springtest/controller/IndexController.java - Your controller class
resources - directory for non-Java (re)sources
webapp - root for the web application resources
WEB-INF/web.xml - JEE web application configuration
WEB-INF/spring-servlet.xml - application context configuration for dispatcher servlet
pom.xml - Maven config (in case you are using Maven)
I would call this a common structure for Java project, mostly "standardized" by Maven.
Correct JEE config
You need to have correct web.xml configuration:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
This is a basic configuration (without root context), which will use your spring-servlet.xml as Spring context configuration.
Correct Spring configuration
You need to have correct Spring context configuration:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven />
<mvc:resources location="/resources/" mapping="/resources/**" />
<!-- With ROOT context we would restrict component scan on controllers here -->
<context:component-scan base-package="in.mmali.springtest" />
<!-- Data source configuration would normally go inside ROOT context. -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/cjbranchdb" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
This will load all classes annotated with #Component (and its companions #Controller, #Service, #Repository) as your beans. Bean in a context of Spring application is a object managed by Spring -> i.e. object which is being instantiated by Spring itself. When you want to work with a Spring bean, you need to have it injected (e.g. by using #Autowired annotation) or you need to pull it out from ApplicationContext#getBean manually.
Working with JDBC
Working with JDBC is painful with all the closeable resources and checked exceptions. That is why Spring-JDBC project wraps JDBC API so you don't have to use it.
To showcase how you should work with JDBC and also how to let Spring inject dependencies, here is a simple controller:
#Controller // Will be detected by <context:component-scan>
#RequestMapping // Will be detected by <mvc:annotation-driven> (more specifically by one of its component - RequestMappingHandlerMapping)
public class IndexController {
#Autowired // Spring will inject JdbcTemplate here
private JdbcOperations jdbcOperations;
#RequestMapping // This method should be called for requests to "/"
#ResponseBody // Returned string will be returned to client... normally you would register view resolver and just return name of a JSP to render
public String renderIndex() {
// You don't need to worry about JDBC's DataSource, Connection, ResultSet, ... just use JdbcTemplate
long rowCount = jdbcOperations.queryForLong("SELECT COUNT(*) FROM my_test_table;");
return "Number of rows in database is: " + String.valueOf(rowCount);
}
}
Note, that in a real application you would not allow controller to work with your data source directly, but rather through service and data layer.
Next steps
Start using logging system and never use System.out.println in a web application again ;). I suggest slf4j with its simple binding for start (later you can configure it to use logback or log4j).
Configure your application to use transactions. Use Spring's transaction handling (<tx:annotation-driven/> with #Transactional). It might look as magic at first, but when you discover something about AOP and proxy classes, you will then start really appretiating the principles of how Spring works.
Split your application logic to service layer and data (DAO) layer.
Check Spring's sample application (http://docs.spring.io/docs/petclinic.html) and reference application (https://github.com/spring-projects/greenhouse).
JdbcTemplate jt = new JdbcTemplate(dataSource); You cant just use new keyword to construct a bean object. Obviously the references in that object will be null.
Instead you should ask spring to give you that bean object. Some thing like this.
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-servlet.xml");
JdbcTemplate jt = (JdbcTemplate)context.getBean("JdbcTemplate");
In this case spring will inject the dependency ie datasource as given in configuration.
According to the file name, I assume you that you are building a web-based application.
For capturing the error, please use a proper logging framework instead of System.out.println.
According to the error, dataSource seems null. Please kindly check if your web.xml has defined the servlet xml file, such as
<servlet>
<servlet-name>spring_app</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
In addition, DriverManagerDataSource is good enough for testing only. For production use, please consider Apache's Jakarta Commons DBCP or C3P0. See detail at the official webpage.
Since your XML file has mentioned
<bean id="JdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource"><ref bean="dataSource"/></property>
</bean>
, the XML file should consist of a servlet section to call the object of id=JdbcTemplate. For example,
<bean id="MyServlet" class="com.my.MyServlet">
<property name="jdbcTemplate"><ref bean="JdbcTemplate"/></property>
</bean>

How do you configure jax-ws to work with Spring using jax-ws commons?

In web.xml I have the following:
<servlet>
<description>JAX-WS endpoint - EARM</description>
<display-name>jaxws-servlet</display-name>
<servlet-name>jaxws-servlet</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSSpringServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>jaxws-servlet</servlet-name>
<url-pattern>/webServices/*</url-pattern>
</servlet-mapping>
In my application context I have the following definitions:
<bean id="helloService" class="com.foo.HelloServiceImpl">
<property name="regularService" ref="regularService" />
</bean>
<wss:binding url="/webServices/helloService" service="#helloService" />
I get a NullPointerException when trying to access the WSDL:
java.lang.NullPointerException
at com.sun.xml.ws.transport.http.HttpAdapter.<init>(HttpAdapter.java:145)
at com.sun.xml.ws.transport.http.servlet.ServletAdapter.<init>(ServletAdapter.java:76)
at com.sun.xml.ws.transport.http.servlet.ServletAdapterList.createHttpAdapter(ServletAdapterList.java:5 0)
at com.sun.xml.ws.transport.http.servlet.ServletAdapterList.createHttpAdapter(ServletAdapterList.java:4 7)
at com.sun.xml.ws.transport.http.HttpAdapterList.createAdapter(HttpAdapterList.java:73)
at com.sun.xml.ws.transport.http.servlet.SpringBinding.create(SpringBinding.java:24)
at com.sun.xml.ws.transport.http.servlet.WSSpringServlet.init(WSSpringServlet.java:46)
Strange ... appears to be a configuration error but the darn thing just dies with a NullPointerException!!!!!!!! No logging is provided.
Deployed in Resin.
UPDATE: I finally figured out the real answer to this problem and posted
it here:
http://forum.springsource.org/showthread.php?p=286701
In short, Resin 3x ships with an
XSD-unaware parser and you have to
replace it with Apache Xerces or some
other parser (see above forum post).
=========================
The following bean definitions get the Spring JAX-WS working (without using the "stupid" xbean / namespace magic). To come by this, I had to read the source and figure out the correct classes to use - sometimes by intentionally providing a property value that I knew would cause an exception (such as an invalid Class - this allowed me to see the stack trace which lead back to the bean class).
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="webServiceMethodInterceptor"
class="com.webservice.util.interceptors.WebServiceMethodInterceptor">
<property name="userId" value="hello" />
<property name="password" value="world" />
<property name="roles" value="ROLE_ANONYMOUS,ROLE_MICKEY_MOUSE" />
</bean>
<bean id="webServiceLoggingInterceptor"
class="com.webservice.util.interceptors.LoggingInterceptor">
<property name="level" value="debug" />
</bean>
<bean id="webServiceExceptionTranslator"
class="com.webservice.util.interceptors.WebServiceExceptionTranslator"/>
<!-- The list of interceptors to apply to all web methods -->
<bean id="webServiceInterceptors" class="java.util.LinkedList">
<constructor-arg index="0">
<list>
<value>webServiceExceptionTranslator</value>
<value>webServiceLoggingInterceptor</value>
<value>webServiceMethodInterceptor</value>
</list>
</constructor-arg>
</bean>
<!-- Proxied ExampleWebService -->
<bean id="exampleWebService" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<bean class="com.webservice.ExampleWebServiceImpl">
<!-- TODO: add dependencies for web service here, for example:
<property name="someService" ref="someService" />
-->
</bean>
</property>
<property name="interceptorNames" ref="webServiceInterceptors" />
<property name="proxyTargetClass" value="true" />
</bean>
<!-- JAX-WS Endpoint for ExampleWebService -->
<bean class="com.sun.xml.ws.transport.http.servlet.SpringBinding">
<property name="url" value="/webServices/exampleService" />
<property name="service">
<bean class="org.jvnet.jax_ws_commons.spring.SpringService">
<property name="bean">
<ref local="exampleWebService" />
</property>
<property name="impl"
value="com.webservice.ExampleWebServiceImpl" />
</bean>
</property>
</bean>
Everything else is as in the tutorial linked to above (you add the JAX-WS Spring servlet in web.xml, add the servlet filter so that web service requests get routed to the jaxws servlet).
The key to using the proxied web service instance (thus enabling dependency injection, AOP, etc.) was setting proxyTargetClass="true" to force a CGLIB proxy instead of a JDK dynamic proxy (which requires an interface). JAX-WS seems to not like interfaces for some reason.
I hope this helps some one!
Regards,
LES
LES2: Your implementation seem most complex.
What's wrong with simple usage of SpringBeanAutowiringSupport, as described in Spring manual

Declaring an explict object dependency in Spring

The basic problem I have here is that I have one xml file that is being used as a utility file and imported into other xml files. It defines a series of objects for connecting to a platform and providing an interface to it. The beans in this file are defined to be lazy-initialised so that if you do not want to connect to the platform you will not but if you start referencing the appropriate bean then everything should get up and running.
The basic problem I have is that one of the beans in this set is not explicity referenced by any of the others but it is required to be constructed as it will call a method on one of the other beans in order to "activate" it. (It is acting as a gate keeper by switching on/off the connectivity based on what it detects as the state of the platform).
Here's a dummy of the sort of XML setup I have:
<?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd"
default-lazy-init="true">
<!-- Provides the connection to the platform -->
<bean id="PlatformConnection">
<constructor-arg ref="PlatformConnectionProperties" />
</bean>
<!-- This bean would be overriden in file importing this XML -->
<bean id="PlatformConnectionProperties"/>
<!-- Controls the databus to be on/off by listening to status on the Platform
(disconnections/reconnections etc...) -->
<bean lazy-init="false" class="PlatformStatusNotifier">
<constructor-arg ref="PlatformConnection" />
<constructor-arg ref="PlatformConnectionDataBus" />
</bean>
<!-- A non platform specific databus for client code to drop objects into -
this is the thing that client XML would reference in order to send objects out -->
<bean id="PlatformConnectionDataBus" class="DataBus"/>
<!-- Connects the DataBus to the Platform using the specific adaptor to manage the java object conversion -->
<bean lazy-init="false" class="DataBusConnector">
<constructor-arg>
<bean class="PlatformSpecificDataBusObjectSender">
<constructor-arg ref="PlatformConnection" />
</bean>
</constructor-arg>
<constructor-arg ref="PlatformConnectionDataBus" />
</bean>
</beans>
Now basically I want to remove the lazy-inits here that are required to get this thing to work properly. The objects referenced by the client XML are the PlatformConnection and the PlatformConnectionDataBus. How can I explicity declare that I want those other beans constructed if they are referenced?
You can add an explicit dependency from one bean to another by using the depends-on attribute:
<bean id="a" class="A"/>
<bean id="b" class="B" depends-on="a"/>
If I'm understanding your questin correctly, then I suggest you make all of your bean definitions lazy-init="true", and use depends-on to tie them together, for example:
<bean id="PlatformStatusNotifier" lazy-init="false" class="PlatformStatusNotifier">
<constructor-arg ref="PlatformConnection" />
<constructor-arg ref="PlatformConnectionDataBus" />
</bean>
<bean id="PlatformConnectionDataBus" lazy-init="false" class="DataBus" depends-on="PlatformStatusNotifier"/>
So if your client config were to express a dependency on PlatformConnectionDataBus, then that would trigger the initialisation of PlatformConnectionDataBus, which in turn would trigger the initialisation of PlatformStatusNotifier.

Categories