What are the three ways the spring IOC injects:
a setter 原理 : 在目标对象中,定义需要注入的依赖对象对应的属性和setter方法;“让ioc容器调用该setter方法”,将ioc容器实例化的依赖对象通过setter注入给目标对象,封装在目标对象的属性中。b 构造器 原理 : 为目标对象提供一个构造方法,在构造方法中添加一个依赖对象对应的参数。ioc容器解析时,实例化目标对象时会自动调用构造方法,ioc只需要为构造器中的参数进行赋值;将ioc实例化的依赖对象作为构造器的参数传入。【接口注入 原理 : 为依赖对象提供一个接口实现,将接口注入给目标对象,实现将接口的实现类注入的效果。比如HttpServletRequest HttpServletResponse接口 注意:这是ioc提供的方式,spring中的ioc技术并没有实现该种注入方式】c 方法注入 原理 : 在目标对象中定义一个普通的方法,将方法的返回值设置为需要注入的依赖对象类型。通过ioc容器调用该方法,将其创建的依赖对象作为方法的返回值返回给目标对象。
Second, the concrete realization
Three methods in the implementation of the main is injected and applicationcontext.xml different, the specific wording including the configuration of the parameters are as follows:
Action
PackageCom.etoak.action;ImportCom.etoak.dao.UserDaoImpl; Public class loginaction { / * * 1 Setter injection STRUTS2 Form single package is the use of setter injection * Private Userdaoimpl ud; public void Setud (Userdaoimpl ud,string name) {This.ud = UD; } */ / * * 2 constructor method constructor parameter is not fixed, the private Userdaoimpl ud is described in XML; private String name; Public loginaction (Userdaoimpl ud,string name) {This.ud = UD; THIS.name = name; } */ /* * 3 Method Injection: The return value of the method is the type of object to inject */ PublicUserdaoimplEtoak(){return NULL; } Public void Init() {System.out.println ("Loginactoin initialized."); } PublicStringExecute()throwsexception{etoak (). Login ();/*ud.login (); System.out.println ("name---->" +name); */ return "Success"; }}
Applicationcontext.xml:
<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-3.2.xsd "default-lazy-init=" true "> <!--default-lazy-init= "true" delays the bean object initialization to Getbean () instead of automatically initializing the container when it starts <!--Purpose: Using Spring's IOC technology to manage loginaction, Userdaoimpl for passive injection passive injection prerequisites: two objects Need to be managed by the IOC container Init-method: Automatically executes the Property:setter method injection Constructor-arg When the bean is initialized: assigns a value to the constructor of the current call The index value is the constructor parameter ref: The type that represents the assignment is a custom type (reference) value: assigns a value to string, base data type, class type Lookup-method: Method Injected, the bean value is the object to be injected, name is the method name Scope:singleton Singleton prototype prototype request IOC instantiated Bean object storage In the request or session scope, these two values are only in the Web environment session--> <bean id="Action" class="Com.etoak.action.LoginAction" Init-method="init" lazy-init= "true" scope="Singleton"> <!--<property name= "ud" ref= "DAO" ></property> Setter injection -- <!--<constructor-arg index= "0" ref= "DAO" ></constructor-arg> constructor injection: Several parameters are assigned several <constructor-arg index= "1" value= "AAA" ></constructor-arg> - <lookup-method name="Etoak" bean="dao"/> <!--method injection -- </Bean> <!--Lazy-init alone This bean is lazily initialized -- <bean id="DAO" class="Com.etoak.dao.UserDaoImpl" ></Bean></Beans>
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Spring Learning (ii) Three ways the spring IOC injects