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

网站建设服务 杭州餐厅vi设计公司

网站建设服务 杭州,餐厅vi设计公司,互助资金盘网站开发,虚拟主机做网站文章目录 1. 什么是Bean#xff0c;如何配置2. 如何配置bean2.1 使用注解Bean2.2 使用注解Import 1. 什么是Bean#xff0c;如何配置 被spring容器所管理的对象被称为bean#xff0c;管理方式可以有纯xml文件方式、注解方式进行管理(比如注解Component)。 在Spring Boot中如何配置2. 如何配置bean2.1 使用注解Bean2.2 使用注解Import 1. 什么是Bean如何配置 被spring容器所管理的对象被称为bean管理方式可以有纯xml文件方式、注解方式进行管理(比如注解Component)。 在Spring Boot中在注解 Component 的基础上衍生出注解 Service(专门用于处理业务类的注解)、Repository(专门用于处理数据访问的注解)。 2. 如何配置bean 2.1 使用注解Bean 在一个配置类上定义一个方法返回值为一个对象的实例化在这个方法上添加注解Bean如下 package com.lize.demo.dao;public class UserDao {public void printUserDao(){System.out.println(UserDao);} }package com.lize.demo.config;import com.lize.demo.dao.UserDao; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;Configuration public class SpringConnfig {Beanpublic UserDao getUserDao(){return new UserDao();} }单元测试类如下 package com.lize.demo;import com.lize.demo.dao.UserDao; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;SpringBootTest class DemoApplicationTests {Autowiredprivate UserDao ud;Testvoid contextLoads() {ud.printUserDao();} }通过Bean这种方式定义Bean相比于直接在类上添加注解component定义Bean。前者返回的是一个实例化对象可以在这个过程中设置一些参数初始化Bean。 2.2 使用注解Import 需要写在类上标记的类必须是一个bean否则不会起作用 Component Import(UserDao.class) public class SpringConnfig {}上述只是基础写法。另外可以实现ImportSelector这个接口并重写其方法selectImports这个方法返回的是一个字符串数组字符串数组的值为类的完整路径进行批量注入Bean如下 package com.lize.demo.config;import org.springframework.context.annotation.ImportSelector; import org.springframework.core.type.AnnotationMetadata;public class MyImportSelector implements ImportSelector {Overridepublic String[] selectImports(AnnotationMetadata importingClassMetadata) {return new String[]{com.lize.demo.dao.UserDao};// 返回字符串数组} }Component Import(MyImportSelector.class) public class SpringConnfig {}还有一种写法为实现ImportBeanDefinitionRegistrar这个接口并重写其方法registerBeanDefinitions如下 package com.lize.demo.config;import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanNameGenerator; import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; import org.springframework.core.type.AnnotationMetadata;public class MyImportBeanDefinitionRegister implements ImportBeanDefinitionRegistrar {Overridepublic void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry, BeanNameGenerator importBeanNameGenerator) {RootBeanDefinition definition new RootBeanDefinition();definition.setBeanClassName(com.lize.demo.dao.UserDao);registry.registerBeanDefinition(UserDao,definition);} } package com.lize.demo.config;import com.lize.demo.dao.UserDao; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.stereotype.Component;Component Import(MyImportBeanDefinitionRegister.class) public class SpringConnfig {}
http://www.hkea.cn/news/14311794/

相关文章:

  • 龙岩建网站搭建好ftp服务器 如何通过网站访问
  • 上传文章的网站app商城开发价格
  • 遂宁市建设局网站青海西宁高端网站建设
  • 专门做房产的网站做行业网站赚钱
  • 帮人做网站犯法wordpress页面透明度
  • 小公司网络组建规划seo服务顾问
  • 定制旅游网站建设方案网站搭建中114514
  • 网站切换中英文长沙房地产开发商排名
  • 外贸网站测速辽宁网站推广的目的
  • 网站建设的运营计划婚纱网站设计素材
  • 书城网站建设项目定义绍兴网站建设方案
  • app开发哪家公司比较专业沈阳专业seo关键词优化
  • 秦皇岛营销式网站微信公众号发文章教程
  • 网易云课堂的网站建设特点海南住房和城乡建设部网站
  • 做网站永久人力外包和劳务派遣哪个更好一些
  • 上海网站推广策划给网站底部做友情链接
  • 所得税 网站建设费中国建设银行网站宁波
  • 成都私人网站制作北京数字智慧展厅设计装修服务商
  • .net 大型网站开发顺德网站
  • 网站建设计划书兰州seo关键词优化
  • 马鞍山网站建设与制作竞价推广的本质是什么
  • yu网站建设西安建筑网站
  • 素材网站上的元素是怎么做的慈溪 网站建设
  • 网站建设前期开发东莞服饰网站建设哪家好
  • 商城网站建设框架赤峰建网站
  • 山东企业建站系统费用免费咨询法律顾问
  • 数据库网站开发工具印后设备网站建设
  • python做笔记的网站wordpress 最简单模板下载
  • 龙华网站建设方案表网站建设方案 流程
  • 延吉省住房和城乡建设厅网站怎么找人做淘宝网站