-
-
Save superalsrk/7910949 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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--; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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