标签: spring-boot

Spring MVC或Spring Boot

对于大型公司(Web)项目,您是否建议使用Spring MVC或Spring-Boot?

与Spring MVC相比,Spring-Boot在配置方面非常容易.

我想知道我是否使用Spring-Boot可以拥有与Spring MVC相同的优势?

您有什么推荐的吗?

spring-mvc spring-boot

112
推荐指数
5
解决办法
7万
查看次数

Spring Boot + JPA:忽略列名注释

我有一个依赖的Spring Boot应用程序spring-boot-starter-data-jpa.我的实体类有一个带有列名的列注释.例如:

@Column(name="TestName")
private String testName;
Run Code Online (Sandbox Code Playgroud)

由此生成的SQL创建test_name为列名.在寻找解决方案后,我发现spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy解决了问题(列名取自列注释).

不过,我的问题是为什么没有设置为EJB3NamingStrategyJPA的naming_strategy 忽略列注释?也许hibernate方言与它有关?我正在连接到MS SQL 2014 Express,我的日志包含:

Unknown Microsoft SQL Server major version [12] using SQL Server 2000 dialect
Using dialect: org.hibernate.dialect.SQLServerDialect 
Run Code Online (Sandbox Code Playgroud)

java hibernate jpa spring-boot

111
推荐指数
9
解决办法
9万
查看次数

Spring Boot 2.6.0 / Spring Fox 3 - 无法启动 bean 'documentationPluginsBootstrapper'

我正在尝试使用OpenJDK 15、Spring Boot 2.6.0、Springfox 3 启动 Spring Boot 项目。

我们正在做一个项目,取代Netty作为 Web 服务器并使用 Jetty 来代替,因为我们不需要非阻塞环境。

在代码中我们主要依赖Reactor API(Flux、Mono),所以我们无法删除org.springframework.boot:spring-boot-starter-webflux依赖。

我在一个新项目中复制了我们遇到的问题:https://github.com/jvacaq/spring-fox

我发现build.gradle文件中的这些行是问题的根源。

compile("org.springframework.boot:spring-boot-starter-web") {
   exclude module: "spring-boot-starter-tomcat"
}
compile("org.springframework.boot:spring-boot-starter-jetty")
Run Code Online (Sandbox Code Playgroud)

这是build.gradle文件:

plugins {
    id 'org.springframework.boot' version '2.6.0'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
    mavenCentral()
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web") {
        exclude module: "spring-boot-starter-tomcat"
    }
    compile("org.springframework.boot:spring-boot-starter-jetty")
    implementation 'org.springframework.boot:spring-boot-starter-webflux'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'io.projectreactor:reactor-test'
    implementation "io.springfox:springfox-boot-starter:3.0.0" …
Run Code Online (Sandbox Code Playgroud)

java gradle spring-boot springfox java-11

111
推荐指数
5
解决办法
15万
查看次数

Spring Boot和多个外部配置文件

我有多个属性文件,我想从类路径加载.有一个默认设置/src/main/resources属于myapp.jar.我springcontext希望文件在类路径上.即

<util:properties id="Job1Props"
    location="classpath:job1.properties"></util:properties>

<util:properties id="Job2Props"
    location="classpath:job2.properties"></util:properties>
Run Code Online (Sandbox Code Playgroud)

我还需要使用外部集覆盖这些属性的选项.我有一个外部配置文件夹cwd.根据spring boot doc配置文件夹应该在classpath上.但是从doc中不清楚它是否只会覆盖applicaiton.propertiesconfig中的那个或所有属性.

当我测试它时,只会application.properties被拾取,其余的属性仍然从中获取/src/main/resources.我已经尝试将它们作为逗号分隔列表提供给spring.config.location但是默认设置仍然没有被覆盖.

如何使多个外部配置文件覆盖默认配置文件?

作为我目前使用的解决方法app.config.location(app特定属性),我通过命令行提供.即

java -jar myapp.jar app.config.location=file:./config
Run Code Online (Sandbox Code Playgroud)

然后我改变applicationcontext

<util:properties id="Job2Props"
    location="{app.config.location}/job2.properties"></util:properties>
Run Code Online (Sandbox Code Playgroud)

这就是我在加载Application时在文件和类路径之间进行分离的方法.
EDITS:

//psuedo code

if (StringUtils.isBlank(app.config.location)) {
            System.setProperty(APP_CONFIG_LOCATION, "classpath:");
}
Run Code Online (Sandbox Code Playgroud)

我真的不想使用上面的解决方法,并让spring覆盖类路径上的所有外部配置文件,就像它对application.properties文件所做的那样.

java spring config spring-boot

110
推荐指数
8
解决办法
34万
查看次数

Spring Security:升级 Spring Boot 2.7.0 中已弃用的 WebSecurityConfigurerAdapter

我正在尝试更新它,WebSecurityConfigurerAdapter因为它已被弃用。该类配置如下:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    UsuariService userDetailsService;

    @Autowired
    private AuthEntryPointJwt unauthorizedHandler;
    
    @Bean
    public AuthTokenFilter authenticationJwtTokenFilter() {
        return new AuthTokenFilter();
    }

    @Override
    public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
        authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().and().csrf().disable().exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and().authorizeRequests()
                .antMatchers("/api/auth/**").permitAll().antMatchers("/api/test/**").permitAll().antMatchers("/api/v1/**").permitAll().anyRequest()
                .authenticated();

        http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);
    }

}
Run Code Online (Sandbox Code Playgroud)

现在没有了WebSecurityConfigurerAdapter我重新定义同一个类,如下所示:

@Configuration …
Run Code Online (Sandbox Code Playgroud)

