Spring Dependent Injection Details

1.Getting Started with Dependent Injection for Bean Create a UserService that calls the save() method of UserDao inside ...
5.1 Injection of Common Data Types
5.2 Input of Collection Data Type (List <String>)
5.3 Input of Collection Data Type (Map<String User>)
5.4 Injection of Collection Data Types (Properties)

1.Getting Started with Dependent Injection for Bean
  1. Create a UserService that calls the save() method of UserDao inside the UserService

UserService

public interface UserService { public void save(); }
  • Last time userDao was called with the creation of a test class, userDao was generated by Spring
  • This time userService is used to call userDao

UserServiceImpl

public class UserServiceImpl implements UserService { @Override public void save() { ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml"); UserDao userDao = (UserDao) app.getBean("userDao"); userDao.save(); } }
  • Create a test class UserController to call the methods of the userDao implementation class in userService

  1. Delegate Creation of UserServiceImpl to Spring
<bean id="userDao"></bean> <bean id="userService"></bean>
  1. Get UserService from Spring container for operation
public class UserController { public static void main(String[] args) { ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService userService = (UserService) app.getBean("userService"); userService.save(); } }

2.Bean Dependency Injection Analysis
  • Currently both UserService and UserDao instances exist in the Spring container. The current practice is to get the UserService and UserDao instances outside the container and then combine them in the program.
  • Since both UserService and UserDao are in the Spring container and the final program uses UserService directly, you can set UserDao inside the UserService in the Spring container.
3.Bean's Dependency Injection Concept
  • Dependency Injection: It is a concrete implementation of the core IOC of the Spring Framework.

  • When writing a program, you pass the creation of the object to Spring by controlling the inversion, but there can be no dependencies in the code.

  • IOC decoupling only reduces their dependencies, but does not eliminate them. For example, the business tier still calls the persistence tier's methods.

  • This dependency between the business and persistence tiers is maintained by Spring after it has been used.
    Simply put, just sit back and wait for the framework to bring the persistence layer objects into the business layer instead of acquiring them by ourselves.

4.Bean's Dependency Injection Method

How do I inject UserDao into UserService?

  1. Construction method
  2. set method

1) set method injection
Add setUserDao method to UserServiceImpl

public class UserServiceImpl implements UserService { private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } @Override public void save() { userDao.save(); } }

Configure the Spring container to call the set method for injection

<bean id="userDao"></bean> <bean id="userService"> <property name="userDao" ref="userDao"></property> </bean>
  • Name in property refers to the name of the setXXX property, making UserDao lowercase
  • ref represents the object to be referenced, where the id=userDao in the previous bean is directly referenced
  • To inject userDao inside a container into it through the userDao method in userService



1) set method injection

P namespace injection is also set method injection in nature, but it is more convenient than the set method injection described above, mainly in the configuration file, as follows:

First, you need to introduce the P namespace:

xmlns:p="http://www.springframework.org/schema/p"


Second, you need to modify the injection method

<bean id="userService" p:userDao-ref="userDao"/>
  • test

2) Construction Method Injection
Create a parametric construct

public class UserServiceImpl implements UserService { private UserDao userDao; public UserServiceImpl() { } public UserServiceImpl(UserDao userDao) { this.userDao = userDao; } @Override public void save() { userDao.save(); } }
<bean id="userDao"></bean> <bean id="userService"> <constructor-arg name="userDao" ref="userDao"></constructor-arg> </bean>
  • test

5.Bean's Dependent Injection Data Type
  • The above operations are all injected reference beans, except for object references that can be injected, common data types, collections, and so on, which can be injected in containers.

  • Three types of data injected
    Common data types
    Reference data type
    Collection data type

  • References to data types are no longer discussed here. Previous operations have been to inject references to UserDao objects. The following example demonstrates injection of common data types and collection data types using set method injection.

5.1 Injection of Common Data Types

public class UserDaoImpl implements UserDao { private String username; private int age; public void setUsername(String username) { this.username = username; } public void setAge(int age) { this.age = age; } @Override public void save() { System.out.println("username=" + username + "\n" + "age=" + age); System.out.println("save running......"); } }
<bean id="userDao"> <property name="username" value="xdr"/> <property name="age" value="22"/> </bean>

