Home  >  Article  >  Java  >  Spring Framework Learning (2) Dependency Injection

Spring Framework Learning (2) Dependency Injection

黄舟
黄舟Original
2016-12-29 13:10:091352browse

The spring framework provides us with three injection methods, namely set injection, constructor injection, and interface injection. Interface injection is not required. The first two methods are introduced below.

1, set injection
Using the set method of the attribute to initialize it becomes set injection.
1) Assign values ​​to ordinary character types.

public class User{
   privateString username;
 
   publicString getUsername() {
       returnusername;
   }
   publicvoid setUsername(String username) {
      this.username= username;
   }
}

We only need to provide the set method of the attribute, and then go to the properties file to configure it so that the framework can find the beans tag of the applicationContext.xml file. Add bean tag to tag beans,
Specify id, class value, id value is not required, class value is the complete path where the object is located. Add the property
tag to the bean tag, requiring that the name value be consistent with the corresponding property name in the User class. The value value is the value we want to assign to the username attribute in the User class.

<bean id="userAction"class="com.lsz.spring.action.User" >
	<property name="username" value="admin"></property>
</bean>

2) Assign a value to the object
Also provide the set method of the object

public class User{
     private UserService userservice;
     public UserServicegetUserservice() {
          returnuser;
     }
     public void setUserservice(UserService userservice){
         this.userservice= userservice;
     }
}

In the configuration file, add the bean label declaration of UserService and the reference of the User object to UserService.

<!--对象的声明-->
<bean id="userService" class="com.lsz.spring.service.UserService"></bean>
 
<bean id="userAction"class="com.lsz.spring.action.User" >
   <property name="userservice" ref="userService"></property>
</bean>

With this configuration, the framework will inject the UserService object into the User class.

3) Assign values ​​to the list collection
Also provide the set method

public class User{
    privateList<String> username;
    publicList<String> getUsername() {
        returnusername;
    }
    publicvoid setUsername(List<String> username) {
        this.username= username;
    }
}
<bean id="userAction"class="com.lsz.spring.action.User" >
     <propertyname="username">
           <list>
               <value>zhang,san</value>
               <value>lisi</value>
               <value>wangwu</value>                                
               </list>
    </property>
</bean>

4) Assign values ​​to the fields in the properties file

public class User{
    privateProperties props ;
    publicProperties getProps() {
        returnprops;
    }
    publicvoid setProps(Properties props) {
        this.props= props;
    }
}
<bean>
    <propertyname="props">
        <props>
           <propkey="url">jdbc:oracle:thin:@localhost:orl</prop>
           <propkey="driverName">oracle.jdbc.driver.OracleDriver</prop>
           <propkey="username">scott</prop>
           <propkey="password">tiger</prop>
        </props>
    </property>
</bean>

23ac7bbddc590263b57e27da10ea2cdfkey in the tag The value is the name in the .properties property file

Note:
No matter what value is assigned, the name attribute value of the 3fcb97bb666cd7884d4d3210fb47b5ef tag in the configuration file must be consistent with the name in the object.


2 Constructor injection
1) One parameter of the constructor

public class User{
    privateString usercode;
    publicUser(String usercode) {
        this.usercode=usercode;
    }
}
<bean id="userAction"class="com.lsz.spring.action.User">                        
    <constructor-argvalue="admin"></constructor-arg>                        
</bean>

2) When the constructor has two parameters
When the parameter is a non-string type, The type needs to be specified in the configuration file. If the type is not specified, the value will be assigned as the string type.
When the parameter types are inconsistent, the framework searches according to the type of the string, so the location of the parameter needs to be specified in the configuration file

<constructor-argvalue="admin"index="0"></constructor-arg>                
<constructor-argvalue="23" type="int"index="1"></constructor-arg>

This is the first parameter in the constructor It is of string type, and the second parameter is of int type


## The above is the content of spring framework learning (2) dependency injection. For more related content, please pay attention to the PHP Chinese website (www .php.cn)!



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn