Spring依赖注入的模式和类型
程序员文章站
2022-06-13 13:02:03
...
手动模式 - 配置或者编程的方式,提前安排注入规则
- XML 资源配置元信息
- Java 注解配置元信息
- API 配置元信息
自动模式 - 实现方提供依赖自动关联的方式,按照內建的注入规则
- Autowiring(自动绑定)
实体类Rumenz.java
package com.rumenz;
public class Rumenz {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
实体类Holder.java
package com.rumenz;
public class Holder {
private Rumenz rumenz;
public Rumenz getRumenz() {
return rumenz;
}
public void setRumenz(Rumenz rumenz) {
this.rumenz = rumenz;
}
}
XML 资源配置元信息
配置文件Beans.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">
<bean id="rumenzz" class="com.rumenz.Rumenz">
<property name="id" value="123"/>
<property name="name" value="入门小站"/>
</bean>
<bean id="holder" class="com.rumenz.Holder">
<property name="rumenz" ref="rumenzz"/>
</bean>
</beans>
调用类
package com.rumenz;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DemoApplication {
public static void main(String[] args) {
ClassPathXmlApplicationContext ap=new ClassPathXmlApplicationContext("Beans.xml");
Holder holder= (Holder) ap.getBean("holder");
System.out.println(holder.getRumenz().getName());
}
}
输出
入门小站
Java 注解配置元信息 @Bean
Beans.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">
<bean id="rumenzz" class="com.rumenz.Rumenz">
<property name="id" value="123"/>
<property name="name" value="入门小站"/>
</bean>
</beans>
调用DemoApplication.java
package com.rumenz;
public class DemoApplication {
public static void main(String[] args) {
AnnotationConfigApplicationContext anc=new AnnotationConfigApplicationContext();
anc.register(DemoApplication.class);
XmlBeanDefinitionReader xmlBeanDefinitionReader=new XmlBeanDefinitionReader(anc);
xmlBeanDefinitionReader.loadBeanDefinitions("Beans.xml");
anc.refresh();
Holder holder = anc.getBean(Holder.class);
System.out.println(holder.getRumenz().getName());
anc.close();
}
@Bean
public Holder holder(Rumenz rumenz){
return new Holder(rumenz);
}
}
输出
入门小站
API 配置元信息
配置文件Beans.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">
<bean id="rumenzz" class="com.rumenz.Rumenz">
<property name="id" value="123"/>
<property name="name" value="入门小站"/>
</bean>
</beans>
调用DemoApplication.java
package com.rumenz;
public class DemoApplication {
public static void main(String[] args) {
AnnotationConfigApplicationContext ac=new AnnotationConfigApplicationContext();
// 生成和注册Holder的BeanDefinition
BeanDefinition beanDefinition=createBeanDefinition();
ac.registerBeanDefinition("holder",beanDefinition);
XmlBeanDefinitionReader xmlBeanDefinitionReader=new XmlBeanDefinitionReader(ac);
xmlBeanDefinitionReader.loadBeanDefinitions("Beans.xml");
//启用上下文
ac.refresh();
Holder bean = ac.getBean(Holder.class);
System.out.println(bean.getRumenz().getName());
ac.close();
}
private static BeanDefinition createBeanDefinition() {
BeanDefinitionBuilder beanDefinitionBuilder=BeanDefinitionBuilder.genericBeanDefinition(Holder.class);
beanDefinitionBuilder.addPropertyReference("rumenz","rumenzz");
return beanDefinitionBuilder.getBeanDefinition();
}
}
输出
入门小站
Autowiring(自动绑定)
属性注入Holder.java
package com.rumenz;
import org.springframework.beans.factory.annotation.Autowired;
public class Holder {
@Autowired //属性注入
private Rumenz rumenz;
public Rumenz getRumenz() {
return rumenz;
}
public void setRumenz(Rumenz rumenz) {
this.rumenz = rumenz;
}
}
调用
package com.rumenz;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
public class DemoApplication {
public static void main(String[] args) {
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext();
ac.register(DemoApplication.class);
ac.refresh();
Holder bean = ac.getBean(Holder.class);
System.out.println(bean.getRumenz().getName());
ac.close();
}
@Bean
public Rumenz rumenz(){
Rumenz r = new Rumenz();
r.setId(123);
r.setName("入门小站");
return r;
}
@Bean
public Holder holder(){
return new Holder();
}
}
输出
入门小站
原文: https://rumenz.com/rumenbiji/Spring-dependency-injection.html
上一篇: 我国有名的荔枝品种名都有哪些呢
下一篇: 36、Node对象的属性
推荐阅读
-
Spring学习之依赖注入的方法(三种)
-
浅谈.net core 注入中的三种模式:Singleton、Scoped 和 Transient
-
angular 服务的单例模式(依赖注入模式下)详解
-
因Spring AOP导致@Autowired依赖注入失败的解决方法
-
浅谈.net core 注入中的三种模式:Singleton、Scoped 和 Transient
-
Spring学习之依赖注入的方法(三种)
-
Mybaits 源码解析 (十一)----- 设计模式精妙使用:静态代理和动态代理结合使用:@MapperScan将Mapper接口生成代理注入到Spring
-
Ioc依赖注入:Unity4.0.1 在项目中的应用 (MVC和API)
-
AngularJS的依赖注入实例分析(使用module和injector)
-
spring的依赖注入