Skip to content
This repository was archived by the owner on Dec 4, 2023. It is now read-only.

Commit d224f55

Browse files
authored
Refactored samples to use @bean method for Bot instead of @component. (#964)
1 parent 28fe834 commit d224f55

File tree

42 files changed

+611
-264
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+611
-264
lines changed

samples/02.echo-bot/src/main/java/com/microsoft/bot/sample/echo/Application.java

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,55 @@
33

44
package com.microsoft.bot.sample.echo;
55

6+
import com.microsoft.bot.builder.Bot;
67
import com.microsoft.bot.integration.AdapterWithErrorHandler;
78
import com.microsoft.bot.integration.BotFrameworkHttpAdapter;
89
import com.microsoft.bot.integration.Configuration;
910
import com.microsoft.bot.integration.spring.BotController;
1011
import com.microsoft.bot.integration.spring.BotDependencyConfiguration;
1112
import org.springframework.boot.SpringApplication;
1213
import org.springframework.boot.autoconfigure.SpringBootApplication;
14+
import org.springframework.context.annotation.Bean;
1315
import org.springframework.context.annotation.Import;
1416

15-
/**
16-
* This is the starting point of the Sprint Boot Bot application.
17-
* <p>
18-
* This class also provides overrides for dependency injections. A class that extends the {@link
19-
* com.microsoft.bot.builder.Bot} interface should be annotated with @Component.
20-
*
21-
* @see EchoBot
22-
*/
17+
//
18+
// This is the starting point of the Sprint Boot Bot application.
19+
//
2320
@SpringBootApplication
2421

2522
// Use the default BotController to receive incoming Channel messages. A custom
2623
// controller could be used by eliminating this import and creating a new
27-
// RestController.
24+
// org.springframework.web.bind.annotation.RestController.
2825
// The default controller is created by the Spring Boot container using
2926
// dependency injection. The default route is /api/messages.
3027
@Import({BotController.class})
3128

29+
/**
30+
* This class extends the BotDependencyConfiguration which provides the default
31+
* implementations for a Bot application. The Application class should
32+
* override methods in order to provide custom implementations.
33+
*/
3234
public class Application extends BotDependencyConfiguration {
3335

3436
public static void main(String[] args) {
3537
SpringApplication.run(Application.class, args);
3638
}
3739

40+
/**
41+
* Returns the Bot for this application.
42+
*
43+
* <p>
44+
* The @Component annotation could be used on the Bot class instead of this method
45+
* with the @Bean annotation.
46+
* </p>
47+
*
48+
* @return The Bot implementation for this application.
49+
*/
50+
@Bean
51+
public Bot getBot() {
52+
return new EchoBot();
53+
}
54+
3855
/**
3956
* Returns a custom Adapter that provides error handling.
4057
*

samples/02.echo-bot/src/main/java/com/microsoft/bot/sample/echo/EchoBot.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import com.microsoft.bot.builder.TurnContext;
1010
import com.microsoft.bot.schema.ChannelAccount;
1111
import org.apache.commons.lang3.StringUtils;
12-
import org.springframework.stereotype.Component;
1312

1413
import java.util.List;
1514
import java.util.concurrent.CompletableFuture;
@@ -23,7 +22,6 @@
2322
* #onMembersAdded(List, TurnContext)} will send a greeting to new conversation participants.
2423
* </p>
2524
*/
26-
@Component
2725
public class EchoBot extends ActivityHandler {
2826

2927
@Override

samples/03.welcome-user/src/main/java/com/microsoft/bot/sample/welcomeuser/Application.java

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,55 @@
33

44
package com.microsoft.bot.sample.welcomeuser;
55

6+
import com.microsoft.bot.builder.Bot;
7+
import com.microsoft.bot.builder.UserState;
68
import com.microsoft.bot.integration.AdapterWithErrorHandler;
79
import com.microsoft.bot.integration.BotFrameworkHttpAdapter;
810
import com.microsoft.bot.integration.Configuration;
911
import com.microsoft.bot.integration.spring.BotController;
1012
import com.microsoft.bot.integration.spring.BotDependencyConfiguration;
1113
import org.springframework.boot.SpringApplication;
1214
import org.springframework.boot.autoconfigure.SpringBootApplication;
15+
import org.springframework.context.annotation.Bean;
1316
import org.springframework.context.annotation.Import;
1417

15-
/**
16-
* This is the starting point of the Sprint Boot Bot application.
17-
*
18-
* This class also provides overrides for dependency injections. A class that
19-
* extends the {@link com.microsoft.bot.builder.Bot} interface should be
20-
* annotated with @Component.
21-
*
22-
* @see WelcomeUserBot
23-
*/
18+
//
19+
// This is the starting point of the Sprint Boot Bot application.
20+
//
2421
@SpringBootApplication
2522

2623
// Use the default BotController to receive incoming Channel messages. A custom
2724
// controller could be used by eliminating this import and creating a new
28-
// RestController.
25+
// org.springframework.web.bind.annotation.RestController.
2926
// The default controller is created by the Spring Boot container using
3027
// dependency injection. The default route is /api/messages.
3128
@Import({BotController.class})
3229

30+
/**
31+
* This class extends the BotDependencyConfiguration which provides the default
32+
* implementations for a Bot application. The Application class should
33+
* override methods in order to provide custom implementations.
34+
*/
3335
public class Application extends BotDependencyConfiguration {
3436
public static void main(String[] args) {
3537
SpringApplication.run(Application.class, args);
3638
}
3739

40+
/**
41+
* Returns the Bot for this application.
42+
*
43+
* <p>
44+
* The @Component annotation could be used on the Bot class instead of this method
45+
* with the @Bean annotation.
46+
* </p>
47+
*
48+
* @return The Bot implementation for this application.
49+
*/
50+
@Bean
51+
public Bot getBot(UserState userState) {
52+
return new WelcomeUserBot(userState);
53+
}
54+
3855
/**
3956
* Returns a custom Adapter that provides error handling.
4057
*

samples/03.welcome-user/src/main/java/com/microsoft/bot/sample/welcomeuser/WelcomeUserBot.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import com.microsoft.bot.schema.ResourceResponse;
1919
import org.apache.commons.lang3.StringUtils;
2020
import org.springframework.beans.factory.annotation.Autowired;
21-
import org.springframework.stereotype.Component;
2221

2322
import java.util.Arrays;
2423
import java.util.Collections;
@@ -36,7 +35,6 @@
3635
*
3736
* @see WelcomeUserState
3837
*/
39-
@Component
4038
public class WelcomeUserBot extends ActivityHandler {
4139
// Messages sent to the user.
4240
private static final String WELCOMEMESSAGE =

samples/05.multi-turn-prompt/src/main/java/com/microsoft/bot/sample/multiturnprompt/Application.java

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,76 @@
33

44
package com.microsoft.bot.sample.multiturnprompt;
55

6+
import com.microsoft.bot.builder.Bot;
7+
import com.microsoft.bot.builder.ConversationState;
8+
import com.microsoft.bot.builder.UserState;
9+
import com.microsoft.bot.dialogs.Dialog;
610
import com.microsoft.bot.integration.AdapterWithErrorHandler;
711
import com.microsoft.bot.integration.BotFrameworkHttpAdapter;
812
import com.microsoft.bot.integration.Configuration;
913
import com.microsoft.bot.integration.spring.BotController;
1014
import com.microsoft.bot.integration.spring.BotDependencyConfiguration;
1115
import org.springframework.boot.SpringApplication;
1216
import org.springframework.boot.autoconfigure.SpringBootApplication;
17+
import org.springframework.context.annotation.Bean;
1318
import org.springframework.context.annotation.Import;
1419

15-
/**
16-
* This is the starting point of the Sprint Boot Bot application.
17-
*
18-
* This class also provides overrides for dependency injections. A class that
19-
* extends the {@link com.microsoft.bot.builder.Bot} interface should be
20-
* annotated with @Component.
21-
*
22-
* @see DialogBot
23-
*/
20+
//
21+
// This is the starting point of the Sprint Boot Bot application.
22+
//
2423
@SpringBootApplication
2524

2625
// Use the default BotController to receive incoming Channel messages. A custom
2726
// controller could be used by eliminating this import and creating a new
28-
// RestController.
27+
// org.springframework.web.bind.annotation.RestController.
2928
// The default controller is created by the Spring Boot container using
3029
// dependency injection. The default route is /api/messages.
3130
@Import({BotController.class})
3231

32+
/**
33+
* This class extends the BotDependencyConfiguration which provides the default
34+
* implementations for a Bot application. The Application class should
35+
* override methods in order to provide custom implementations.
36+
*/
3337
public class Application extends BotDependencyConfiguration {
3438
public static void main(String[] args) {
3539
SpringApplication.run(Application.class, args);
3640
}
3741

42+
/**
43+
* Returns the Bot for this application.
44+
*
45+
* <p>
46+
* The @Component annotation could be used on the Bot class instead of this method
47+
* with the @Bean annotation.
48+
* </p>
49+
*
50+
* @return The Bot implementation for this application.
51+
*/
52+
@Bean
53+
public Bot getBot(
54+
ConversationState conversationState,
55+
UserState userState,
56+
Dialog dialog
57+
) {
58+
return new DialogBot(conversationState, userState, dialog);
59+
}
60+
61+
/**
62+
* Returns the starting Dialog for this application.
63+
*
64+
* <p>
65+
* The @Component annotation could be used on the Dialog class instead of this method
66+
* with the @Bean annotation.
67+
* </p>
68+
*
69+
* @return The Dialog implementation for this application.
70+
*/
71+
@Bean
72+
public Dialog getRootDialog(UserState userState) {
73+
return new UserProfileDialog(userState);
74+
}
75+
3876
/**
3977
* Returns a custom Adapter that provides error handling.
4078
*

samples/05.multi-turn-prompt/src/main/java/com/microsoft/bot/sample/multiturnprompt/DialogBot.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import com.microsoft.bot.builder.TurnContext;
1111
import com.microsoft.bot.builder.UserState;
1212
import java.util.concurrent.CompletableFuture;
13-
import org.springframework.stereotype.Component;
1413

1514
/**
1615
* This IBot implementation can run any type of Dialog. The use of type parameterization is to
@@ -21,7 +20,6 @@
2120
* been used in a Dialog implementation, and the requirement is that all BotState objects are
2221
* saved at the end of a turn.
2322
*/
24-
@Component
2523
public class DialogBot extends ActivityHandler {
2624
protected Dialog dialog;
2725
protected BotState conversationState;

samples/05.multi-turn-prompt/src/main/java/com/microsoft/bot/sample/multiturnprompt/UserProfileDialog.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@
2727
import java.util.List;
2828
import java.util.concurrent.CompletableFuture;
2929
import org.apache.commons.lang3.StringUtils;
30-
import org.springframework.stereotype.Component;
3130

32-
@Component
3331
public class UserProfileDialog extends ComponentDialog {
3432
private StatePropertyAccessor<UserProfile> userProfileAccessor;
3533

samples/06.using-cards/src/main/java/com/microsoft/bot/sample/usingcards/Application.java

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,76 @@
33

44
package com.microsoft.bot.sample.usingcards;
55

6+
import com.microsoft.bot.builder.Bot;
7+
import com.microsoft.bot.builder.ConversationState;
8+
import com.microsoft.bot.builder.UserState;
9+
import com.microsoft.bot.dialogs.Dialog;
610
import com.microsoft.bot.integration.AdapterWithErrorHandler;
711
import com.microsoft.bot.integration.BotFrameworkHttpAdapter;
812
import com.microsoft.bot.integration.Configuration;
913
import com.microsoft.bot.integration.spring.BotController;
1014
import com.microsoft.bot.integration.spring.BotDependencyConfiguration;
1115
import org.springframework.boot.SpringApplication;
1216
import org.springframework.boot.autoconfigure.SpringBootApplication;
17+
import org.springframework.context.annotation.Bean;
1318
import org.springframework.context.annotation.Import;
1419

15-
/**
16-
* This is the starting point of the Sprint Boot Bot application.
17-
*
18-
* This class also provides overrides for dependency injections. A class that
19-
* extends the {@link com.microsoft.bot.builder.Bot} interface should be
20-
* annotated with @Component.
21-
*
22-
* @see RichCardsBot
23-
*/
20+
//
21+
// This is the starting point of the Sprint Boot Bot application.
22+
//
2423
@SpringBootApplication
2524

2625
// Use the default BotController to receive incoming Channel messages. A custom
2726
// controller could be used by eliminating this import and creating a new
28-
// RestController.
27+
// org.springframework.web.bind.annotation.RestController.
2928
// The default controller is created by the Spring Boot container using
3029
// dependency injection. The default route is /api/messages.
3130
@Import({BotController.class})
3231

32+
/**
33+
* This class extends the BotDependencyConfiguration which provides the default
34+
* implementations for a Bot application. The Application class should
35+
* override methods in order to provide custom implementations.
36+
*/
3337
public class Application extends BotDependencyConfiguration {
3438
public static void main(String[] args) {
3539
SpringApplication.run(Application.class, args);
3640
}
3741

42+
/**
43+
* Returns the Bot for this application.
44+
*
45+
* <p>
46+
* The @Component annotation could be used on the Bot class instead of this method
47+
* with the @Bean annotation.
48+
* </p>
49+
*
50+
* @return The Bot implementation for this application.
51+
*/
52+
@Bean
53+
public Bot getBot(
54+
ConversationState conversationState,
55+
UserState userState,
56+
Dialog rootDialog
57+
) {
58+
return new RichCardsBot(conversationState, userState, rootDialog);
59+
}
60+
61+
/**
62+
* Returns the starting Dialog for this application.
63+
*
64+
* <p>
65+
* The @Component annotation could be used on the Dialog class instead of this method
66+
* with the @Bean annotation.
67+
* </p>
68+
*
69+
* @return The Dialog implementation for this application.
70+
*/
71+
@Bean
72+
public Dialog getRootDialog() {
73+
return new MainDialog();
74+
}
75+
3876
/**
3977
* Returns a custom Adapter that provides error handling.
4078
*

samples/06.using-cards/src/main/java/com/microsoft/bot/sample/usingcards/MainDialog.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020
import java.util.Arrays;
2121
import java.util.List;
2222
import java.util.concurrent.CompletableFuture;
23-
import org.springframework.stereotype.Component;
2423

25-
@Component
2624
public class MainDialog extends ComponentDialog {
2725
public MainDialog() {
2826
super("MainDialog");

samples/06.using-cards/src/main/java/com/microsoft/bot/sample/usingcards/RichCardsBot.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,21 @@
88
import com.microsoft.bot.builder.MessageFactory;
99
import com.microsoft.bot.builder.TurnContext;
1010
import com.microsoft.bot.builder.UserState;
11+
import com.microsoft.bot.dialogs.Dialog;
1112
import com.microsoft.bot.schema.Activity;
1213
import com.microsoft.bot.schema.ChannelAccount;
1314
import java.util.List;
1415
import java.util.concurrent.CompletableFuture;
1516
import org.apache.commons.lang3.StringUtils;
16-
import org.springframework.stereotype.Component;
1717

1818
// RichCardsBot prompts a user to select a Rich Card and then returns the card
1919
// that matches the user's selection.
20-
@Component
21-
public class RichCardsBot extends DialogBot<MainDialog> {
20+
public class RichCardsBot extends DialogBot<Dialog> {
2221

2322
public RichCardsBot(
2423
ConversationState withConversationState,
2524
UserState withUserState,
26-
MainDialog withDialog
25+
Dialog withDialog
2726
) {
2827
super(withConversationState, withUserState, withDialog);
2928
}

0 commit comments

Comments
 (0)