请选择 进入手机版 | 继续访问电脑版
楼主: Lisrelchen
1185 14

Spring Cookbook [推广有奖]

  • 0关注
  • 62粉丝

VIP

院士

67%

还不是VIP/贵宾

-

TA的文库  其他...

Bayesian NewOccidental

Spatial Data Analysis

东西方数据挖掘

威望
0
论坛币
49957 个
通用积分
79.4887
学术水平
253 点
热心指数
300 点
信用等级
208 点
经验
41518 点
帖子
3256
精华
14
在线时间
766 小时
注册时间
2006-5-4
最后登录
2022-11-6

Lisrelchen 发表于 2016-11-20 05:16:23 |显示全部楼层 |坛友微信交流群
相似文件 换一批

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币


本帖隐藏的内容

Spring Cookbook.pdf (3.06 MB)


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Cookbook Spring Pring Book Cook

已有 1 人评分经验 学术水平 热心指数 信用等级 收起 理由
cmwei333 + 60 + 1 + 1 + 1 精彩帖子

总评分: 经验 + 60  学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

本帖被以下文库推荐

jcyang 发表于 2016-11-21 10:06:13 |显示全部楼层 |坛友微信交流群
看看,谢谢

使用道具

cmwei333 发表于 2016-11-22 03:49:59 |显示全部楼层 |坛友微信交流群
packt 的程序书我都很喜欢

使用道具

Lisrelchen 发表于 2017-6-7 09:43:10 |显示全部楼层 |坛友微信交流群
  1. Defining a bean explicitly with @Bean

  2. The simplest way to define a bean is to create, in a Spring configuration class, a method annotated with @Bean returning an object (the actual bean). Such beans are usually used to configure Spring in some way (database, security, view resolver, and so on). In this recipe, we'll define a bean that contains the connection details of a database.

  3. How to do it…

  4. In a Spring configuration class, add a dataSource() method annotated with @Bean and return a Datasource object. In this method, create a DriverManagerDataSource object initialized with the connection details of a database:

  5. @Bean
  6. public DataSource dataSource() {
  7.         DriverManagerDataSource dataSource = new DriverManagerDataSource();
  8.         
  9.         dataSource.setDriverClassName("com.mysql.jdbc.Driver");
  10.         dataSource.setUrl("jdbc:mysql://localhost:3306/db1");
  11.         dataSource.setUsername("root");
  12.         dataSource.setPassword("123");
  13.          
  14.         return dataSource;
  15. }
复制代码

使用道具

Lisrelchen 发表于 2017-6-7 09:43:38 |显示全部楼层 |坛友微信交流群
  1. Defining a bean implicitly with @Component

  2. Beans don't have to be defined in a Spring configuration class. Spring will automatically generate a bean from any class annotated with @Component.

  3. Getting ready

  4. We will use the basic web application created in the Creating a Spring web application recipe in Chapter 1, Creating a Spring Application.

  5. Create the com.springcookbook.service package and the following service class in it:

  6. public class UserService {
  7.   public int findNumberOfUsers() {
  8.     return 10;
  9.   }
  10. }
  11. How to do it…

  12. Here are the steps to define a bean by adding @Component to an existing class:

  13. In the Spring configuration file, in the @ComponentScan class annotation, add the com.springcookbook.service base package:
  14. @Configuration
  15. @EnableWebMvc
  16. @ComponentScan(basePackages = {"com.springcookbook.controller", "com.springcookbook.service"})
  17. public class AppConfig {  
  18. }
  19. In the UserService class, add @Component:
  20. @Component
  21. public class UserService {
  22.   public int findNumberOfUsers() {
  23.     return 10;
  24.   }
  25. }
复制代码

使用道具

