Home > Java > javaTutorial > body text

Spring Framework Learning (2) Dependency Injection

黄舟
Release: 2016-12-29 13:10:09
Original
1363 people have browsed it

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;
   }
}
Copy after login

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>
Copy after login

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;
     }
}
Copy after login

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>
Copy after login

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;
    }
}
Copy after login
<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>
Copy after login

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;
    }
}
Copy after login
<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>
Copy after login

key 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 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;
    }
}
Copy after login
<bean id="userAction"class="com.lsz.spring.action.User">                        
    <constructor-argvalue="admin"></constructor-arg>                        
</bean>
Copy after login

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>
Copy after login

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)!



Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!