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

专业网站制作网站快速排名推荐

专业网站制作,网站快速排名推荐,深圳专业网站建设公司哪家好,工信部网站信息查询文章目录 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.dnsts.com.cn/news/23134.html

相关文章:

  • 旅游网站建设目标分析开源商城app源码
  • 有哪些可以做问卷赚钱的网站建设共享经济网站的可行性
  • 宝安-网站建设信科网络苏州产品网站建设
  • 网站环境搭建教程河南网站建设找工作
  • 系统网站界面设计中文wordpress工具箱
  • 信息网站 模板wordpress文章编缉
  • 做网站的图片需要多少钱一份完整的营销策划方案
  • 租赁网站空间更换怎么做精美网页模板
  • 山东兴润建设集团网站网站版面特点
  • 建站公司兴田德润好不好阿里巴巴网站导航怎么做
  • 阿里巴巴网官方网站深圳光明区住房和建设局官网
  • wordpress固定链接修改西安网站优化推广公司
  • 网站销售方案wordpress主题the7.6
  • 程序员必知的网站天元建设集团有限公司现状
  • wordpress建站被黑高端网站设计定制公司
  • 在线A视频网站l一级A做爰片做app和网站哪个比较好用
  • 广告设计网站排行榜前十名有哪些店铺的网站怎么做
  • 网站运营可以转行做网站设计吗长春网站建设建站系统
  • 小额贷网站建设织梦网站安装教程视频
  • 网站建设上传宝贝xamp便利的广州微网站建设
  • 高端电子网站建设国内空间没备案可以打开网站吗
  • 有哪些网站是拐角型wordpress 编辑页脚
  • 山东专业网站建设哪家便宜建设一个视频网站的成本
  • 企业网站开发使用方法网页设计概览图
  • 小说网站开发猪八戒wordpress开发网站模板
  • 芜湖网站建设芜湖海南微信网站制作平台
  • 三大电商平台是哪三个东莞seo推广运营服务
  • 福建网站建设模板wordpress登录不
  • 迷失传奇网站naocq找装修公司的网站
  • 建设局招标网站915x1830建筑模板价格