spring-security spring-boot

107
推荐指数
4
解决办法
19万
查看次数

运行jar时找不到类路径资源

在Spring Boot 1.1.5和1.1.6中都有这个问题 - 我正在使用@Value注释加载一个类路径资源,当我在STS(3.6.0,Windows)中运行应用程序时,它可以正常工作.但是,当我运行mvn包然后尝试运行jar时,我得到FileNotFound异常.

资源message.txt位于src/main/resources中.我检查了jar并验证它在顶层包含文件"message.txt"(与application.properties相同的级别).

这是应用程序:

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application implements CommandLineRunner {

    private static final Logger logger = Logger.getLogger(Application.class);

    @Value("${message.file}")
    private Resource messageResource;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void run(String... arg0) throws Exception {
        // both of these work when running as Spring boot app from STS, but
        // fail after mvn package, and then running as java -jar
        testResource(new ClassPathResource("message.txt"));
        testResource(this.messageResource);
    }

    private void testResource(Resource resource) {
        try { …
Run Code Online (Sandbox Code Playgroud)

java spring-boot

106
推荐指数
5
解决办法
11万
查看次数

@ Mock,@ MovieBean和Mockito.mock()之间的区别

在创建测试和模拟依赖项时,这三种方法之间有什么区别?

  1. @MockBean:

    @MockBean
    MyService myservice;
    
    Run Code Online (Sandbox Code Playgroud)
  2. @嘲笑:

    @Mock
    MyService myservice;
    
    Run Code Online (Sandbox Code Playgroud)
  3. Mockito.mock()

    MyService myservice = Mockito.mock(MyService.class);
    
    Run Code Online (Sandbox Code Playgroud)

java junit spring mockito spring-boot

106
推荐指数
2
解决办法
5万
查看次数

在SpringBoot中禁用Logback

看来Springboot自动配置使用Logback和Tomcat.我想禁用它并使用我在类路径中提供的那个.

以下错误消息.

LoggerFactory不是Logback LoggerContext,但Logback在类路径上.删除Logback或竞争实现(类org.slf4j.impl.SimpleLoggerFactory)类[org.slf4j.impl.SimpleLoggerFactory]的对象必须是类ch.qos.logback.classic.LoggerContext的实例

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>1.0.1.RELEASE</version>
    </parent>

    <groupId>com.fe</groupId>
    <artifactId>cloudapp</artifactId>
    <version>1.0.0</version>
    <name>Withinet-PaaS</name>
    <description>Develop your web applications in on our infrastructure and we will worry about administration and scalability of your app.</description>

    <properties>
        <java.version>1.7</java.version>
        <guava.version>16.0.1</guava.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <dependencies>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-client</artifactId>
        <version>1.8</version>
    </dependency>
        <dependency>
        <groupId>com.withinet.cloudapp</groupId>
    <artifactId>slave</artifactId>
    <version>1.0.0</version>    
        </dependency>
        <dependency>
            <groupId>org.apache.wicket</groupId>
            <artifactId>wicket-core</artifactId>
            <version>6.15.0</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.0.Final</version>
        </dependency>

        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.2.4</version>
        </dependency>

        <!-- Spring Boot -->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId> …
Run Code Online (Sandbox Code Playgroud)

spring-boot

104
推荐指数
15
解决办法
13万
查看次数

Spring Boot JPA - 配置自动重新连接

我有一个很好的小Spring Boot JPA Web应用程序.它部署在Amazon Beanstalk上,并使用Amazon RDS来保存数据.然而,经常使用它并因此在一段时间后因此类异常而失败:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:从服务器成功收到的最后一个数据包是79,870,633毫秒.
成功发送到服务器的最后一个数据包是79,870,634毫秒.比服务器配置的'wait_timeout'值长.您应该考虑在应用程序中使用之前过期和/或测试连接有效性,增加服务器配置的客户端超时值,或使用Connector/J连接属性"autoReconnect = true"来避免此问题.

我不知道如何配置此设置,但无法在http://spring.io上找到相关信息(虽然这是一个非常好的网站).有哪些想法或信息指针?

configuration spring spring-boot

102
推荐指数
4
解决办法
10万
查看次数

从IntelliJ运行时如何激活Spring Boot配置文件?

我有5个环境:

 - local (my development machine)
 - dev
 - qc
 - uat
 - live
 - staging
Run Code Online (Sandbox Code Playgroud)

我希望为每个环境使用不同的应用程序属性,因此我有以下属性文件,每个文件都有不同的数据源URL:

 - application.properties  (containing common properties)
 - application-local.properties
 - application-dev.properties
 - application-qc.properties
 - application-uat.properties
 - application-live.properties
Run Code Online (Sandbox Code Playgroud)

我正在使用IntelliJ并在本地计算机上的Gradle插件中使用bootRun运行我的应用程序.我将使用在运行Tomcat的所有其他环境上部署相同的应用程序war文件.

我试过添加:

--spring.profiles.active =本地

到脚本参数下的运行配置.

我试过添加

-Dspring.profiles.active =本地

到VM选项下的运行配置.

都没有工作.我一直看到启动时的INFO消息说:没有活动的配置文件集,回退到默认配置文件:默认

如果我使用Windows命令行运行我的应用程序

gradle bootRun
Run Code Online (Sandbox Code Playgroud)

但我首先设置了环境变量

set SPRING_PROFILES_ACTIVE=local
Run Code Online (Sandbox Code Playgroud)

一切正常.

所以我的问题是,从IntelliJ运行bootRun时如何激活本地弹簧启动配置文件?

spring intellij-idea gradle spring-boot

102
推荐指数
8
解决办法
10万
查看次数