springboot是开源框架,它的主要目的是用来简化新Spring应用的初始搭建以及开发过程,提高开发人员的工作效率,那springboot怎么改端口号?下面来我们就来给大家讲解一下springboot改端口号的方法。
方法一:右击对应的方法,选择Run as à Run Confugerations…
选择Arguments下的VM Arguments:-Dserver.port=8280(对应的端口号),然后直接运行 Run as Java application

方法二:修改配置文件
修改配置文件“application. properties”或者“application.yml”中的server.port = 9290
方法二:通过敲命令

Spring Boot如何导入Spring配置?
1.@ImportResource 导入 Spring 配置文件
在主启动类上使用 @ImportResource 注解可以导入一个或多个 Spring 配置文件,并使其中的内容生效。
1. 以 helloworld 为例,在 net.biancheng.www.service 包下创建一个名为 PersonService 的接口,代码如下。
package net.biancheng.www.service;
import net.biancheng.www.bean.Person;
public interface PersonService
{
public Person getPersonInfo();
}2. 在 net.biancheng.www.service.impl 包下创建一个名为 PersonServiceImpl 的类,并实现 PersonService 接口,代码如下。
package net.biancheng.www.service.impl;
import net.biancheng.www.bean.Person;
import net.biancheng.www.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
public class PersonServiceImpl implements PersonService
{
@Autowired
private Person person;
@Override
public Person getPersonInfo()
{
return person;
}
}3. 在该项目的 resources 下添加一个名为 beans.xml 的 Spring 配置文件,配置代码如下。
<?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="personService" class="net.biancheng.www.service.impl.PersonServiceImpl"></bean> </beans>
4. 修改该项目的单元测试类 HelloworldApplicationTests ,校验 IOC 容器是否已经 personService,代码如下。
package net.biancheng.www;
import net.biancheng.www.bean.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
@SpringBootTest
class HelloworldApplicationTests
{
@Autowired
Person person;
//IOC 容器
@Autowired
ApplicationContext ioc;
@Test
public void testHelloService()
{
//校验 IOC 容器中是否包含组件 personService
boolean b = ioc.containsBean("personService");
if (b)
{
System.out.println("personService 已经添加到 IOC 容器中");
}
else
{
System.out.println("personService 没添加到 IOC 容器中");
}
}
@Test
void contextLoads()
{
System.out.println(person);
}
}5. 运行单元测试代码,结果如下图。

6 在主启动程序类上使用 @ImportResource 注解,将 Spring 配置文件 beans.xml 加载到项目中,代码如下。
package net.biancheng.www;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
//将 beans.xml 加载到项目中
@ImportResource(locations = {
"classpath:/beans.xml"
})
@SpringBootApplication
public class HelloworldApplication
{
public static void main(String[] args)
{
SpringApplication.run(HelloworldApplication.class, args);
}
}8. 再次执行测试代码,结果如下图。

2.全注解方式加载 Spring 配置
Spring Boot 推荐我们使用全注解的方式加载 Spring 配置,其实现方式如下:
1. 使用 @Configuration 注解定义配置类,替换 Spring 的配置文件;
2. 配置类内部可以包含有一个或多个被 @Bean 注解的方法,这些方法会被 AnnotationConfigApplicationContext 或
AnnotationConfigWebApplicationContext 类扫描,构建 bean 定义(相当于 Spring
配置文件中的
以 helloworld 为例,删除主启动类的 @ImportResource 注解,在 net.biancheng.www.config 包下添加一个名为 MyAppConfig 的配置类,代码如下。
package net.biancheng.www.config;
import net.biancheng.www.service.PersonService;
import net.biancheng.www.service.impl.PersonServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Configuration 注解用于定义一个配置类,相当于 Spring 的配置文件
* 配置类中包含一个或多个被 @Bean 注解的方法,该方法相当于 Spring 配置文件中的 <bean> 标签定义的组件。
*/
@Configuration
public class MyAppConfig
{
/**
* 与 <bean id="personService" class="PersonServiceImpl"></bean> 等价
* 该方法返回值以组件的形式添加到容器中
* 方法名是组件 id(相当于 <bean> 标签的属性 id)
*/
@Bean
public PersonService personService()
{
System.out.println("在容器中添加了一个组件:peronService");
return new PersonServiceImpl();
}
}执行测试代码,执行结果如下图。

总之Spring Boot 具有 Spring 一切优秀特性,Spring 能做的事,Spring Boot 都可以做,并且使用更加简单,功能更加丰富,最后大家如果想要了解更多java架构师知识,敬请关注奇Q工具网。
推荐阅读: