Y colocador inyección similar, simplemente cambiarlo dos documentos
1.UserServiceImpl añadió código de constructor:
público de clase UserServiceImpl implementos UserService { privado UserDao UserDAO; // 构造器注入 pública UserServiceImpl (UserDao UserDAO) { este .userDao = UserDAO; } // // 设值注入 // setUserDao public void (UserDao UserDAO) { // this.userDao = UserDAO; // } @ Override pública vacío Guardar (String arg) { System.out.println ( "userServiceImpl接受参数:" + arg); arg = arg + ":" + este .hashCode (); userDao.save (arg); } }
2. Modificar configurado para inyectar Bean.xml
<? xml version = "1.0" encoding = "UTF-8" >? < habas xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001 / XMLSchema instancia" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" > <! - <bean id = clase "UserService" = "test.UserServiceImpl"> -> <! - <property name = "UserDAO" ref = "UserDAO" /> -> <! - </ bean> -> <frijol > < frijol ID = "UserService" clase = "test.UserServiceImpl" > < constructor-arg nombre = "UserDAO" ref = "UserDAO" /> </ bean > </ frijol >
3. Otro código no se mueve, TestMain los resultados: