依赖注入-7

依赖注入

  1. 项目结构图

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Pij3tSkT-1667351873764)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221102091419425.png)]

  2. pom.xml

    <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>thinking-in-spring</artifactId>
            <groupId>org.xiaoge</groupId>
            <version>1.0-SNAPSHOT</version>
            <relativePath>../pom.xml</relativePath>  <!--解决依赖顺序问题, 解决依赖集成问题-->
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>ioc-container-overview</artifactId>
    
    
        <dependencies>
    <!--        spring ioc 核心依赖-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
            </dependency>
        </dependencies>
    
    </project>
    
  3. dependency-lookup-context.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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <!--    实施加载bean-->
        <bean id="user" class="org.xiaoge.thinking.in.spring.ioc.overview.domain.User">
            <property name="id" value="1" />
            <property name="name" value="xiaoge"/>
        </bean>
    
        <!-- 延迟bean 是沟通objectFactory简介创建的 -->
        <bean id="objectFactory" class="org.springframework.beans.factory.config.ObjectFactoryCreatingFactoryBean">
            <property name="targetBeanName" value="user" />
        </bean>
    
        <!-- 延迟bean 是沟通objectFactory简介创建的 -->
        <bean id="superUser" class="org.xiaoge.thinking.in.spring.ioc.overview.domain.SuperUser" parent="user" primary="true">
            <property name="address" value="武汉" />
        </bean>
    
    </beans>
    
  4. dependency-injection-context.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:util="http://www.springframework.org/schema/util"
           xsi:schemaLocation="
            http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
    
        <!--    通过导入服用 dependency-lookup-context.xml -->
        <import resource="dependency-lookup-context.xml" />
    
        <bean id="userRepository" class="org.xiaoge.thinking.in.spring.ioc.overview.repository.UserRepository"
        autowire="byType"> <!-- auto-wiring autowire自动绑定配置 -->
    <!-- 手动配置        -->
    <!--        <property name="users">-->
    <!--            <util:list>-->
    <!--                <ref bean="superUser" />-->
    <!--                <ref bean="user" />-->
    <!--            </util:list>-->
    <!--        </property>-->
    
        </bean>
    
    
    </beans>
    
  5. Super注解

    package org.xiaoge.thinking.in.spring.ioc.overview.annotation;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    /**
     * @Classname Super
     * @Date 2022/11/1 16:06
     * @Created by ZhangXiao
     * @Description TODO
     */
    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    public @interface Super {
    }
    
  6. User.class

    package org.xiaoge.thinking.in.spring.ioc.overview.domain;
    
    /**
     * @Classname User
     * @Date 2022/10/17 14:57
     * @Created by ZhangXiao
     * @Description TODO
     */
    public class User {
    
        private Long id;
    
        private String name;
    
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        @Override
        public String toString() {
            return "User{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    '}';
        }
    }
    
  7. SuperUser.class

    package org.xiaoge.thinking.in.spring.ioc.overview.domain;
    
    import org.xiaoge.thinking.in.spring.ioc.overview.annotation.Super;
    
    /**
     * @Classname SuperUser 超级用户
     * @Date 2022/11/1 16:06
     * @Created by ZhangXiao
     * @Description TODO
     */
    @Super
    public class SuperUser extends User{
    
        private String address;
    
        public String getAddress() {
            return address;
        }
    
        public void setAddress(String address) {
            this.address = address;
        }
    
        @Override
        public String toString() {
            return "SuperUser{" +
                    "address='" + address + '\'' +
                    "} " + super.toString();
        }
    }
    
  8. UserRepository.class

    package org.xiaoge.thinking.in.spring.ioc.overview.repository;
    
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.ObjectFactory;
    import org.springframework.context.ApplicationContext;
    import org.xiaoge.thinking.in.spring.ioc.overview.domain.User;
    
    import java.util.Collection;
    
    /**
     * @Classname UserRepository
     * @Date 2022/11/1 16:52
     * @Created by ZhangXiao
     * @Description TODO
     */
    public class UserRepository {
    
        private Collection<User> users; // 自定义  bean
    
        private BeanFactory beanFactory; // 内建 非 bean 对象 (依赖)
    
        private ObjectFactory<ApplicationContext> objectFactory;
    
        public Collection<User> getUsers() {
            return users;
        }
    
        public void setUsers(Collection<User> users) {
            this.users = users;
        }
    
        public BeanFactory getBeanFactory() {
            return beanFactory;
        }
    
        public void setBeanFactory(BeanFactory beanFactory) {
            this.beanFactory = beanFactory;
        }
    
        public ObjectFactory<ApplicationContext> getObjectFactory() {
            return objectFactory;
        }
    
        public void setObjectFactory(ObjectFactory<ApplicationContext> objectFactory) {
            this.objectFactory = objectFactory;
        }
    }
    
  9. 测试类

    package org.xiaoge.thinking.in.spring.ioc.overview.dependency.injection;
    
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.ListableBeanFactory;
    import org.springframework.beans.factory.ObjectFactory;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.xiaoge.thinking.in.spring.ioc.overview.annotation.Super;
    import org.xiaoge.thinking.in.spring.ioc.overview.domain.User;
    import org.xiaoge.thinking.in.spring.ioc.overview.repository.UserRepository;
    
    import java.util.Map;
    
    /**
     * 注意: 依赖注入/依赖查找 它们的来源并不是一个
     * 依赖注入示例 看 UserRepository
     *  1. 名称集合注入: util:list
     *  2. 类型集合注入: autowire
     *  3. 注入内部bean: users
     *  4. 注入非内建bean: BeanFactory
     *  5. 延迟注入: ObjectFactory
     *
     * @Classname DependencyInjectionDemo
     * @Date 2022/10/17 14:56
     * @Created by ZhangXiao
     * @Description TODO
     */
    public class DependencyInjectionDemo {
    
        public static void main(String[] args) {
            // 配置xml配置文件
            // 启动spring应用上下文
            BeanFactory beanFactory = new ClassPathXmlApplicationContext("classpath:META-INF/dependency-injection-context.xml");
    
            UserRepository userRepository = beanFactory.getBean("userRepository", UserRepository.class);
    
            // 依赖注入, 注入内部bean
            System.out.println(userRepository.getUsers());
    
            // 依赖注入  注入非bean对象
            System.out.println(userRepository.getBeanFactory());
            System.out.println(userRepository.getBeanFactory() == beanFactory);
    
            // 依赖查找 (错误)
            // System.out.println(beanFactory.getBean(BeanFactory.class));
    
            // 延迟注入
            ObjectFactory<ApplicationContext> objectFactory = userRepository.getObjectFactory();
            System.out.println(objectFactory.getObject());
    
            System.out.println(objectFactory.getObject() == beanFactory);
    
        }
    
    
    }
    
    
    
    
    /*
    	运行结果
    		[User{id=1, name='xiaoge'}, SuperUser{address='武汉'} User{id=1, name='xiaoge'}]
    		
    		org.springframework.beans.factory.support.DefaultListableBeanFactory@22f71333: defining beans 			
    		[user,objectFactory,superUser,userRepository]; root of factory hierarchy
    		
    		false
    		
    		org.springframework.context.support.ClassPathXmlApplicationContext@7e6cbb7a, started on Wed Nov 02 09:17:05 CST 2022
            
            true
    
    */
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

只因为你温柔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值