Spring Framework - configuration injection

Spring framework implemented method of implantation there are many, one configured injection

A typical three-tier architecture:

. 1  Package DAO;
 2  
. 3  Import entity.User;
 . 4  
. 5  / ** 
. 6  * DAO interface increases, defines the required persistence method
 . 7   * / 
. 8  public  interface UserDao {
 . 9      public  void Save (the User User);
 10 }
. 1  Package dao.impl;
 2  
. 3  Import dao.UserDao;
 . 4  Import entity.User;
 . 5  
. 6  / ** 
. 7  * DAO user class, IDao implement interfaces for the operation of the User class persistent
 . 8   * / 
. 9  public  class UserDaoImpl the implements UserDao {
 10  
. 11      public  void save (the user user) {
 12 is          // here not achieve a complete database operations merely illustrative 
13 is          System.out.println ( "user information stored in the database" );
 14      }
 15 }
. 1  Package -Service;
 2  
. 3  Import entity.User;
 . 4  
. 5  / ** 
. 6  * user service interface which defines the required business methods
 . 7   * / 
. 8  public  interface UserService {
 . 9      public  void AddNewUser (the User User);
 10 }
. 1  Package service.impl;
 2  
. 3  Import service.UserService;
 . 4  Import dao.UserDao;
 . 5  Import entity.User;
 . 6  
. 7  / ** 
. 8  * user traffic class, to achieve business management User functions
 . 9   * / 
10  public  class UserServiceImpl the implements UserService {
 . 11  
12 is      // declaration of the interface reference type, and decoupling implementation class 
13 is      Private UserDao DAO;
 14  
15      // generated constructor with no arguments 
16      public UserServiceImpl () {
 . 17  
18 is      }
. 19  
20 is      // parameters as constructor assignment DAO 
21 is      public UserServiceImpl (UserDao DAO) {
 22 is          the this .dao = DAO;
 23 is      }
 24  
25      public UserDao getDao () {
 26 is          return DAO;
 27      }
 28  
29      // DAO properties setter accessor is invoked Spring, setter injection to achieve 
30      public  void setDao (UserDao DAO) {
 31 is          the this .dao = DAO;
 32      }
 33 is  
34 is      public  void AddNewUser (the User User) {
 35          //DAO method calls the user to save the user information 
36          dao.save (User);
 37 [      }
 38 is }

Entity classes:

. 1  Package Entity;
 2  
. 3  / ** 
. 4  * User entity class
 . 5   * / 
. 6  public  class the User the implements the java.io.Serializable {
 . 7      Private Integer ID; // User ID 
. 8      Private String username; // username 
. 9      Private String password ; // password 
10      Private String In Email; // email
 . 11  
12 is      // getters & the setter 
13 is      public Integer getId () {
 14         return id;
15     }
16 
17     public void setId(Integer id) {
18         this.id = id;
19     }
20 
21     public String getUsername() {
22         return username;
23     }
24 
25     public void setUsername(String username) {
26         this.username = username;
27     }
28 
29     public String getPassword() {
30         return password;
31     }
32 
33     public void setPassword(String password) {
34         this.password = password;
35     }
36 
37     public String getEmail() {
38         return email;
39     }
40 
41     public void setEmail(String email) {
42         this.email = email;
43     }
44 
45 }

Core configuration file:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans
 5     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
 6     http://www.springframework.org/schema/aop
 7     http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
 8     <!--以上是Spring框架的头信息 -->
     <! - placing the object instance to be managed ->
910      <-! Embodiment constructed using injection ->
 . 11      <the bean ID = "userDao" class = "dao.impl.UserDaoImpl"> </ the bean>
 12 is      <the bean ID = "UserService" class = "service.impl. UserServiceImpl ">
 13 is          <-! through constructor configured to achieve injection ->
 14          <constructor-Arg>
 15              <REF = the bean" userDao "/>
 16          </ Arg-constructor>
 . 17      </ the bean>
 18 is  
. 19 </ beans>

Test Methods:

 1 package test;
 2 
 3 import org.junit.Test;
 4 import org.springframework.context.ApplicationContext;
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 
 7 import service.UserService;
 8 import service.impl.UserServiceImpl;
 9 
10 import entity.User;
11 
12 
13 public class AopTest {
14 
15     @Test
16     public void aopTest() {
17         ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
18         UserService service = (UserService) ctx.getBean("UserService");
19         
20         User user = new User();
21         user.setId(1);
22         user.setUsername("test");
23         user.setPassword("123456");
24         user.setEmail("[email protected]");
25 
26         service.addNewUser(user);
27     }
28 
29 }

operation result:

12-29 16:22:52[INFO]org.springframework.context.support.ClassPathXmlApplicationContext
 -Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2db0f6b2: startup date [Sun Dec 29 16:22:52 CST 2019]; root of context hierarchy
12-29 16:22:52[INFO]org.springframework.beans.factory.xml.XmlBeanDefinitionReader
 -Loading XML bean definitions from class path resource [applicationContext.xml]
12-29 16:22:52[INFO]org.springframework.beans.factory.support.DefaultListableBeanFactory
 -Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2f686d1f: defining beans [userDao,UserService]; root of factory hierarchy
保存用户信息到数据库

  

Guess you like

Origin www.cnblogs.com/dongyaotou/p/12115385.html