This example presents the basic concept of using in Spring the Annotation based configuration.
The technologies used are :
– Spring 4.3.4
– JDK 1.8
– Maven 3.3.9
You can convert this example to an Eclipse IDE project by going to folder where is the pom.xml is, and use the command :
mvn eclipse:eclipse
package javaspringexamples.spring.ioc.configuration.annotation; /** * * @author mounir.sahrani@gmail.com * */ public class User { private int id; private String fisrtName; private String lastName; private boolean Accountlocked; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getFisrtName() { return fisrtName; } public void setFisrtName(String fisrtName) { this.fisrtName = fisrtName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public boolean isAccountlocked() { return Accountlocked; } public void setAccountlocked(boolean accountlocked) { Accountlocked = accountlocked; } }
package javaspringexamples.spring.ioc.configuration.annotation; /** * * @author mounir.sahrani@gmail.com * */ public interface UserDao { User find(int userId); void insert(User user); void update(User user); void delete(int userId); void lockAccount(int userId, boolean locked); }
package javaspringexamples.spring.ioc.configuration.annotation; import java.util.HashMap; import java.util.Map; import org.springframework.stereotype.Repository; /** * * @author mounir.sahrani@gmail.com * */ @Repository public class UserDaoInMemoryImpl implements UserDao { private Map<Integer, User> usersMap = new HashMap<>(); { User user1 = new User(); user1.setId(1); user1.setFisrtName("FirstName 1"); user1.setLastName("LastName 1"); user1.setAccountlocked(false); User user2 = new User(); user2.setId(2); user2.setFisrtName("FirstName 2"); user2.setLastName("LastName 2"); user2.setAccountlocked(false); usersMap.put(user1.getId(), user1); usersMap.put(user2.getId(), user2); } @Override public void insert(User user) { usersMap.put(user.getId(), user); } @Override public void update(User user) { usersMap.put(user.getId(), user); } @Override public void delete(int userId) { usersMap.remove(userId); } @Override public User find(int userId) { return usersMap.get(userId); } @Override public void lockAccount(int userId, boolean locked) { User user = usersMap.get(userId); user.setAccountlocked(locked); update(user); } }
package javaspringexamples.spring.ioc.configuration.annotation; /** * * @author mounir.sahrani@gmail.com * */ public interface UserService { String getUserinformation(int userId); void lockAccountUser(int userId); void unlockAccountUser(int userId); }
package javaspringexamples.spring.ioc.configuration.annotation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * * @author mounir.sahrani@gmail.com * */ @Service public class UserServiceImpl implements UserService { private UserDao userDao; @Autowired public void setUserDao(UserDao userDao) { this.userDao = userDao; } @Override public void lockAccountUser(int userId) { userDao.lockAccount(userId, true); } @Override public void unlockAccountUser(int userId) { userDao.lockAccount(userId, false); } @Override public String getUserinformation(int userId) { User user = userDao.find(userId); return user.getFisrtName() + " - " + user.getLastName() + " - " + user.isAccountlocked(); } }
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <context:component-scan base-package="javaspringexamples.spring.ioc.configuration.annotation"/> </beans>
package javaspringexamples.spring.ioc.configuration.annotation; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * * @author mounir.sahrani@gmail.com * */ public class Main { public static void main(String[] args) { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext( "/javaspringexamples/spring/ioc/configuration/annotation/configuration.xml"); UserService userService = applicationContext.getBean(UserService.class); System.out.println("User1 Informations: " + userService.getUserinformation(1)); System.out.println("User2 Informations: " + userService.getUserinformation(2)); System.out.println("Locking User1 account"); userService.lockAccountUser(1); System.out.println("User1 Informations: " + userService.getUserinformation(1)); } }
Get the sources of the example from the following GitHub url