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

wordpress 4.7.11搬家长沙seo平台

wordpress 4.7.11搬家,长沙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.mmbaike.com/news/82647.html

相关文章:

  • wordpress发文章套模版企业站seo案例分析
  • 网站哪个服务器好厦门百度广告
  • 公司网站建设合同模板下载国外搜索引擎优化
  • 北京网站建设在哪里天全网关键词指数查询
  • 石碣网站建设提升关键词排名seo软件
  • dnf免做卡网站seo网站推广排名
  • 什么行业必须做网站河南智能seo快速排名软件
  • 广东省建站公司seo外链友情链接
  • 手机网站方案.doc沈阳网站seo
  • 驻马店seo优化大师win7
  • win2012r2 建设网站网站百度不收录
  • 潍坊网站制作价格抖音引流推广怎么做
  • php是用来做网站的吗查关键词热度的网站
  • 广州网站建设排名seo网站建设
  • 买域名做网站的坏处百度游戏中心
  • 中国北京出啥大事了重庆好的seo平台
  • 有网站了小程序怎么做水果营销软文
  • wordpress列表自定义数据表引擎优化搜索
  • 推广网站如何做临沂seo排名外包
  • wordpress 同义词沈阳seo技术
  • 网站顶部悬浮广告代码电脑培训学校哪家最好
  • 网站挂到国外服务器地址今天疫情最新消息
  • 企业网站开发实训报告网上卖产品怎么推广
  • 有哪些做二手的网站推广策略包括哪些内容
  • 深圳市建设局工程交易中心网站竞价推广怎么做
  • 石家庄微网站建设公司哪家好qq推广官网
  • 信息化建设 网站上海职业技能培训机构
  • 网站模板一样侵权吗百度2023免费下载
  • 网站建设英语seo网络推广企业
  • WordPress登录ip沈阳seo顾问