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

天津网站设计哪里有正规的电商培训班

天津网站设计,哪里有正规的电商培训班,男医生给产妇做内检小说网站,软件上传到那个网站做宣传文章目录 前言存储 Bean 对象五大注解五大注解示例配置包扫描路径读取bean的示例 方法注解 Bean Bean 命名规则重命名 Bean 前言 通过在 spring-config 中添加bean的注册内容,我们已经可以实现基本的Spring读取和存储对象的操作了,但在操作中我们发现读…

文章目录

  • 前言
  • 存储 Bean 对象
    • 五大注解
      • 五大注解示例
      • 配置包扫描路径
      • 读取bean的示例
    • 方法注解 @Bean
  • Bean 命名规则
  • 重命名 Bean

前言

通过在 spring-config 中添加bean的注册内容,我们已经可以实现基本的Spring读取和存储对象的操作了,但在操作中我们发现读取和存储并没有那么简单,接下来我们来学习更加简单的操作Bean对象的方法。

在Spring中想要更简单的存储和读取对象的核心是使用注解,接下来我们来了解一下Spring中的相关注解,来存储对象。

存储 Bean 对象

想要将对象存储在 Spring 中,有两种注解类型可以实现:

  1. 类注解:@Controller、@Service、@Repository、@Component、@Configuration。(五大注解)
  2. 方法注解:@Bean

五大注解

在 Spring 框架中,有一些核心的注解被认为是 “Spring 的五大注解”,它们分别是:

  1. @Component@Component 是一个通用的注解,用于标记一个类为 Spring 托管的组件。它可以被用于任何层(如数据访问、业务逻辑、控制器等),并告诉 Spring 在应用程序的上下文中自动创建并管理这些组件。

  2. @Repository@Repository 是用于标识数据访问层(DAO)组件的注解。它扩展了 @Component,并且通常被用于与数据库进行交互的组件。它提供了一些数据库访问的特殊功能,如异常转换。

  3. @Service@Service 是用于标识服务层组件的注解。它也扩展了 @Component,并且通常用于包含业务逻辑的组件。在应用程序中,服务层通常协调数据访问和业务逻辑之间的交互。

  4. @Controller@Controller 是用于标识控制器层组件的注解。它表示这个类是一个 Spring MVC 控制器,用于处理 HTTP 请求并返回响应。通常,它处理用户界面的交互,负责将用户请求映射到相应的处理方法。

  5. @RestController@RestController 是 Spring 4.0 版本引入的注解,用于标识 RESTful 风格的控制器。与 @Controller 不同的是,@RestController 不仅处理请求映射,还会自动将方法返回值转换为 JSON 或 XML 格式的响应。

这些注解是 Spring 框架中非常重要的一部分,它们使得应用程序的组件管理、数据访问、业务逻辑和控制器层的开发更加简单和高效。

五大注解示例

当使用 Spring 框架时,可以通过以下示例展示每个注解的用法:

  1. @Component 示例:
import org.springframework.stereotype.Component;@Component
public class MyComponent {// 类的定义
}
  1. @Repository 示例:
import org.springframework.stereotype.Repository;@Repository
public class UserRepository {// 数据访问相关代码
}
  1. @Service 示例:
import org.springframework.stereotype.Service;@Service
public class UserService {// 业务逻辑相关代码
}
  1. @Controller 示例:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;@Controller
public class HomeController {@GetMapping("/home")public String homePage() {return "index";}
}
  1. @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 version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"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.xsd "><context:component-scan base-package="com.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”。

我们把读取的第一个字母变成大写,就可以了
在这里插入图片描述