Lisrelchen 发表于 2017-6-7 09:44:42 |显示全部楼层 |坛友微信交流群
  1. Using a bean via dependency injection with @Autowired

  2. Spring configuration beans, such as the one in the Defining a bean explicitly with @Bean recipe are automatically discovered and used by Spring. To use a bean (any kind of bean) in one of your classes, add the bean as a field and annotate it with @Autowired. Spring will automatically initialize the field with the bean. In this recipe, we'll use an existing bean in a controller class.

  3. Getting ready

  4. We will use the code from the Defining a bean implicitly with @Component recipe, where we defined a UserService bean.

  5. How to do it…

  6. Here are the steps to use an existing bean in one of your classes:

  7. In the controller class, add a UserService field annotated with @Autowired:
  8. @Autowired
  9. UserService userService;
  10. In a controller method, use the UserService field:
  11. @RequestMapping("hi")
  12. @ResponseBody
  13. public String hi() {
  14.   return "nb of users: " + userService.findNumberOfUsers();
  15. }
  16. In a web browser, go to http://localhost:8080/hi to check whether it's working.
复制代码

使用道具

Lisrelchen 发表于 2017-6-7 09:45:18 |显示全部楼层 |坛友微信交流群
  1. Using a bean directly

  2. It's possible to get a bean directly from Spring instead of using dependency injection by making Spring's ApplicationContext, which contains all the beans, a dependency of your class. In this recipe, we'll inject an existing bean into a controller class.

  3. Getting ready

  4. We will use the code from the Defining a bean implicitly with @Component recipe, where we defined a UserService bean.

  5. How to do it…

  6. Here are the steps to get and use a bean directly:

  7. In the controller class, add an ApplicationContext field annotated with @Autowired:
  8. @Autowired
  9. private ApplicationContext applicationContext;
  10. In a controller method, use the ApplicationContext object and its getBean() method to retrieve the UserService bean:
  11. UserService userService = (UserService)applicationContext.getBean("userService");   
复制代码

使用道具

Lisrelchen 发表于 2017-6-7 09:45:55 |显示全部楼层 |坛友微信交流群
  1. Associating a route to a controller method

  2. In this recipe, you will learn how to define a controller method to be executed for a given route.

  3. How to do it…

  4. Here are the steps for creating a controller method for a given route:

  5. Create a controller class in your controller package (for example, com.springcookbook.controller). This is a normal Java class annotated with @Controller:
  6. @Controller
  7. public class UserController {
  8. ...
  9. }
  10. Add a controller method. This is a standard Java method annotated with @RequestMapping, which takes the route as a parameter:
  11. @RequestMapping("/user/list")
  12. public void userList() {
  13. ...
  14. }
复制代码

使用道具

Lisrelchen 发表于 2017-6-7 09:46:26 |显示全部楼层 |坛友微信交流群
  1. Using a JSP view

  2. In this recipe, you'll learn how to render and return a JSP view after the execution of a controller method.

  3. How to do it…

  4. Here are the steps to create a JSP view:

  5. Add the Maven dependency for JSTL in pom.xml:
  6. <dependency>
  7.   <groupId>javax.servlet</groupId>
  8.   <artifactId>jstl</artifactId>
  9.   <version>1.2</version>
  10. </dependency>
  11. Add a JSP view resolver to the Spring configuration class:
  12. @Bean
  13. public ViewResolver jspViewResolver(){
  14.     InternalResourceViewResolver resolver = new InternalResourceViewResolver();
  15.     resolver.setViewClass(JstlView.class);
  16.     resolver.setPrefix("/WEB-INF/jsp/");
  17.     resolver.setSuffix(".jsp");
  18.     return resolver;
  19. }
  20. Create a controller method:
  21. @RequestMapping("/user/list")
  22. public void userList() {
  23.   ...
  24. }
  25. Create the /WEB-INF/jsp/user/list.jsp JSP:
  26. <html>
  27. <body>
  28.   There are many users.
  29. </body>
  30. </html>
复制代码

使用道具

Lisrelchen 发表于 2017-6-7 09:47:41 |显示全部楼层 |坛友微信交流群
  1. Passing attributes from a controller to a JSP view

  2. In this recipe, you'll learn how to set attributes in a controller method and use them in a JSP view.

  3. How to do it…

  4. Here are the steps to pass data from a controller to a view:

  5. Add a Model argument to the controller method:
  6.   @RequestMapping("/user/list")
  7.   public void userList(Model model) {
  8.   ...
  9. In the controller method, add attributes to the Model object:
  10. model.addAttribute("nbUsers", 13);
  11. Use the attributes in the JSP file:
  12. <p>There are ${nbUsers} users</p>
复制代码

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-17 03:09