连初级都算不上的代码小白 2023-11-18 13:54 采纳率: 52.4%
浏览 9
已结题

spring全注解开发出错


package config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("POJO")
public class SpringConfig2 {
}

package POJO;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;

@Component
public class Cat {
    public Cat() {
        System.out.println("testMod---cat");
    }
    public Cat(String arg){
        System.out.println(arg);
    }
}

上面是配置了和bean类,下面是测试类

public class Main {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(SpringConfig2.class);
    }
}

项目结构如下

img


为什么会报错

img

  • 写回答

3条回答 默认 最新

  • 关注

    错误原因是pom中加了

    <properties>
            <maven.compiler.source>17</maven.compiler.source>
            <maven.compiler.target>17</maven.compiler.target>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 11月26日
  • 已采纳回答 11月18日
  • 创建了问题 11月18日