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

网站加速器免费玉溪市住房和城乡建设局网站

网站加速器免费,玉溪市住房和城乡建设局网站,个人网站建设流程图,用户登录界面设计Bean 扫描 可以浏览下面的博客链接 :spring 学习 (注解)-CSDN博客 在学习spring 注解时,我们使用 Component ,Service,Controller等 这样的注解,将目标类信息,传递给IOC容器,为其创…

Bean 扫描

可以浏览下面的博客链接 :spring 学习 (注解)-CSDN博客

在学习spring 注解时,我们使用 @Component ,@Service,@Controller等 这样的注解,将目标类信息,传递给IOC容器,为其创建对象


那时候扫描,解析为目标类创建对象的注解 有两者办法

1 spring 配置文件 中 使用<context :component-scan >标签 ,在标签,表明需要扫描哪一个包下的注解,明确 包名。

<context:component-scan base-package="fs">
</context:component-scan>

2 使用注解 扫描

// @Configuration 注解表示当前类是一个配置类,用来代替xml配置文件
@Configuration
// @ComponentScan 注解表示扫描包,用来代替xml配置文件
// basePackages 属性表示扫描的包
@ComponentScan(basePackages ="fs.exerise")

学习 springBoot 时,发现我们同样也需要使用很多注解像@Mapper,@RestController,@RequstMapping等... 


思考

1springboot 是spring 家族中的,按理来说也应该存在 扫描注解的。比如@ComponentScan ?

答:在springboot 中确实存在 扫描 ,解析的注解。只是被隐藏在 @SpringBootApplication 组成注解中

启动类

鼠标点击, @SpringBootApplication 组成注解 按ctrl+b  ,看到 该注解的底层代码


demo(案例)

验证 :使用的注解的可以被扫描的范围是启动类所在包及其子包

  • 启动类所在包在 springboot01 包下

  • 运行截图


修改:移动 controller 包到 heima 包下和 springboot 01包平级

发现:controller 包扫描不到。因此报了一个404 错误 !表示服务器 无法访问


解决办法

扩大 扫描范围,将范围扩展到heima 包下

运行截图


总结

1 在springboot 项目中,我们只管写各自注解 ,至于被使用的注解的扫描,解析过程,将交由springboot 自动完成,不需要我们人为操作。

2 springboot 项目中可以被扫描的范围是 默认是 启动类所在包及其子包


Bean 注册

目的:这里主要研究第三方 jar 对象【非自定义 对象】怎么注入到loc容器

看到这里,我们知道在学习 spring 时使用注解来注册bean 有:

思考

1 把三方jar 包 bean 对象注入到loc容器 使用这些注解 是否可行呢?

举例:打开springboot项目的外部库,任意一个 字节码文件,都是只读的。你无法在该类上使用上面说的 bean 注册的注解。

解决办法

1 @Bean 注解

功能:将目的对象以方法返回值的形式,传递给ioc 容器

@Bean 的使用

1 @Bean 默认方法名就是bean的id 将方法的返回值传递给ioc 容器

demo(案例)

目的:使用@ Bean 注解,将Country 对象传递给spring 容器

项目准备

1  使用 黑马视频教学使用的第三方jar 包 ,通过网盘分享的文件:02_Bean注册资料.rar
链接: https://pan.baidu.com/s/1rYC0mgIp_eWvdD6h2k015Q 提取码: mdpv

2 解压 文件

3 打开 本地仓库文件 修改jar包的所在路径

4 打开命令行在确保maven 环境配置好的情况下,运行该命令

  • 把这个jar 包下载到本地仓库

5 打开自己的本地仓库,可以看到 已经下好的jar包

6 打开IDEA 构建springboot 项目,在pom 文件添加已经下载的jar包坐标,最好加载成功

        <dependency><groupId>cn.itcast</groupId><artifactId>common-pojo</artifactId><version>1.0</version></dependency>

7 使用@ Bean 注解,将Country ,Provice对象传递给spring 容器

正式步骤

1 一个config 包,创建ComonConfig 类作为配置类

package com.it.heima.springboot03.config;import cn.itcast.pojo.Country;
import cn.itcast.pojo.Province;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class ComonConfig {@Beanpublic Country getCountry(){return new Country();}@Beanpublic Province getProvince() {return new Province();}
}

2 启动类中,调用getBean方法,id为默认的方法名

package com.it.heima.springboot03;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;@SpringBootApplication
public class Springboot03Application {public static void main(String[] args) {ApplicationContext run = SpringApplication.run(Springboot03Application.class, args);System.out.println(run.getBean("getCountry"));System.out.println("--------------------------------");System.out.println(run.getBean("getProvince"));}}

3 运行截图

  • 发现 Country 对象,Province 对象已经创建成功


2 @import 注解

使用方式

1 导入配置类

问题: 假设 我们把之前创建的配置类移动到启动类所在包之外,再一次运行代码很显然因为扫描不到配置类,从而无法从ioc容器 获得 对象,而报错

解决办法:在启动类 使用@import 注解,注解中写入 配置类的字节码文件


2 导入ImportSelector 接口实现类

应用场景:如果存在许多 配置类需要导入

  • CommonImportSelector 接口实现类

  • 启动类中使用 @Import 注解 ,注解中使用接口实现类的字节码文件

