2525import org .junit .Test ;
2626import org .springframework .beans .factory .annotation .Autowired ;
2727import org .springframework .context .annotation .Bean ;
28+ import org .springframework .context .annotation .ComponentScan ;
2829import org .springframework .context .annotation .Configuration ;
2930import org .springframework .mock .web .test .MockServletContext ;
31+ import org .springframework .stereotype .Component ;
3032import org .springframework .web .context .ContextLoader ;
3133import org .springframework .web .context .support .AnnotationConfigWebApplicationContext ;
3234
@@ -40,6 +42,8 @@ public class SpringConfiguratorTests {
4042
4143 private AnnotationConfigWebApplicationContext webAppContext ;
4244
45+ private SpringConfigurator configurator ;
46+
4347
4448 @ Before
4549 public void setup () {
@@ -50,6 +54,8 @@ public void setup() {
5054
5155 this .contextLoader = new ContextLoader (this .webAppContext );
5256 this .contextLoader .initWebApplicationContext (this .servletContext );
57+
58+ this .configurator = new SpringConfigurator ();
5359 }
5460
5561 @ After
@@ -59,29 +65,33 @@ public void destroy() {
5965
6066
6167 @ Test
62- public void getEndpointInstanceCreateBean () throws Exception {
63-
64- PerConnectionEchoEndpoint endpoint = new SpringConfigurator ().getEndpointInstance (PerConnectionEchoEndpoint .class );
65-
68+ public void getEndpointInstancePerConnection () throws Exception {
69+ PerConnectionEchoEndpoint endpoint = this .configurator .getEndpointInstance (PerConnectionEchoEndpoint .class );
6670 assertNotNull (endpoint );
6771 }
6872
6973 @ Test
70- public void getEndpointInstanceUseBean () throws Exception {
71-
72- EchoEndpointBean expected = this .webAppContext .getBean (EchoEndpointBean .class );
73- EchoEndpointBean actual = new SpringConfigurator ().getEndpointInstance (EchoEndpointBean .class );
74+ public void getEndpointInstanceSingletonByType () throws Exception {
75+ EchoEndpoint expected = this .webAppContext .getBean (EchoEndpoint .class );
76+ EchoEndpoint actual = this .configurator .getEndpointInstance (EchoEndpoint .class );
77+ assertSame (expected , actual );
78+ }
7479
80+ @ Test
81+ public void getEndpointInstanceSingletonByComponentName () throws Exception {
82+ AlternativeEchoEndpoint expected = this .webAppContext .getBean (AlternativeEchoEndpoint .class );
83+ AlternativeEchoEndpoint actual = this .configurator .getEndpointInstance (AlternativeEchoEndpoint .class );
7584 assertSame (expected , actual );
7685 }
7786
7887
7988 @ Configuration
89+ @ ComponentScan (basePackageClasses =SpringConfiguratorTests .class )
8090 static class Config {
8191
8292 @ Bean
83- public EchoEndpointBean echoEndpointBean () {
84- return new EchoEndpointBean (echoService ());
93+ public EchoEndpoint echoEndpoint () {
94+ return new EchoEndpoint (echoService ());
8595 }
8696
8797 @ Bean
@@ -90,13 +100,29 @@ public EchoService echoService() {
90100 }
91101 }
92102
93- private static class EchoEndpointBean extends Endpoint {
103+ private static class EchoEndpoint extends Endpoint {
104+
105+ @ SuppressWarnings ("unused" )
106+ private final EchoService service ;
107+
108+ @ Autowired
109+ public EchoEndpoint (EchoService service ) {
110+ this .service = service ;
111+ }
112+
113+ @ Override
114+ public void onOpen (Session session , EndpointConfig config ) {
115+ }
116+ }
117+
118+ @ Component ("echoEndpoint" )
119+ private static class AlternativeEchoEndpoint extends Endpoint {
94120
95121 @ SuppressWarnings ("unused" )
96122 private final EchoService service ;
97123
98124 @ Autowired
99- public EchoEndpointBean (EchoService service ) {
125+ public AlternativeEchoEndpoint (EchoService service ) {
100126 this .service = service ;
101127 }
102128
0 commit comments