Annotation Implementation of IOC in Spring

Source: Internet
Author: User

Annotation Implementation of IOC in Spring

Annotation Implementation of IOC in Spring:

Note the annotations and text descriptions in each class to better understand the Annotation Implementation of IOC in Spring!

First, the schema of context needs to be introduced in beans. xml:

 
 

Configure Spring's Annotation configuration in XML:

 
 
 
 

Complete XML configuration:
 
 
  
  
  
  
 

User entity 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 interface:
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 org. springframework. stereotype. Component; import org. springframework. stereotype. Repository; import com. spring. model. User; // equal
 // @ Component ("userDao") // @ Annotation @ Repository ("userDao") of the bean created by Component ") // @ Repository indicates the injection of 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 );}}

IUserService interface:
package com.spring.service;import com.spring.model.User;public interface IUserService {public void add(User user);public void delete(int id);public User load(int id);}

UserService class:
Package com. spring. service; import javax. annotation. resource; import org. springframework. stereotype. component; import org. springframework. stereotype. service; import com. spring. dao. IUserDao; import com. spring. model. user; // @ Component (value = "userService") @ Service ("userService") // The business layer uses @ Service to inject public class UserService implements IUserService {private IUserDao userDao; public IUserDao getUserDao () {return userDao;} // injection by name by default. @ Inject is provided in JSR330 to Inject @ Resourcepublic void setUserDao (IUserDao userDao) {this. userDao = userDao;} @ Overridepublic void add (User user) {userDao. add (user) ;}@ Overridepublic void delete (int id) {userDao. delete (id) ;}@ Overridepublic User load (int id) {return userDao. load (id );}}

UserAction class:
Package com. spring. action; import javax. annotation. resource; import org. springframework. context. annotation. scope; import org. springframework. stereotype. component; import org. springframework. stereotype. controller; import com. spring. model. user; import com. spring. service. IUserService; // @ Component ("userAction") @ Controller ("userAction") // The MVC control layer generally uses @ Controller @ Scope ("prototype ") // set multiple examples of public class UserAction {private User user User; private IUserService userService; private int id; public user getUser () {return User;} public void setUser (user User user) {this. user = user;} public IUserService getUserService () {return userService;} @ Resourcepublic 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 );}}

Test class:
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. user; public class TestSpring {// create Spring factory private BeanFactory beanFactory = new ClassPathXmlApplicationContext ("beans. xml "); @ Testpublic void testUser () {UserAction ua = beanFactory. getBean ("userAction", UserAction. class); User user = new User (1, "My name is spring"); ua. setUser (user); ua. add (); // add the same content for a single instance. add multiple instances as nullUserAction ua1 = beanFactory. getBean ("userAction", UserAction. class); ua1.add ();}}

Running result and project result diagram:



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.