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);
}
}

最新文章

  1. jdk8中java.util.concurrent包分析
  2. PHP_Bibel阅读学习(一)——看书看经典,写文写代码
  3. android_Activity之Button_OnClickListener
  4. 利用在线工具根据JSon数据自动生成对应的Java实体类
  5. Oracle EBS环境下查找数据源(OAF篇)
  6. 同一台Windows机器中启动多个Memcached服务
  7. window下面配置sftp
  8. MMORPG中的相机跟随算法
  9. Java高并发--原子性可见性有序性
  10. js 手动插入meta标签和script标签
  11. *args和**kwargs的区别
  12. Mysql数据库账户权限设置
  13. TED_Topic7:How we unearthed the spinosaurus
  14. 模块化开发之butterknife 在 library中使用
  15. [leetcode trie]211. Add and Search Word - Data structure design
  16. Mac下删除安装的pkg
  17. 正则表达式—RegEx(RegularExpressio)(一)
  18. selenium webdriver API详解(二)
  19. 【BZOJ 2121】字符串游戏
  20. $ 一步一步学Matlab(3)——Matlab中的数据类型

热门文章

  1. 做自动化测试选择Python还是Java?
  2. 巧用KingbaseES中的动态DDL
  3. [python]-random模块-手动随机数
  4. 命令行配置Windows高级防火墙
  5. 万星开源项目强势回归「GitHub 热点速览 v.22.38」
  6. Django 连接数据库 MySQL
  7. Docker与Containerd使用区别
  8. 几篇关于MySQL数据同步到Elasticsearch的文章---第四篇:使用go-mysql-elasticsearch同步mysql数据库信息到ElasticSearch
  9. scss的使用方法
  10. 媒介查询兼容各种端口的响应式范围取值(移动端、PC端、ipad、移动端侧屏)