package com.it.heima.springboot03;import com.it.heima.config.ComonConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Import;//@Import(ComonConfig.class)
@Import(CommonImportSelector.class)
@SpringBootApplication
public class Springboot03Application {public static void main(String[] args) {ApplicationContext run = SpringApplication.run(Springboot03Application.class, args);System.out.println(run.getBean("getCountry"));System.out.println("--------------------------------");System.out.println(run.getBean("getProvince"));}}
  • 运行截图


更新

在ImportSelector 接口实现类 中把配置类的权限定类名存储在 配置文件

package com.it.heima.springboot03;import com.it.heima.config.ComonConfig;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;public class CommonImportSelector implements ImportSelector {@Overridepublic String[] selectImports(AnnotationMetadata importingClassMetadata) {InputStream resourceAsStream = CommonImportSelector.class.getClassLoader().getResourceAsStream("config.properties");BufferedReader in = new BufferedReader(new InputStreamReader(resourceAsStream));String line = null;List<String> list = new ArrayList<>();try {while ((line = in.readLine()) != null) {list.add(line);}} catch (Exception e) {e.printStackTrace();}finally {if (in != null){try {in.close();} catch (Exception e) {e.printStackTrace();}}}return list.toArray(new String[0]);}
}

运行截图


修改启动类中的 @Import(CommonImportSelector.class),把它试图变成 组合注解

  • 如下图所示 allCommon组合注解 具备以下三个注解的所有功能
package com.it.heima.springboot03;import org.springframework.context.annotation.Import;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
// 作用在类上
@Target({ElementType.TYPE})
// 保留到运行时
@Retention(RetentionPolicy.RUNTIME)
@Import(CommonImportSelector.class)
public @interface allCommon {
}

启动类中

运行截图


文章转载自:

http://6wj7hqql.dppfh.cn
http://PV3kgkCG.dppfh.cn
http://h48enbjW.dppfh.cn
http://sCdB4GOW.dppfh.cn
http://gPvHahay.dppfh.cn
http://gFfDsGXa.dppfh.cn
http://lEbDOGJK.dppfh.cn
http://psQ67dUZ.dppfh.cn
http://L6xaot2Z.dppfh.cn
http://uGf2gYnI.dppfh.cn
http://bRLnCbA6.dppfh.cn
http://dmDquUcx.dppfh.cn
http://7FkiyOxS.dppfh.cn
http://cTaCRqlH.dppfh.cn
http://Gbo5qADP.dppfh.cn
http://hNKNirRQ.dppfh.cn
http://JMi6XLwb.dppfh.cn
http://9C0iNKRZ.dppfh.cn
http://O1CFOc4V.dppfh.cn
http://ppa1i8f8.dppfh.cn
http://zzROWsyg.dppfh.cn
http://MVhmMd0Q.dppfh.cn
http://jTiod6t6.dppfh.cn
http://NYxYCgaV.dppfh.cn
http://3P43VQ5P.dppfh.cn
http://TJ1RGNzD.dppfh.cn
http://CeKmG1p6.dppfh.cn
http://ng9X1jHp.dppfh.cn
http://NoQQmvOc.dppfh.cn
http://lfyicAPd.dppfh.cn
http://www.dtcms.com/wzjs/732500.html

相关文章:

  • 如何别人看自己做的网站发来贵州省建设厅网站
  • 苏州网站建设上往建站品牌建设情况汇报
  • 建设网站需要准备什么资料玉树电子商务网站建设哪家快
  • 网站模板 使用深圳seo搜索优化
  • 建设考试的报名网站外贸网站 域名后缀
  • 清远做网站的有哪些wordpress加置顶功能
  • 静态网站开发的目的广州卓天跨境电商网站
  • 濮阳微信网站开发didv 模板WordPress
  • 怎样做一个购物型的网站杭州 网站定制
  • 成都网站制作东三环wordpress自媒体新闻模板
  • 要建设网站郑州seo优化顾问热狗
  • 哈尔滨网站建设策划方案新建的网站百度搜索不到
  • 广州制作网站seo南京网站制作工具
  • 企业网站建设尚未实现宣传功能信用 网站 建设方案
  • 中国建设网站官方网站电商 网站建设文字
  • 建站系统社区天津科技制造有限公司
  • wordpress 三款站群插件之比较全国工业设计大赛官网
  • 网站制作商家入驻本地wordpress 上传到服务器
  • 在哪里创建网站wordpress最大上传2m
  • 自助式网站建设 济南长春网站建设开发的有哪些
  • 仿织梦小说网站源码石龙建设网站
  • 深圳网络公司网站工商营业执照在线查询
  • 商丘网站建设服务做网站需要什么资质
  • 做视频网站盈利模式上线了建站价格
  • 购买一个网站需要多少钱?网络营销策划活动方案
  • 企业建设网站昆明最新新闻事件今天
  • 如何看网站是否有做网站地图数据库端口 wordpress
  • 网站做多久能盈利怎么用图片做网站背景图
  • 营销型网站建设团队建筑工人信息平台
  • 企业网站优化兴田德润优惠网站开发有几个阶段