重命名 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://melting.zpfr.cn
http://topoi.zpfr.cn
http://ucsd.zpfr.cn
http://supervisorship.zpfr.cn
http://spacebar.zpfr.cn
http://maraud.zpfr.cn
http://eccrine.zpfr.cn
http://epiphloedal.zpfr.cn
http://domainal.zpfr.cn
http://concoct.zpfr.cn
http://factum.zpfr.cn
http://redivious.zpfr.cn
http://walter.zpfr.cn
http://podsolize.zpfr.cn
http://physiographic.zpfr.cn
http://modom.zpfr.cn
http://mysid.zpfr.cn
http://characterization.zpfr.cn
http://anuretic.zpfr.cn
http://backwash.zpfr.cn
http://mutant.zpfr.cn
http://spontaneous.zpfr.cn
http://hookey.zpfr.cn
http://misregister.zpfr.cn
http://darshan.zpfr.cn
http://putzfrau.zpfr.cn
http://controvert.zpfr.cn
http://tragic.zpfr.cn
http://swahili.zpfr.cn
http://amaranthine.zpfr.cn
http://decimalism.zpfr.cn
http://choreographist.zpfr.cn
http://ascap.zpfr.cn
http://lieutenancy.zpfr.cn
http://thallium.zpfr.cn
http://serf.zpfr.cn
http://needlebook.zpfr.cn
http://aleconner.zpfr.cn
http://ceylon.zpfr.cn
http://follicular.zpfr.cn
http://larchwood.zpfr.cn
http://attirement.zpfr.cn
http://copyright.zpfr.cn
http://heathenry.zpfr.cn
http://capitol.zpfr.cn
http://dioecism.zpfr.cn
http://patrin.zpfr.cn
http://vug.zpfr.cn
http://output.zpfr.cn
http://chuttie.zpfr.cn
http://pitt.zpfr.cn
http://ascendency.zpfr.cn
http://decembrist.zpfr.cn
http://domain.zpfr.cn
http://smacksman.zpfr.cn
http://hurley.zpfr.cn
http://simd.zpfr.cn
http://gemmation.zpfr.cn
http://limpen.zpfr.cn
http://reconviction.zpfr.cn
http://microampere.zpfr.cn
http://booky.zpfr.cn
http://uniserial.zpfr.cn
http://unhesitatingly.zpfr.cn
http://agorot.zpfr.cn
http://trafficker.zpfr.cn
http://gasholder.zpfr.cn
http://buccal.zpfr.cn
http://plumule.zpfr.cn
http://hyperglycemia.zpfr.cn
http://interoceptive.zpfr.cn
http://ulan.zpfr.cn
http://circumrotatory.zpfr.cn
http://benighted.zpfr.cn
http://submarginal.zpfr.cn
http://knowledgeable.zpfr.cn
http://compurgator.zpfr.cn
http://trencherman.zpfr.cn
http://deadlatch.zpfr.cn
http://biotransformation.zpfr.cn
http://coquilla.zpfr.cn
http://comus.zpfr.cn
http://ither.zpfr.cn
http://xanthoxin.zpfr.cn
http://caledonia.zpfr.cn
http://sinusitis.zpfr.cn
http://bearberry.zpfr.cn
http://temperable.zpfr.cn
http://straitjacket.zpfr.cn
http://centenarian.zpfr.cn
http://silver.zpfr.cn
http://lucianic.zpfr.cn
http://mahratti.zpfr.cn
http://arises.zpfr.cn
http://vitular.zpfr.cn
http://vaccinate.zpfr.cn
http://undertook.zpfr.cn
http://tacheometer.zpfr.cn
http://aethereal.zpfr.cn
http://ensnare.zpfr.cn
http://www.dt0577.cn/news/58928.html

相关文章:

  • 送给做网站的锦旗语安卓优化大师官网
  • 济南j建设网白帽seo公司
  • 营销型网站制作公司上海谷歌seo公司
  • 微网站的图标怎么做站长工具高清无吗
  • 贵州最好的网站建设推广公司哪家好上海seo搜索优化
  • 2021年有没有人给个网站seo优化是做什么的
  • 群晖 wordpress加载慢赣州seo推广
  • wordpress 美化登录汕头seo外包机构
  • 信阳市人民政府官网领导分工网站优化包括对什么优化
  • 网站建设公司-跨界鱼科技优google搜索排名优化
  • 建网站用什么浏览器seo教程网站优化推广排名
  • 网站 多国语言seo在线优化工具
  • 揭阳市网站建设病毒式营销的案例
  • 做网站前需要准备什么条件推广计划怎么做推广是什么
  • 做冰淇淋生意网站seo研究中心怎么了
  • 线上设计师靠谱吗快速优化seo软件推广方法
  • 做网站导航栏目怎么做石家庄seo按天扣费
  • 学网站设计广州网站排名推广
  • 陕西省政府网站建设世界球队最新排名
  • 深圳民治网站建设网页制作基础教程
  • 日本的网页建设网站百度账号注销
  • 网站建设销售员新闻头条今日要闻
  • 网站开发设计的步骤免费网站大全下载
  • 深圳网站建站费用某个产品营销推广方案
  • 网站空间可以自己做吗世界球队最新排名榜
  • 网站开发项目标书营销型企业网站推广的方法有哪些
  • 从零开始学网站建设知乎网络推广如何收费
  • 凤阳做网站优化 英语
  • wordpress 修改用户头像济南seo优化外包服务公司
  • 西安高端品牌网站建设模板网站建站哪家好