//赋值注入
<?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 http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="school" class="ioc.iocSetter.School">
<property name="id">
<value>10006</value>
</property>
<property name="name">
<value>BUAA</value>
</property>
</bean>
<bean id="student" class="ioc.iocSetter.Student">
<property name="id">
<value>101</value>
</property>
<property name="name">
<value>Bill</value>
</property>
<property name="school">
<ref bean="school"/>
</property>
<property name="courses">
<list>
<value>英语</value>
<value>物理</value>
<value>高等数学</value>
<value>软件工程</value>
</list>
</property>
</bean>
</beans>
//测试
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class GreetingMain {
public static void main(String[] args) {
//加载配置文件
ClassPathResource resource = new ClassPathResource("ioc/iocSample/beans.xml");
//根据配置文件构造BeanFactory对象
BeanFactory factory = new XmlBeanFactory(resource);
//取得提供具体业务逻辑的Java Bean
Action action = (Action)factory.getBean("greet");
//调用Java Bean中的具体方法
action.greet();
}
}
//构造器注入
<?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 http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="school" class="ioc.iocConstructor.School">
<property name="id">
<value>10006</value>
</property>
<property name="name">
<value>BUAA</value>
</property>
</bean>
<bean id="student" class="ioc.iocConstructor.Student">
<constructor-arg index="0">
<value>101</value>
</constructor-arg>
<constructor-arg index="1">
<value>Bill</value>
</constructor-arg>
<constructor-arg index="2">
<list>
<value>英语</value>
<value>物理</value>
<value>高等数学</value>
<value>软件工程</value>
</list>
</constructor-arg>
<constructor-arg index="3">
<ref bean="school"/>
</constructor-arg>
</bean>
</beans>
分享到:
相关推荐
Spring-注入依赖,AOP,自动注入Bean。入门。为什么没有0积分?
主要介绍了Spring使用@Autowired为抽象父类注入依赖代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
自定义依赖注入工具类SpringUtil.java
该资源包含了一个利用spring实现的依赖注入的案例,简单易懂。
本项目为Java项目,使用Spring实现依赖注入,适合初学者了解Spring依赖注入的实现。
Java项目,已有Spring所需的jar包。适合于入门篇。。。。。。。。。。。。。。。。。。。。。。。。。。
我们的思路是应用程序用到一个Foo类,就会创建Foo类并调用Foo类的方法,假如这个方法内需要一个Bar类,就会创建Bar类并调用Bar类的方法,而这个方法内需要一个...总之容器负责实例化,注入依赖,处理依赖关系等工作。
Dagger2进阶用法,不用再创建大量的Component和Module
控制反转 依赖注入的c#实现,很好的教程。
实现了基本验证登录功能,没验证就不能请求接口。 Demo比较简单,代码不多,没有无用代码,注释能带的都带上了,适合新手学习。
第一DI 轻松为打字稿应用程序注入依赖描述: 为了运行该库,需要使用Metadata Reflection API。 如果您的平台(浏览器/ nodejs)不支持,则必须使用polifyll。 示例: 。 为了进行工作反射,应在tsconfig文件中启用...
<?... namespace Auryn; class CachingReflector implements Reflector { ... const CACHE_KEY_CLASSES = 'auryn.refls.classes.';... const CACHE_KEY_...总之容器负责实例化,注入依赖,处理依赖关系等工作。
较为框架式的演示了DI服务依赖注入 适用于熟悉C#中的继承,基本的Core命令使用等. 该示例演示了以服务器集群为背景的各种业务的依赖注入. 示例来自于B站杨中科老师的.NET Core(6.0)教学.
✨一个轻量级的PHP依赖注入框架,让你自定义的方法也可以使用依赖注入.
依赖注入,全称是“依赖注入到容器”, 容器(IOC容器)是一个设计模式,它也是个对象,你把某个类(不管有多少依赖关系)放入这个容器中,可以“解析”出这个类的实例。所以依赖注入就是把有依赖关系的类放入容器...
对于spring配置一个bean时,如果需要给该bean提供一些初始化参数,则需要通过依赖注入方式,所谓的 依赖注入就是通过spring将bean所需要的一些参数传递到bean实例对象的过程(将依赖关系注入到对象中) ,spring的...
详解 Spring 3.0 基于 Annotation 的依赖注入实现。。详解 Spring 3.0 基于 Annotation 的依赖注入实现。。
IoC 依赖注入 技术总结 IoC 依赖注入 技术总结
最近有个需求就是一个抽象仓储层接口方法需要SqlServer以及Oracle两种实现方式,为了灵活我在依赖注入的时候把这两种实现都给注入进了依赖注入容器中,但是在服务调用的时候总是获取到最后注入的那个方法的实现,这...
HiltTest:Hilt注入依赖的例子