依赖注入
-
项目结构图
-
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>
-
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>
-
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>
-
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 { }
-
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 + '\'' + '}'; } }
-
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(); } }
-
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; } }
-
测试类
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 */