5.2 Input of Collection Data Type (List <String>)

public interface UserDao { public void save(); }
public class UserDaoImpl implements UserDao { private List<String> strList; public void setStrList(List<String> strList) { this.strList = strList; } public void save() { System.out.println(strList); System.out.println("save running......"); }
public interface UserService { public void save(); }
public class UserServiceImpl implements UserService { private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } public UserServiceImpl() { } public UserServiceImpl(UserDao userDao) { this.userDao = userDao; } @Override public void save() { userDao.save(); } }
  • List<String>is a common data type, with value, and ref if User or other object
<bean id="userDao"> <property name="strList"> <list> <value>aaa</value> <value>bbb</value> <value>ccc</value> </list> </property> </bean> <bean id="userService"> <constructor-arg name="userDao" ref="userDao"></constructor-arg> </bean>
public class UserController { public static void main(String[] args) { ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService userService = (UserService) app.getBean("userService"); userService.save(); } }

5.3 Input of Collection Data Type (Map<String User>)

  • Create User Class
public class User { private String name; private String addr; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddr() { return addr; } public void setAddr(String addr) { this.addr = addr; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", addr='" + addr + '\'' + '}'; } }
  • set method injection
public class UserDaoImpl implements UserDao { private List<User> userList; public void setUserList(List<User> userList) { this.userList = userList; } public void save() { System.out.println(userList); System.out.println("save running......"); } }
  • value-ref denotes a value reference, and the referenced object must exist in the container before it can be injected. Therefore, user1, user2 are created later and the corresponding value is referenced at the same time.
<bean id="userDao"> <property name="userMap"> <map> <entry key="u1" value-ref="user1"></entry> <entry key="u2" value-ref="user2"></entry> </map> </property> </bean> <bean id="user1"> <property name="name" value="tom"/> <property name="addr" value="beijing"/> </bean> <bean id="user2"> <property name="name" value="jerry"/> <property name="addr" value="shanghai"/> </bean> <bean id="userService"> <constructor-arg name="userDao" ref="userDao"></constructor-arg> </bean>
public class UserController { public static void main(String[] args) { ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService userService = (UserService) app.getBean("userService"); userService.save(); } }

5.4 Injection of Collection Data Types (Properties)

  • Properties themselves are strings
<bean id="userDao"> <property name="properties"> <props> <prop key="p1">ppp1</prop> <prop key="p2">ppp2</prop> <prop key="p3">ppp3</prop> </props> </property> </bean> <bean id="userService"> <constructor-arg name="userDao" ref="userDao"></constructor-arg> </bean>
public class UserDaoImpl implements UserDao { private Properties properties; public void setProperties(Properties properties) { this.properties = properties; } public void save() { System.out.println(properties); System.out.println("save running......"); } }
public class UserController { public static void main(String[] args) { ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService userService = (UserService) app.getBean("userService"); userService.save(); } }

  • Put the above configurations together and run:
6.Introduce additional configuration files (modular development)
  • In actual development, Spring has a lot of configuration content, which results in a very complex and bulky Spring configuration, so you can split some of the configuration into other configuration files, while the main Spring configuration file is loaded through the import tag

  • For example, you can reference the configuration files of other sub-modules in the main configuration file

  • By reference to:

<import resource="applicationContext-xxx.xml"/>
  • Once referenced, the subfiles are loaded together as long as the main configuration file is loaded
7.Focus Configuration for Spring
<bean>Label id attribute:In a container Bean Unique identification of instance, no duplication allowed class attribute:To instantiate Bean Fully qualified name scope attribute:Bean Scope of action, commonly used as Singleton(default)and prototype <property>Label: Attribute Injection name Property: Property name value Attribute: Injected common attribute value ref Property: injected object reference value <list>Label <map>Label <properties>Label <constructor-arg>Label <import>Label:Import Other Spring Subfile

12 October 2021, 12:24 | Views: 4813

Add new comment

For adding a comment, please log in
or create account

0 comments