spring相关注解说明

该博客介绍了项目相关内容,包含项目目录,有实体类、启动器等;配置类分为主配置类和子配置类,还有测试类;此外还提及了项目的依赖。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目目录

在这里插入图片描述

实体类
@Data       //直接生成get、set、toString、hashcode方法
@AllArgsConstructor //生成所有参数的构造器
@NoArgsConstructor  //生成无参构造器
public class User {
    private Integer id;
    private String name;
    private String address;
}
@Data       //直接生成get、set、toString、hashcode方法
@AllArgsConstructor //生成所有参数的构造器
@NoArgsConstructor  //生成无参构造器
public class Person {
    private Integer id;
    private String name;
    private String address;
}
启动器
@SpringBootApplication
/**
 * 只要和启动类同级目录下的文件:就可以自动被扫描
 *
 * java/config与启动类不在同级目录,因此需要扫包
 */
@ComponentScan(basePackages = "config") //扫描java/config下的两个配置类,就不需要import了

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

主配置类

@Configuration
//@Import(value = {JavaConfigA.class, JavaConfigB.class})
public class AppConfig {
}

子配置类

@Configuration   //相当于创建一个spring的xml文件
public class JavaConfigA {

    //@Primary//设置优先级:当IOC容器里面有多个对象里,那么会有冲突,可以使用这个注解来标记它为主要的使用对象
    @Bean("user1")   //相当于xml里边<bean id ="user" class = "com.sxt.domain.User"/>
    public User getUser1(){
        return new User(1,"张三","北京");
    }
    @Bean("user2")
    public User getUser2(){
        return new User(12,"张三2","北京2");
    }

    @Bean("user3")
    public User getUser3(@Qualifier("user2") User user){
        user.setName("小红");
        return user;
    }
}
@Configuration   //相当于创建一个spring的xml文件
public class JavaConfigB {

    @Bean   //相当于xml里边<bean id ="user" class = "com.sxt.domain.Person"/>
    public Person getPerson1(){
        return new Person(2,"李四","上海");
    }
}

测试类

@SpringBootTest
class ApplicationTests {

    @Autowired
    //@Qualifier("user2")
    private User user2;

    @Autowired
    //@Qualifier("user1")
    private User user1;

    @Autowired
    @Qualifier("user3")
    private User user3;

    @Autowired
    private Person person;

    @Test
    void contextLoads() {
        System.out.println(user2);
        System.out.println(user1);
        System.out.println("user3:"+user3);
        System.out.println(person);
    }
}
依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.sxt</groupId>
    <artifactId>02_springboot_config01</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>02_springboot_config01</name>
    <description>spring相关注解说明</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值