spring注解的使用

该文章介绍了如何通过Spring框架的注解方式来处理实体类。首先,在实体类上添加@Component注解,然后在applicationContext.xml配置文件中扫描对应的包。接着通过测试类验证Spring容器能否正确创建和管理Bean。最后展示了如何进行属性注入,包括使用@Autowired和@Value注解来自动装配Bean的依赖。
摘要由CSDN通过智能技术生成

一、步骤

1.给实体类添加注解

2.在配置文件(applicationContext.xml)中扫描实体类所在包

3.测试

4.属性注入

目录结构

 

 二、实操

 1.给实体类添加注解

package com.zxs.pojo;

import org.springframework.stereotype.Component;

@Component
public class Book {
    private int id;
    private String name;
    private String author;

    public Book(){
        System.out.println("调用了Book类的无参构造器");
    }

    public Book(int id2, String name, String author) {
        this.id = id2;
        this.name = name;
        this.author = author;
        System.out.println("调用了Book类的有参构造器");
    }

    public int getId() {
        return id;
    }

    public void setId1(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", author='" + author + '\'' +
                '}';
    }
}

2.在配置文件(applicationContext.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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <!--    扫描实体类包,通过注解创建实体类    -->
    <context:component-scan base-package="com.zxs.pojo"></context:component-scan>


</beans>

3.测试

package test;

import com.zxs.pojo.Book;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test01 {
    public static void main(String[] args) {
        //创建Spring容器
        //当创建Spring容器的时候,会自动创建beans中的所有类
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取对象
        Book book= (Book) context.getBean("book");
        //打印对象信息
        System.out.println(book);
    }
}

4.属性注入

Boy

package com.zxs.pojo;

public class Boy {
    private int age;
    private String name;

    public Boy(){}

    public Boy(int age, String name) {
        this.age = age;
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Boy{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}

Girl

package com.zxs.pojo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Girl {

    @Value("21")
    private int age;
    @Value("丽丽")
    private String name;
    @Autowired
    private Boy boyfriend;

    public Girl(){}

    public Girl(int age, String name, Boy boyfriend) {
        this.age = age;
        this.name = name;
        this.boyfriend = boyfriend;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Boy getBoyfriend() {
        return boyfriend;
    }

    public void setBoyfriend(Boy boyfriend) {
        this.boyfriend = boyfriend;
    }

    @Override
    public String toString() {
        return "Girl{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", boyfriend=" + boyfriend +
                '}';
    }
}

Test01

package test;

import com.zxs.pojo.Book;
import com.zxs.pojo.Girl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test01 {
    public static void main(String[] args) {
        //创建Spring容器
        //当创建Spring容器的时候,会自动创建beans中的所有类
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取对象
        Girl girl=(Girl) context.getBean("girl");
        //打印对象信息
        System.out.println(girl);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值