! <- spring in the dependency injection Dependency Injection: Dependency Injection role of the IOC: to reduce the coupling (dependencies) between programs dependencies management: after all to spring to maintain the need to use other types of objects in the current class, provides us with the spring, we just need to explain in the configuration file maintenance dependencies: it is called dependency injection dependency injection: can inject data: there are three basic types String and other bean types (in the configuration file or the configuration notes off the bean) complex type / collection type injection ways: three first: using the provided constructor second: providing method using the set third: annotations provided ->
<! - Constructor injection: Tags used: constructor - Age location tag appear: internal bean tag label attribute type: type of data used to specify the data to be injected, the data type is a constructor certain parameters or type index: injecting data used to specify the index position of the specified parameter assigned to the constructor, the location index starts from 0 name: specifies the name of the parameter assigned to the constructor (conventional) = assigning three or more ================== ==================== for which parameter to the constructor = value: for a basic type String and data of type ref: other bean types for the specified data. It refers to appear in the spring of IOC core container had bean objects advantage: while acquiring bean object, inject data is a must, otherwise the object can not be created successfully drawbacks: change the instantiation way bean object, so that we when you create an object, if not these data must also be provided. -> <the bean ID = "AccountService" class="cn.flypig666.service.impl.AccountServiceImpl"> <constructor-arg name="name" value="飞猪"></constructor-arg> <constructor-arg name="age" value="20"></constructor-arg> <constructor-arg name="birthday" ref="now"></constructor-arg> </bean> <!--配置一个日期对象--> <bean id="now" class="java.util.Date"></bean>
public class AccountServiceImpl the implements IAccountService { // If the data is constantly changing, does not apply to the injection manner Private String name; Private Integer Age; Private a Date Birthday; public AccountServiceImpl (name String, Integer Age, a Date Birthday) { the this .name = name; the this .age = Age; the this .birthday = Birthday; } public void saveAccount () { System.out.println ( "saveAccount method of performing service ....." + name + "," + age + ", "+ Birthday); } }