当前位置: 首页 > news >正文

建设银行杭州招聘网站网站怎么做交易

建设银行杭州招聘网站,网站怎么做交易,舞蹈培训东莞网站建设,在国外社交网站做产品推广文章目录 前言存储 Bean 对象五大注解五大注解示例配置包扫描路径读取bean的示例 方法注解 Bean Bean 命名规则重命名 Bean 前言 通过在 spring-config 中添加bean的注册内容#xff0c;我们已经可以实现基本的Spring读取和存储对象的操作了#xff0c;但在操作中我们发现读… 文章目录 前言存储 Bean 对象五大注解五大注解示例配置包扫描路径读取bean的示例 方法注解 Bean Bean 命名规则重命名 Bean 前言 通过在 spring-config 中添加bean的注册内容我们已经可以实现基本的Spring读取和存储对象的操作了但在操作中我们发现读取和存储并没有那么简单接下来我们来学习更加简单的操作Bean对象的方法。 在Spring中想要更简单的存储和读取对象的核心是使用注解接下来我们来了解一下Spring中的相关注解来存储对象。 存储 Bean 对象 想要将对象存储在 Spring 中有两种注解类型可以实现 类注解Controller、Service、Repository、Component、Configuration。五大注解方法注解Bean 五大注解 在 Spring 框架中有一些核心的注解被认为是 “Spring 的五大注解”它们分别是 ComponentComponent 是一个通用的注解用于标记一个类为 Spring 托管的组件。它可以被用于任何层如数据访问、业务逻辑、控制器等并告诉 Spring 在应用程序的上下文中自动创建并管理这些组件。 RepositoryRepository 是用于标识数据访问层DAO组件的注解。它扩展了 Component并且通常被用于与数据库进行交互的组件。它提供了一些数据库访问的特殊功能如异常转换。 ServiceService 是用于标识服务层组件的注解。它也扩展了 Component并且通常用于包含业务逻辑的组件。在应用程序中服务层通常协调数据访问和业务逻辑之间的交互。 ControllerController 是用于标识控制器层组件的注解。它表示这个类是一个 Spring MVC 控制器用于处理 HTTP 请求并返回响应。通常它处理用户界面的交互负责将用户请求映射到相应的处理方法。 RestControllerRestController 是 Spring 4.0 版本引入的注解用于标识 RESTful 风格的控制器。与 Controller 不同的是RestController 不仅处理请求映射还会自动将方法返回值转换为 JSON 或 XML 格式的响应。 这些注解是 Spring 框架中非常重要的一部分它们使得应用程序的组件管理、数据访问、业务逻辑和控制器层的开发更加简单和高效。 五大注解示例 当使用 Spring 框架时可以通过以下示例展示每个注解的用法 Component 示例 import org.springframework.stereotype.Component;Component public class MyComponent {// 类的定义 }Repository 示例 import org.springframework.stereotype.Repository;Repository public class UserRepository {// 数据访问相关代码 }Service 示例 import org.springframework.stereotype.Service;Service public class UserService {// 业务逻辑相关代码 }Controller 示例 import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping;Controller public class HomeController {GetMapping(/home)public String homePage() {return index;} }RestController 示例 import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;RestController public class ApiController {GetMapping(/api/greet)public String greet() {return Hello, world!;} }在这些示例中每个注解都被用于不同的类类型以表明它们的角色和用途。它们在 Spring 框架中自动被扫描和管理从而简化了组件的创建和管理过程。 配置包扫描路径 在上面我们介绍了类注解但是想要将对象成功的存储到 Spring 中我们还需要配置一下存储对象的扫描包路径只有配置的包下的所有类添加了注解才能被正确的识别并保存到 Spring 中。即使添加了注解如果不是在配置的扫描包下的类对象也是不能被存储到 Spring 中的。 在 spring-config.xml 中添加如下配置有关配置文件的内容请参考spring创建与使用 除了增加xmlns:context外还需要在xsi:schemaLocation增加spring-context.xsd描述然后添加注册扫描的包 ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:contexthttp://www.springframework.org/schema/contextxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://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.xsd context:component-scan base-packagecom.fyd//beans可以把这个配置作为模板下次直接复制粘贴过去就好了 读取bean的示例 UserController类 package com.fyd;import org.springframework.stereotype.Controller;Controller public class UserController {public void sayHello(String name) {System.out.println(Hello name);} }启动类 import com.fyd.UserController; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class App {public static void main(String[] args) {// 1. 得到 Spring 的上下文对象ApplicationContext context new ClassPathXmlApplicationContext(spring-config.xml);// 2. 从上下文对象中得到需要的 beanUserController userController (UserController) context.getBean(userController);// 3. 使用对象userController.sayHello(fyd);} }方法注解 Bean 类注解是添加到某个类上的⽽⽅法注解是放到某个⽅法上的。方法注解Bean要配合类注解才能将对象正常的存储到Spring容器中如以下代码的实现 存对象 package com.fyd;import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component;Component public class Users {Beanpublic User user1() {User user new User();user.setName(fyd);user.setAge(18);return user;} }使用对象 import com.fyd.Users; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class App {public static void main(String[] args) {ApplicationContext context new ClassPathXmlApplicationContext(spring-config.xml);Users users (Users) context.getBean(users);System.out.println(users.user1().getName());} }执行效果 Bean 命名规则 通常我们 bean 使⽤的都是标准的⼤驼峰命名⽽读取的时候⾸字⺟⼩写就可以获取到 bean 了如下图所示 当我们⾸字⺟和第⼆个字⺟都是⼤写时就不能正常读取到 bean 了如下图所示 我们找到Bean命名规则的源码 翻译过来就是 实用程序方法用于将字符串转换为正常的 Java 变量名大小写。这通常意味着将第一个字符从大写字母转换为小写字母但在有多个字符且第一个和第二个字符都是大写字母的不常见的特殊情况下我们不对其进行转换。 因此FooBah 变成了 “fooBah”X 变成了 “x”但 URL 仍然是 “URL”。 参数 name - 要去大写的字符串。 返回 返回 返回 返回值值值值 字符串的去首字母版本。 我们把读取的第一个字母变成大写,就可以了 重命名 Bean 我们可以通过设置name属性给Bean对象进行重命名操作: 这里的name属性实际上是一个数组一个bean可以有多个名字。 Component public class Users {Bean(name {user1,fyd})public User user1() {User user new User();user.setName(fyd);user.setAge(18);return user;} }name{}可以省略 Component public class Users {Bean({user1,fyd})public User user1() {User user new User();user.setName(fyd);user.setAge(18);return user;} }
http://www.pierceye.com/news/962935/

