2.3.2 spring属性注入-注解注入-半注解注入-后序

代码:

spring属性注入-注解注入-半注解后序.zip - 蓝奏云文件大小:15.2 K|https://www.lanzouw.com/iCjbVvpvxaf

上一个博客的半注解是,一个类在xml定义java bean,一个类使用注解方式定义java bean,这节我们将所有的类都使用注解来定义java bean

1、创建Category 和Product

package com.how2j.pojo;
//使用注解配置, 不需要定义set函数,

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
//定义java bean, id为c
@Component("c")
public class Category {
    @Value("4")
    private int id;
    @Value("Category 1")
    private String name;

    //如果使用注解定义java bean,必须有无参构造函数。下面存在有参构造方法,所以如果使用注解必须手动写无参构造函数
    public Category() {
    }


    public Category(int id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Category{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}
package com.how2j.pojo;
//这个类使用注解配置
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

//因为这个类没在xml文件中定义,所以使用注解来将这个类设置成java bean, 并设置id
@Component("p")
public class Product {
    //普通属于类型
    @Value("2")
    private int id;
    @Value("product test")
    private String name;
    //引用类型属性注入
    //这里的c是java bean的id,即Category中定义的@ComPonet("c")
    @Resource(name="c")
    private Category category;

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

3、编写xml,这里的xml只用来进行组件扫描,不再定义java bean

<?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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <!--    半注解方式:一个类在xml中定义,一个类使用注解定义-->
    <!--开启组件扫描, 因为我们使用注解来注入属性,所以-->
    <context:component-scan base-package="com.how2j.pojo"></context:component-scan>


<!--    这个xml文件只用来开启组件扫描,不定义java bean-->
</beans>

4、测试

package test;

import com.how2j.pojo.Category;
import com.how2j.pojo.Product;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpring {

    @Test
    //spring的控制翻转
    public void test1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml" );
        //这里的c是在定义xml文件中
        Category c = (Category) context.getBean("c");

        System.out.println(c);
    }

    @Test
    //测试spring的属性注入
    public void test2(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml" );
        //这里的p是定义在类里的, 即@Component("p")
        Product p = (Product) context.getBean("p");

        System.out.println(p);
    }
}

补充:下面是目录结构

猜你喜欢

转载自blog.csdn.net/stay_zezo/article/details/120938597