美文网首页
Spring-IOC/DI(注解)

Spring-IOC/DI(注解)

作者: 通灵路耳 | 来源:发表于2020-06-22 14:17 被阅读0次
    @Autowired  注入对象
    @Component 注入bean
    

    代码

    1、导入jar包
    链接:https://pan.baidu.com/s/1wtBmbkGKWRNZb6bTyxK5Eg 
    提取码:op0j
    2、写model
    Category
    
    package com.llhc.pojo;
    import org.springframework.stereotype.Component;
    @Component("c")
    public class Category {
        private int id;
        private String name = "张三";
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    }
    
    Product
    
    package com.llhc.pojo;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    @Component("p")
    public class Product {
        private int id;
        private String name = "里斯";
        @Autowired
        private Category category;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public Category getCategory() {
            return category;
        }
        public void setCategory(Category category) {
            this.category = category;
        }
    }
    
    3、配置文件
    
    <?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:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/context     
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">
       
       <!-- 我将使用注解方式 -->
        <context:component-scan base-package="com.llhc.pojo"/>
      </beans>
    
    
    4、测试类
    
    package com.llhc.controller;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import com.llhc.pojo.Category;
    import com.llhc.pojo.Product;
    
    public class TestSpring {
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
            Product p=(Product) context.getBean("p");
            System.out.println(p.getName());
            System.out.println(p.getCategory().getName());
        }
    }
    
    
    图片.png

    相关文章

      网友评论

          本文标题:Spring-IOC/DI(注解)

          本文链接:https://www.haomeiwen.com/subject/mlmxfktx.html