相关文章:

  • 做静态头像网站网站做百度竞价利于百度优化
  • 网站建设属于税收建立网站后怎样收费
  • 婚礼礼网站如何做的云南推广公司
  • 模板建站流程seo优化推广
  • 龙岗网络推广深圳网站建设我的世界的头怎么做视频网站
  • 高明网站建设首选公司深圳市建设安监站网站
  • 宁波网站建设科技有限公司注册开发公司
  • 什么网站有女人跟狗做的和平东路网站建设
  • 绍兴手机网站建设wordpress 文字排版
  • 宁波网站设计公司有几家企业网站建设计划书
  • 做微信小程序和网站那个简单给周杰伦做网站
  • 营销型网站建设题库网站制作里面链接怎么做
  • 做网站空间 阿里云h5下一页
  • 怎样才能在百度搜索到自己的网站网站建设制作要学什么
  • 北京网站推广排名外包河南省工程建设业协会网站
  • 桂林市电力建设公司网站野望王绩翻译
  • 网站模版免费网片生产厂家
  • 实用网站设计步骤百度竞价广告代理
  • 怎么在vk网站上做推广网站建设柚子网络科技官网
  • 威海网站优化公司wordpress post title
  • 网站建设验收期安阳后营吧
  • 询盘网站培训机构前端开发
  • 企业如何做网站建站小程序定制开发深圳
  • 创建网站怎么赚钱的视频博客主题wordpress
  • 北京大兴区网站建设如何打造平台
  • 建设公司网站需要多少天棋盘游戏类网站开发
  • 织梦网站logo修改探测器 东莞网站建设
  • 图片网站收录淮北网站建设求职简历
  • 北京建设局投诉网站首页晋江外贸网站建设
  • 如何更改网站模板网站建设这一行业怎样