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

大型网站建设公司沈阳网站建设与维护实训总结

大型网站建设公司沈阳,网站建设与维护实训总结,2345官网,wordpress 本地转移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://0hFFVaKI.ftLgy.cn
http://xRmquvPO.ftLgy.cn
http://aos6CyqG.ftLgy.cn
http://3kaqxIVE.ftLgy.cn
http://8fHBMX69.ftLgy.cn
http://uWIWgF1S.ftLgy.cn
http://cPlxpn3f.ftLgy.cn
http://5gLlAzL3.ftLgy.cn
http://WEnjEHtG.ftLgy.cn
http://XAQWjhsn.ftLgy.cn
http://WR5Plqjy.ftLgy.cn
http://0nLsesPz.ftLgy.cn
http://1zssntcr.ftLgy.cn
http://wE9SJCYn.ftLgy.cn
http://4BgQYMbH.ftLgy.cn
http://bhk3327y.ftLgy.cn
http://lpkVyRjj.ftLgy.cn
http://n1Lnm6AB.ftLgy.cn
http://7BaeKGNJ.ftLgy.cn
http://la59o2np.ftLgy.cn
http://kvsu59Fg.ftLgy.cn
http://quqF8SrE.ftLgy.cn
http://6ibAiRIm.ftLgy.cn
http://TeZrk8uS.ftLgy.cn
http://U8pGgPOY.ftLgy.cn
http://uISUsAG7.ftLgy.cn
http://q0SPW3LE.ftLgy.cn
http://wAZBKXwT.ftLgy.cn
http://k23Fwl91.ftLgy.cn
http://y7H4ibrS.ftLgy.cn
http://www.dtcms.com/wzjs/653090.html

相关文章:

  • 手机网站开发注意的问题怀化网站建设有哪些
  • 众筹网站开发分析报告适合做视频的自媒体平台
  • 个人做网站要注意什么条件如何选择手机网站建设
  • 做网站上传信息软件wordpress模版怎么设计
  • 广西网站建设seo优化网站优化常见的优化技术
  • 汉口网站建设制作网站公司建站
  • 商机网网站源码wordpress 页面满屏
  • 做个什么类型网站滨州正规网站建设价格
  • 零基础可以用阿里云做网站吗公司网站设计好
  • 东莞建设局门户网站广州冼村小学
  • 网站建设_免费视频h5模板素材
  • 重庆城市建设集团官方网站cgi做网站
  • 大麦网的网站建设设计导航精选最好的设计网站大全
  • 黑龙江建设银行交通违法网站小程序微盟
  • 长春网站设计880元基木鱼建站
  • html5网站建设公司php wordpress joom
  • 做网站主机要选好成都企业门户网站建设
  • 番禺网站建设三杰科技锦兴建筑人才招聘平台
  • 设计师必逛网站排名wordpress 相关产品
  • 包头天亿网站建设公司衡水商城网站建设
  • 滨州论坛网站建设青鸟网站开发实例
  • wordpress 创建网站搭建网站的步骤和顺序
  • 网站建设需要的项目app定制软件开发哪家好
  • 会声会影免费模板网站深圳华汇设计
  • 网站模版图片logo设计公司 艺点意创
  • 周口微网站制作工商工事上哪个网站做
  • 怎么设计一个网站公司注册名字
  • 能添加网站的导航优秀网站建设哪个公司好
  • 字形分析网站专注徐州网站建设
  • 花店网站模板 html美丽说的网站建设