Annotation Implementation of IOC in Spring, iocannotation
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:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
Configure Spring's Annotation configuration in XML:
<! -- Enable Spring's Annotation support --> <context: annotation-config/> <! -- Set which packages Spring uses to find Annotation --> <context: component-scan base-package = "com. spring"/>
Complete XML configuration:
<? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: context = "http://www.springframework.org/schema/context" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-conte Xt-3.2.xsd "> <! -- Enable Spring's Annotation support --> <context: annotation-config/> <! -- Set which packages Spring uses to find Annotation --> <context: component-scan base-package = "com. spring"/> </beans>
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; // equals to the completion of <bean id = "userDao" class = "com. spring. dao. userDao "/> // @ Component (" userDao ") // @ Component public create bean Annotation @ Repository (" userDao ") // @ 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:
Java programming. How ioc and aop in spring are implemented
It is cool to use java reflection technology;
Based on the given parameters (mainly strings)
Create a bean and access the corresponding method or constructor and member variables;
For more information about the application, see Constructor, Method, Field/java. lang. Class in the java. lang. reflect package.
What design mode is used for spring Ioc?
AOP Aspect-Oriented Programming
Encapsulate the cross-business logic (such as security, logs, and transactions) in a program into a plane and inject it into the target object (specific business logic.
For example:
Many methods may throw an exception. to record this exception to the log, you can write an interceptor class to record the log in this class. In spring. configure an aop interceptor for these methods to record logs in xml
After this method is executed, the interceptor is called to record logs. In this way, you do not need to manually record logs every time an exception is thrown.
Spring transaction management uses aop.
This can also improve the cohesion of the program.
In addition, the Group IDC has many online product groups, which are cheap and well-known.