springboot的前置知识:通过注解创建对象和读取配置文件

1. JavaConfig

设计思想

  • 使用java类作为xml配置文件的替代,是配置spring容器的纯java的方式
  • 可以创建java对象并把对象注入到spring容器中

注解实现

  • @Configuration : 放在一个类的上面,表示这个类是作为配置文件使用的
  • @Bean:放在返回值是对象的方法上,容器启动时,声明对象,并把对象注入到容器中
  • 上面两个注解配套使用

代码实现

package com.example.springboot.configuration;

import com.example.springboot.model.Student;
import org.springframework.context.annotation.*; @Configuration
public class SpringConfig {
@Bean
public Student getStudent(){
Student student = new Student();
student.setName("橘子");
student.setAge(18);
return student;
} @Bean(name = "student")
public Student getStudentByBeanName(){
Student student = new Student();
student.setName("饺子");
student.setAge(21);
return student;
}
}

测试代码

package com.example.springboot.testspringconfig;

import com.example.springboot.configuration.SpringConfig;
import com.example.springboot.model.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class TestSpringConfig {
@Test
public void testSpringConfig(){
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
Student student = (Student) applicationContext.getBean("student");
//未在@Bean中指定对象名称时,从方法名(小驼峰命名规范)来获取对象
//Student student = (Student) applicationContext.getBean("getStudent");
System.out.println("获取到的对象: " + student);
}
}

2. @ImportResource

设计思想

  • 导入其他的xml配置文件, 等于在xml 使用如下import标签
<import resources="其他配置文件"/>

代码实现

  • SpringConfig类
package com.example.springboot.configuration;

import org.springframework.context.annotation.*;

@ImportResource(value = "classpath:applicationContext.xml")
public class SpringConfig { }
  • applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="cat" class="com.example.springboot.model.Cat">
<property name="catCard" value="0010"/>
<property name="catName" value="tomcat"/>
</bean>
</beans>

测试代码

package com.example.springboot.testspringconfig;

import com.example.springboot.configuration.SpringConfig;
import com.example.springboot.model.Cat;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class TestSpringConfig {
@Test
public void testImportResource(){
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
Cat cat = (Cat) applicationContext.getBean("cat");
System.out.println("获取到的对象: " + cat);
}
}

3. @PropertyResource

设计思想

  • 读取properties属性配置文件,使用属性配置文件可以实现外部化配置

使用步骤

  • 在resources目录下,创建properties文件, 使用 key=value 的格式提供数据
  • 在@PropertyResource 指定properties文件的位置
  • 使用在待注入值的变量上使用@Value(value="${key}")

需要用的其他注解

  • @Component:用在实体类上
  • @ComponentScan:SpringConfig类上
  • @Value:待注入值的属性上

代码实现

  • SpringConfig类
package com.example.springboot.configuration;

import com.example.springboot.model.Student;
import org.springframework.context.annotation.*; @PropertySource(value = "classpath:food.properties")
@ComponentScan(basePackages = "com.example.springboot.model")
public class SpringConfig {
}
  • food.properties
food.name=饺子
food.price=13
  • JiaoZi类
package com.example.springboot.model;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; @Component("jiaozi")
public class JiaoZi { @Value("${food.name}")
private String name;
@Value("${food.price}")
private double price; @Override
public String toString() {
return "JiaoZi{" +
"name='" + name + '\'' +
", price=" + price +
'}';
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public double getPrice() {
return price;
} public void setPrice(double price) {
this.price = price;
} public JiaoZi(String name, double price) {
this.name = name;
this.price = price;
} public JiaoZi() {
}
}

测试代码

package com.example.springboot.testspringconfig;

import com.example.springboot.configuration.SpringConfig;
import com.example.springboot.model.JiaoZi;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class TestSpringConfig { @Test
public void testPropertiesSource(){
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
JiaoZi jiaoZi = (JiaoZi) applicationContext.getBean("jiaozi");
System.out.println("food: " + jiaoZi);
}
}

SpringBoot 01: JavaConfig + @ImportResource + @PropertyResource的更多相关文章

  1. SpringBoot开发使用@ImportResource注解影响拦截器

    问题描述 今天在给SpringBoot项目配置拦截器的时候发现怎么都进不到拦截器的方法里面,在搜索引擎上看了无数篇关于配置拦截器的文章都没有找到解决方案. 就在我准备放弃的时候,在 CSDN 上发现了 ...

  2. SpringBoot 01 概述

    官方文档 https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/ 简介 SpringBoot 是一个 JavaWeb ...

