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

网站制作价格多少钱长沙php网站建设

网站制作价格多少钱,长沙php网站建设,江门seo,大连新图闻网站设计文章目录 1. 什么是Bean,如何配置2. 如何配置bean2.1 使用注解Bean2.2 使用注解Import 1. 什么是Bean,如何配置 被spring容器所管理的对象被称为bean,管理方式可以有纯xml文件方式、注解方式进行管理(比如注解Component)。 在Spring Boot中&…

文章目录

    • 1. 什么是Bean,如何配置
    • 2. 如何配置bean
      • 2.1 使用注解@Bean
      • 2.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.yayakq.cn/news/941568/

相关文章:

  • 专业的网站公司到哪里找安卓手机搭建网页
  • 仙游住房与城乡建设局网站东莞百度快照优化排名
  • 怎么修改自己公司网站客户关系管理系统功能
  • 深圳营销型网站建网站建设类文章要发多少片
  • 网站建设教论坛诸暨做网站广告的电话
  • 百度首页网站推广多少钱一年wordpress点赞插件
  • 找出网站所有死链接wordpress get cat id
  • 网站301是什么网站效果检测
  • 做旅游计划的网站wordpress静态cdn
  • 福州公司网站建设一定要用主流程序php语言网站模块如何添加
  • 建站方案书怎么免费制作企业网站
  • 网站开发流程图和介绍自建网站需要什么手续
  • 怎么建自己的摄影网站网站使用特殊字体
  • 商城小程序开发报价平原县网站seo优化排名
  • 如何写代码做网站6网站建设流程分为哪几个阶段
  • 福州企业网站企业网站建设电话
  • 杭州行业网站建设wordpress不能添加文章
  • 精通网站建设 100浦东做营销网站
  • 小米网站建设书中国建设银行网站首页下载
  • 禄丰县住房和城乡建设局网站网络行业有哪些
  • 织梦教育网站开发建站公司网站论坛
  • 网站优化应该怎么做如何获取免费域名
  • 制作网站的代码网站开发需要的所有技术
  • 网站如何提高用户量网站的优点有哪些
  • 合肥网络公司seo建站雄安移动网络
  • 通过高权重网站做长尾关键词qq空间 同步 wordpress
  • 专业做网站有哪些WordPress连接符
  • 韩国美容网站 模板免费的室内装修设计软件
  • 建网站需要什么东西郑州经济技术开发区协同办公系统
  • 网站设计 站云建站推荐