Skip to content

Instantly share code, notes, and snippets.

@chocotan
Last active December 17, 2015 22:59
Show Gist options
  • Save chocotan/5686458 to your computer and use it in GitHub Desktop.
Save chocotan/5686458 to your computer and use it in GitHub Desktop.
JSR-330 in Spring3
package io.loli.jsj.test;
import io.loli.jsj.test.MyQualifier.Count;
import javax.inject.Inject;
import javax.inject.Named;
@Named("antherHelloService")
@MyQualifier(countMethod=Count.DESC)
@SessionScoped
public class AnotherHelloServiceImpl implements HelloService {
private int count=100;
@Inject
private HelloBao helloBao;
@Override
public String getHelloMessage(){
return helloBao.getHelloMessage()+ count--;
}
}
<?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"
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-3.2.xsd">
<!-- scope-resolver="org.springframework.context.annotation.Jsr330ScopeMetadataResolver" -->
<!-- default is prototype -->
<context:component-scan base-package="io.loli.jsj.test"
scope-resolver="io.loli.jsj.test.CustomScopeMetadataResolver" />
</beans>
package io.loli.jsj.test;
import org.springframework.context.annotation.Jsr330ScopeMetadataResolver;
import org.springframework.web.context.WebApplicationContext;
public class CustomScopeMetadataResolver extends Jsr330ScopeMetadataResolver {
public CustomScopeMetadataResolver() {
registerScope(RequestScoped.class.getName(),
WebApplicationContext.SCOPE_REQUEST);
registerScope(SessionScoped.class.getName(),
WebApplicationContext.SCOPE_SESSION);
}
}
package io.loli.jsj.test;
import io.loli.jsj.test.MyQualifier.Count;
import javax.inject.Inject;
import javax.inject.Named;
//@ManagedBean
//@RequestScoped
@Named("helloBean")
public class HelloBean {
//@ManagedProperty(value = "#{helloService}")
@Inject
//@Named("helloService")
@MyQualifier(countMethod=Count.DESC)
private HelloService helloService;
public String sayHello(){
return helloService.getHelloMessage();
}
public void setHelloService(HelloService helloService) {
this.helloService = helloService;
}
}
package io.loli.jsj.test;
import io.loli.jsj.test.MyQualifier.Count;
import javax.inject.Inject;
import javax.inject.Named;
@Named("helloService")
@MyQualifier(countMethod=Count.ASC)
@SessionScoped
public class HelloServiceImpl implements HelloService {
private int count;
@Inject
private HelloBao helloBao;
@Override
public String getHelloMessage(){
return helloBao.getHelloMessage()+ count++;
}
}
package io.loli.jsj.test;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.inject.Qualifier;
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
@Target(value = {ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE})
public @interface MyQualifier {
Count countMethod() default Count.ASC;
public enum Count {ASC, DESC}
}
package io.loli.jsj.test;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.annotation.RetentionPolicy;
import javax.inject.Scope;
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Scope
@Target({ ElementType.TYPE })
public @interface RequestScoped {
}
package io.loli.jsj.test;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.annotation.RetentionPolicy;
import javax.inject.Scope;
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Scope
@Target({ ElementType.TYPE })
public @interface SessionScoped {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment