Spring中使用XML方式导入Spring配置文件,Boot中使用全注解导入Spring配置
目录
Spring中的方法
Spring Boot中的方法
Spring中的方法
@ImportResource:导入Spring的配置文件,让配置文件里面的内容生效;
Spring Boot里面没有Spring的配置文件,自己编写的配置文件,也不能自动识别;
想让Spring的配置文件生效,加载@ImportResource标注配置类
项目结构如下:
程序运行截图如下:
对应的源码为:
源码如下:
Dog.java
package com.sourceinport.demo.bean;public class Dog {private String name;private Integer age;@Overridepublic String toString() {return "Dog{" +"name='" + name + '\'' +", age=" + age +'}';}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}
}
Person.java
package com.sourceinport.demo.bean;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;import java.util.Date;
import java.util.List;
import java.util.Map;@PropertySource(value = {"classpath:person.properties"})
@Component
@ConfigurationProperties(prefix = "person")
public class Person {private String lastName;private Integer age;private Boolean boss;private Date birth;private Map<String, Object> maps;private List<Object> lists;private Dog dog;@Overridepublic String toString() {return "Person{" +"lastName='" + lastName + '\'' +", age=" + age +", boss=" + boss +", birth=" + birth +", maps=" + maps +", lists=" + lists +", dog=" + dog +'}';}public String getLastName() {return lastName;}public void setLastName(String lastName) {this.lastName = lastName;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public Boolean getBoss() {return boss;}public void setBoss(Boolean boss) {this.boss = boss;}public Date getBirth() {return birth;}public void setBirth(Date birth) {this.birth = birth;}public Map<String, Object> getMaps() {return maps;}public void setMaps(Map<String, Object> maps) {this.maps = maps;}public List<Object> getLists() {return lists;}public void setLists(List<Object> lists) {this.lists = lists;}public Dog getDog() {return dog;}public void setDog(Dog dog) {this.dog = dog;}
}
HelloService.java
package com.sourceinport.demo.service;public class HelloService {
}
beans.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 = "helloService" class = "com.sourceinport.demo.service.HelloService"></bean>
</beans>
DemoApplicationTests.java
package com.sourceinport.demo;import com.sourceinport.demo.bean.Person;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {@AutowiredApplicationContext ioc;@Testpublic void testHelloService(){boolean b = ioc.containsBean("helloService");System.out.println(b);}
}
Spring Boot中的方法
Spring Boot中推荐使用给容器中添加组建的方式(全注解方式)
@configuration指明当前类是一个配置类,就是来替代上面的xml文件的。
@Bean:将方法的返回值添加到容器中,容器中这个组建默认的id就是方法名;
程序逻辑结构如下:
运行截图如下:
Dog.java
package com.sourceinport.demo.bean;public class Dog {private String name;private Integer age;@Overridepublic String toString() {return "Dog{" +"name='" + name + '\'' +", age=" + age +'}';}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}
}
Person.java
package com.sourceinport.demo.bean;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;import java.util.Date;
import java.util.List;
import java.util.Map;@PropertySource(value = {"classpath:person.properties"})
@Component
@ConfigurationProperties(prefix = "person")
public class Person {private String lastName;private Integer age;private Boolean boss;private Date birth;private Map<String, Object> maps;private List<Object> lists;private Dog dog;@Overridepublic String toString() {return "Person{" +"lastName='" + lastName + '\'' +", age=" + age +", boss=" + boss +", birth=" + birth +", maps=" + maps +", lists=" + lists +", dog=" + dog +'}';}public String getLastName() {return lastName;}public void setLastName(String lastName) {this.lastName = lastName;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public Boolean getBoss() {return boss;}public void setBoss(Boolean boss) {this.boss = boss;}public Date getBirth() {return birth;}public void setBirth(Date birth) {this.birth = birth;}public Map<String, Object> getMaps() {return maps;}public void setMaps(Map<String, Object> maps) {this.maps = maps;}public List<Object> getLists() {return lists;}public void setLists(List<Object> lists) {this.lists = lists;}public Dog getDog() {return dog;}public void setDog(Dog dog) {this.dog = dog;}
}
MyAppConfig.java
package com.sourceinport.demo.config;import com.sourceinport.demo.service.HelloService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class MyAppConfig {@Beanpublic HelloService helloService(){System.out.println("配置@Bean给容器中添加组建");return new HelloService();}
}
HelloService.java
package com.sourceinport.demo.service;public class HelloService {
}
DemoApplication.java
package com.sourceinport.demo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;@SpringBootApplication
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}}
DemoApplicationTests.java
package com.sourceinport.demo;import com.sourceinport.demo.bean.Person;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {@AutowiredPerson person;@AutowiredApplicationContext ioc;@Testpublic void contextLoads() {System.out.println(person);}@Testpublic void testHelloService(){boolean b = ioc.containsBean("helloService");System.out.println(b);}
}
Spring中使用XML方式导入Spring配置文件,Boot中使用全注解导入Spring配置相关推荐
- Spring——Bean管理-xml方式进行属性注入
目录 一.xml方式创建对象 二.xml方式注入属性 第①种方式注入:set方法注入 第②种方式注入:有参构造函数注入 constructor-arg:通过构造函数注入 用name标签属性: 不按照顺 ...
- Spring Bean、XML方式Bean配置、Bean实例化配置、Bean注入
文章目录 Bean管理 一.SpringBoot Bean 初了解 1.1 了解 1.2 Bean的作用域 1.2.1 注意事项 1.3 第三方Bean 二. 基于XML方式Bean的配置 2.1 S ...
- Spring AOP(三)--XML方式实现
本文介绍通过XML方式实现Spring AOP,在上一篇中已经介绍了通过注解+java配置的方式,这篇文章主要是看XML中怎么配置,直接上代码了: 一.创建一个连接点 1⃣️定义接口 注意⚠️:可以定 ...
- spring boot(7)-mybatis全注解化
关于配置数据库可以参考上一篇文章,这里只讲mybatis pom.xml [html] view plaincopyprint? <!-- 引入mybatis --> <depend ...
- 用java编写xml通讯录实现增_删_改_查实验报告_mybatis中的xml方式增删改查
实体类package cn.liwen.entity;public class User { private long id; //主键 private String username;/ ...
- C#中采用自定义方式读取自定义配置文件
在C#中读取配置文件的时候,我们通常都是通过ConfigurationManager类来直接获取配置文件中的相关数据的,但是如果我们不想读取项目的默认配置文件App.config,则很不爽,所以经过了 ...
- transformer中attention计算方式_Transformer在推荐模型中的应用总结
最近基于transformer的一些NLP模型很火(比如BERT,GPT-2等),因此将transformer模型引入到推荐算法中是近期的一个潮流.transformer比起传统的LSTM.GRU等模 ...
- Word开发工具Aspose.Words功能演示:在C ++中以编程方式在Word文档中添加或删除页眉和页脚
Word文档中的页眉和页脚用于格式化和显示重要信息,例如主题,章节,页码,Copywrite等.以编程方式使用Word文档时,可能需要添加或删除页眉和页脚.为此,本文将教您如何使用C ++在Word文 ...
- 3分钟学会在C ++中以编程方式合并Excel工作表中的单元格
合并和取消合并单元格是Microsoft Excel的一项简单且常用功能.合并单元格可能会在某些情况下很有用,例如,当工作表中有多个列共享相同的标题时,可以合并列上方的单元格以使其具有共同的标题.如果 ...
最新文章
- 2020 年,让 OKR 助力你稳住 flag 清单!
- 5年没有工资收入,他如何支撑世界上最大的免费编程社区?
- 数据结构之快速排序图文详解及代码(C++实现)
- numpy报错:ModuleNotFoundError: No module named 'numpy.core._multiarray_umath'(解决办法:安装更高版本的numpy)
- java 常量池详解
- 如何一次性复制带有markdown/mathjax/latex的博客内容
- JAVA中跨平台分隔符
- 微信接口java解密_微信小程序Java登录流程(ssm实现具体功能和加解密隐私信息问题解决方案)...
- matlab prn文件,PRN格式文件 如何打开PRN文件 PRN是什么格式的文件 用什么打开 - The X 在线工具...
- cadlisp点选面积标注_一个在CAD中标注坐标的LISP
- 计算机考研复试【英语面试题汇总】
- Windows下DOS/bat命令行下载网络文件
- jarvisoj_typo
- 解读用巨象指纹浏览器进行引流的4种操作手法
- Android学习笔记七之ContentProvider
- 使用面向对象思想,输出员工信息并计算员工的工资。定义一个部门(Department)类,该类有部门编号(Id)、部门名称(Name)属性;再定义一个员工(Employee)类,该类的属性有员工编号
- 国外十几种国外撸美金项目适合小白们
- Centos7安装开启BBR加速
- Jupyter notebook 报错 500 : Internal Server Error的解决方法
- Android常用的音频驱动节点介绍