  3. SpringBoot 01 hello world 01

    hello world项目结构: pom中配置的依赖相当于spring boot的可安装插件,需要下载的依赖直接在里边配置. 目前用到的每个注解: 1.主程序中 @SpringBootApplicat ...

  4. SpringBoot系列之Spring容器添加组件方式

    SpringBoot系列之Spring容器添加组件方式 本博客介绍SpringBoot项目中将组件添加到Spring容器中的方法,SpringBoot项目有一个很明显的优点,就是不需要再编写xml配置 ...

  5. MyBatis原理,Spring、SpringBoot整合MyBatis

    1. MyBatis概述 MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可 ...

  6. 【springboot】知识点总结

    [springboot 基础编] 01.SpringBoot>01 - 第一个应用–HelloWorld 02.SpringBoot>02 - 整合 MyBatis 03.SpringBo ...

  7. 编程从入门到放弃(Java)

      1.Java入门篇 1.1 基础入门和面向对象 1.1.1 编程基础 [01] Java语言的基本认识 [02] 类和对象 [03] 类的结构和创建对象 [04] 包和访问权限修饰符 [05] 利 ...

  8. Spring in Action 4th 学习笔记

    约定: 一.@Xxx Class 表示被@Xxx注解的类.同理还有@Xxx注解的字段或方法. 例如:@Bean Method. 二.@Component Class 同时代指 @Controller. ...

  9. Spring Boot2(001):入门介绍和一些官网链接参考

    Spring官方文档比较齐全,学习的过程中可以多参考官方文档,最权威的版本.01.Spring Boot的一些官方链接 01.01 Spring Boot官网 https://spring.io/pr ...

随机推荐

  1. JCEF 初体验,window系统构建jar包

    前言 本文记录如何通过jcef源代码去构建自己所需要的jar包,此文章构建的为windows64位jcef 的 jar 包,若需要构建 32 位的 jar 包,则需要按照文章将相关准备软件设置为 32 ...

  2. [CF1537E] Erase and Extend (字符串)

    题面 给一个长度为 n \tt n n 的字符串,你可以进行无限次以下两种操作之一: 删去末尾的字符(此时要保证删去后字符串非空). 把当前整个字符串复制一份,接到自己的后面. 输出最终通过操作能达到 ...

  3. 【Matlab】学习记录1-简单的函数介绍

    sind(30) %正弦函数,以角度为单位  ans =0.5000 exp(2) %以e为底的指数函数,即e^x   ans =7.3891 log10(10)  ans =1log(exp(1)) ...

  4. Configuration的学习

    创建 //1.创建,调用的空惨 Configuration conf = new Configuration(); 加载主配置 //2.读取主配置文件==>如果是空参方法则自动加载sec下的re ...

  5. C++ 初识函数模板

    1. 前言 什么是函数模板? 理解什么是函数模板,须先搞清楚为什么需要函数模板. 如果现在有一个需求,要求编写一个求 2 个数字中最小数字的函数,这 2 个数字可以是 int类型,可以是 float ...

  6. 【ASP.NET Core】自定义Session的存储方式

    在开始今天的表演之前,老周先跟大伙伴们说一句:"中秋节快乐". 今天咱们来聊一下如何自己动手,实现会话(Session)的存储方式.默认是存放在分布式内存中.由于HTTP消息是无状 ...

  7. 后端程序员实现一个IP归属地的小程序

    在日常开发中,后端主要提供数据以及处理业务逻辑,前端主要提供页面布局以及数据展示.后端程序员对于页面布局接触比较少,但是小程序有完善的文档说明.页面布局也相对简单,实现起来相对简单一些.而且小程序相对 ...

  8. 事件循环:微任务和宏任务在v8中实现的简单理解

    微任务 在js中,当使用promise,会将当前任务加入事件执行的微任务队列,有且只有这一种方法可以,因为当使用了promise,在JS引擎中会触发VM::queueMicrotask,会向m_mic ...

  9. Optional源码解析与实践

    1 导读 NullPointerException在开发过程中经常遇到,稍有不慎小BUG就出现了,如果避免这个问题呢,Optional就是专门解决这个问题的类,那么Optional如何使用呢?让我们一 ...

  10. 修改-Python函数-2

    一.导入 $$f ( x , y ) = 2 x + 3 y$$ 上面括号里面的就是数学公式里的自变量,自变量就相当于函数里的参数. 二.为什么要有参数 如果一个大楼里有两种尺寸不一的窗户,显然在没有 ...