DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Frameworks
  4. Chicken and Egg Problem With Spring and Vaadin

Chicken and Egg Problem With Spring and Vaadin

Nicolas Fränkel user avatar by
Nicolas Fränkel
CORE ·
May. 04, 10 · Interview
Like (0)
Save
Tweet
Share
11.64K Views

Join the DZone community and get the full member experience.

Join For Free

The more I dive into Vaadin, the more I love it: isolation from dirty plumbing, rich components, integration with portlets, Vaadin has it all.

Anyway, the more you explore a technology, the bigger the chances you fall down the proverbial rabbit hole. I found one just yesterday and came up with a solution. The problem is the following: in Vaadin, application objects are tied to the session. Since I’m a Spring fanboy, it does make sense to use Spring to wire all my dependencies. As such, I scoped all application-related beans (application, windows, buttons, resources, …) with session like so:

<?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:context="http://www.springframework.org/schema/context"
  xmlns:aop="http://www.springframework.org/schema/aop"
  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/aop

    http://www.springframework.org/schema/aop/spring-aop.xsd">

  <context:annotation-config />

  <bean id="application" scope="session">
  <aop:scoped-proxy />
...
</beans>

This works nicely, until you happen to use DI to wire further. By wire further, I mean wiring windows into application, button into windows and then resources (icons) into buttons. It is the last  step that cause a circular dependency. Icon resources are implemented in Vaadin by com.vaadin.terminal.ClassResource. This class has two constructors that both needs an application instance.

The dependency cycle is thus: application -> window -> button -> resource -> application. Spring don’t like it and protests energically to it by throwing an exception which is labeled like so Requested bean is currently in creation: Is there an unresolvable circular reference?

. I think, in this case, that the design of the ClassResource is not adapted. That’s whyI designed a class aligned with Spring deferred instantiation: since the application is session-scoped, Spring uses a proxy that defers instantation from application start (Spring normal behaviour) to session use. The point is to implement the ApplicationResource interface, which needs an interface, but to set application only when needed:

public class DeferredClassResource implements ApplicationResource {

private static final long serialVersionUID = 1L;
private int bufferSize = 0;
private long cacheTime = DEFAULT_CACHETIME;
private Class<?> associatedClass;
private final String resourceName;
private Application application;

public DeferredClassResource(String resourceName) {

if (resourceName == null) {

throw new IllegalArgumentException("Resource name cannot be null");
}

this.resourceName = resourceName;
}

public DeferredClassResource(Class<?> associatedClass, String resourceName) {

this(resourceName);

if (associatedClass == null) {

throw new IllegalArgumentException("Associated class cannot be null");
}

this.associatedClass = associatedClass;
}

... // standard getters

@Override
public String getFilename() {

int index = 0;

int next = 0;

while ((next = resourceName.indexOf('/', index)) > 0
&& next + 1 < resourceName.length()) {
index = next + 1;
}

return resourceName.substring(index);
}

@Override
public DownloadStream getStream() {

final DownloadStream ds = new DownloadStream(associatedClass
.getResourceAsStream(resourceName), getMIMEType(),
getFilename());

ds.setBufferSize(getBufferSize());

ds.setCacheTime(cacheTime);

return ds;
}

@Override
public String getMIMEType() {

return FileTypeResolver.getMIMEType(resourceName);
}

public void setApplication(Application application) {

if (this.application == application) {

return;
}

if (this.application != null) {

throw new IllegalStateException("Application is already set for this resource");
}

this.application = application;

associatedClass = application.getClass();

application.addResource(this);
}
}

DeferredClassResource is just a copy of ClassResource, but with adaptations that let the application be set later, not only in constructors. My problem is solved, I just need to let application know my resources so it can call setApplication(this) in a @PostConstruct annotated method.

From http://blog.frankel.ch/chicken-and-egg-problem-with-spring-and-vaadin

Spring Framework Vaadin application EGG (file format)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Important Takeaways for PostgreSQL Indexes
  • How Agile Architecture Spikes Are Used in Shift-Left BDD
  • Securing Cloud-Native Applications: Tips and Tricks for Secure Modernization
  • Key Elements of Site Reliability Engineering (SRE)

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: