Spring IOC dependency injection instance
Operation steps: 1. Create all classes in beans. xml 2. Inject dependent classes 2. 1. Create corresponding Getter and setter methods for each dependent class
2. constructor Injection
. ByName is generally used in automatic injection (not commonly used) development.
Autowire = "default", byName: Injection by name (setUserDao, named userDao); byType: Injection by type (if there are multiple objects of the same type, an exception is thrown, I don't know which one to inject); no: no. Automatic injection can reduce configuration, but the results of the entire class cannot be well understood through the bean file, so autowire is not recommended. 2. 4. Property Injection
1111
2222
3333
3. Singleton and multi-instance (the default is Singleton). 3. 1. When the status of the attribute value does not change, we use Singleton. (Such as dao, Service). For Action, the attribute value status in the Action will get different values according to different threads, so you should use multiple examples (actions ).
Beans. xml
User. class
package com.spring.model;public class User {public User() {super();}private int id;private String username;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}@Overridepublic String toString() {return "User [id=" + id + ", username=" + username + "]";}public User(int id, String username) {super();this.id = id;this.username = username;}}
IUserDao. class
package com.spring.dao;import com.spring.model.User;public interface IUserDao {public void add(User user);public void delete(int id);public User load(int id);}
UserDao. class
Package com. spring. dao; import com. spring. model. user; public class UserDao implements IUserDao {@ Overridepublic void delete (int id) {System. out. println ("deleted" + id) ;}@ Overridepublic User load (int id) {System. out. println ("load User"); return null ;}@ Overridepublic void add (User user) {System. out. println ("added" + user );}}
The server layer is similar to the dao layer, which is omitted here ......
UserAction. class
package com.spring.action;import com.spring.model.User;import com.spring.service.IUserService;public class UserAction {private User user;private IUserService userService;private int id;public User getUser() {return user;}public void setUser(User user) {this.user = user;}public IUserService getUserService() {return userService;}public void setUserService(IUserService userService) {this.userService = userService;}public int getId() {return id;}public void setId(int id) {this.id = id;}public void add(){userService.add(user);}public void delete(){userService.delete(id);}public void load(){userService.load(id);}}
TestSpring. calss
Package com. spring. test; import org. junit. test; import org. springframework. beans. factory. beanFactory; import org. springframework. context. support. classPathXmlApplicationContext; import com. spring. action. userAction; import com. spring. model. helloWorld; import com. spring. model. user; public class TestSpring {// create Spring factory private BeanFactory beanFactory = new ClassPathXmlApplicationContext ("beans. xml "); @ Testpublic void testHello () {// obtain the Spring object through the factory // helloWorld in getBean is beans. idHelloWorld hello1 = (HelloWorld) beanFactory in xml. getBean ("helloWorld"); HelloWorld hello2 = beanFactory. getBean ("helloWorld", HelloWorld. class); // at this time, the hello1 object is the System object managed by Spring. out. println (hello1.hello (); // if no scope configuration is made in bean, the default value is singleton. out. println (hello1 = hello2);} @ Testpublic void testUser () {UserAction ua = beanFactory. getBean ("userAction", UserAction. class); User user = new User (1, "My name is spring"); ua. setUser (user); ua. add ();}}
Running result:
Added User [id = 1, username = My name is spring]
Project Structure: