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
  1. DZone
  2. Coding
  3. Frameworks
  4. Custom scopes in CDI 1.0 and Spring 3.1

Custom scopes in CDI 1.0 and Spring 3.1

Niklas Schlimm user avatar by
Niklas Schlimm
·
Jul. 05, 11 · Interview
Like (0)
Save
Tweet
Share
10.58K Views

Join the DZone community and get the full member experience.

Join For Free

This blog post describes in short how to implement custom scopes in CDI using Weld 1.1 and Spring 3.1. To get basic information on scoping see the Spring reference or the Weld reference respectively.

Custom scopes in Spring

In Spring you have to implement your custom scope using the Scope interface. I have implemented a custom scope as a copy of request scope for illustration:

package com.mycompany.springapp.scope;
 
import org.springframework.beans.factory.config.Scope;
import org.springframework.web.context.request.AbstractRequestAttributesScope;
import org.springframework.web.context.request.RequestAttributes;
 
public class MyCustomScope extends AbstractRequestAttributesScope implements Scope {
 
 @Override
 protected int getScope() {
  return RequestAttributes.SCOPE_REQUEST;
 }
 
 /**
  * There is no conversation id concept for a request, so this method
  * returns <code>null</code>.
  */
 public String getConversationId() {
  return null;
 }
 
}
Then you register your new scope like this in your beans.xml:

<beans>
 <bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
  <property name="scopes">
   <map>
    <entry key="custom">
     <bean class="com.mycompany.springapp.scope.MyCustomScope">
    </bean></entry>
   </map>
  </property>
 </bean>
</beans>

You can then use it like all the other built-in scopes:
package com.mycompany.springapp.scope;
 
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
 
@Component
@Scope("custom")
public class MyCustomScopedService {
 
}

See here for additional information.

Custom scopes with CDI

In CDI - according to the CDI programming model - you first define your own scope annotation:

package com.mycompany.jeeapp.scope.extension;
 
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
 
import javax.enterprise.context.NormalScope;
 
@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@NormalScope
public @interface CustomScoped {
 
}
Then there is the Context interface that you need to implement. I have made it a copy of HttpRequestScope for illustration.
	
package com.mycompany.jeeapp.scope.extension;
 
import org.jboss.weld.context.http.HttpRequestContext;
import org.jboss.weld.context.http.HttpRequestContextImpl;
import java.lang.Class;
 
public class MyCustomScope extends HttpRequestContextImpl implements HttpRequestContext {
 
 public Class getScope() {
  return CustomScoped.class;
 }
  
}

You assign your new scope annotation to the beans that are instintiated in this scope:
@CustomScoped
public class MyCustomScopeService { ... }
To register the scope create this class and drop it together with your application into the CDI enabled container. The CDI runtime will register it automatically.

package com.mycompany.jeeapp.scope.extension;
 
import javax.enterprise.event.Observes;
import javax.enterprise.inject.spi.AfterBeanDiscovery;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.Extension;
 
public class MyCustomScopeExtension implements Extension{
 
    public void afterBeanDiscovery(@Observes AfterBeanDiscovery event, BeanManager manager) {
        event.addContext(new MyCustomScope());
    }
}

See this blog entry for a comprehensive example.

Nothing to complain about! Both technologies perfectly support extensions for scopes and context.

 

From http://niklasschlimm.blogspot.com/2011/07/custom-scopes-in-cdi-10-and-spring-31.html

CDI Spring Framework

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Introduction to Automation Testing Strategies for Microservices
  • Practical Example of Using CSS Layer
  • Using GPT-3 in Our Applications
  • 7 Most Sought-After Front-End Frameworks for Web Developers

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: