- 首页 > 生活 > >
SpringBoot 01: JavaConfig + @ImportResource + @PropertyResource
【SpringBoot 01: JavaConfig + @ImportResource + @PropertyResource】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.*;@Configurationpublic class SpringConfig {@Beanpublic 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 {@Testpublic 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="其他配置文件"/>
代码实现
package com.example.springboot.configuration;import org.springframework.context.annotation.*;@ImportResource(value = "https://www.huyubaike.com/biancheng/classpath:applicationContext.xml")public class SpringConfig {}
<?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="https://www.huyubaike.com/biancheng/0010"/><property name="catName" value="https://www.huyubaike.com/biancheng/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 {@Testpublic 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="https://www.huyubaike.com/biancheng/${key}")
需要用的其他注解
- @Component:用在实体类上
经验总结扩展阅读
-
-
活了一把年纪了|婚礼上,我遇到了妻子以前的恩客,才知妻子之前的经历我该原谅吗
-
-
属兔人2022年9月健康运势怎么样 属兔人2022年9月运势完整版
-
拉玛十世|情断慕尼黑,拉玛十世已逾半年未回德国有原因,他并非要改过自新
-
-
女神“不老女神”林志玲,46岁依然肤白貌美,她的养生秘籍终于公开了
-
-
-
-
-
秦始皇|霍金去世前曾称“秦始皇还活着,只是走不出地宫”,这事是真的?
-
-
每个人心里都有一个关于爱情的梦 频率相同的人,才是往后路上的最佳伴侣
-
-
-
发票自助领用机怎么用,自助发票机必须本人去买发票吗
-
美容 人人都能驾驭的5款短发造型,随手一拨都是气质感
-
世界首个唾液孕检试剂盒即将上市 唾液孕检试剂盒怎么使用准确率高吗
-
何药师健康科普|长春胺和甲钴胺有什么区别?保护脑细胞和神经哪个更好?药师告诉您