Home » Java » Spring entry configuration example

Spring entry configuration example


 describes the simplest configuration of spring from the landing module,.
1, and the configuration master servlet and servlet-mapping in web.xml, as follows: 




mvc-dispatcher
org.springframework.web.servlet.DispatcherServlet
1


mvc-dispatcher
/


2, configure the spring configuration files that need to be loaded in web.xml, such as applicationContext.xml (simple case, single configuration file is enough), or other names (separated by commas), as follows:




contextConfigLocation
classpath*: *-beans.xml


3, spring MVC has a default spring configuration file, -servlet.xml will automatically load assembly and other configuration files in the file, need to configure to scan and parser, as follows:








In
4, web.xml, the listener responsible for launching the spring container is configured as follows:




org.springframework.web.context.ContextLoaderListener
5, declares the @Controller, @RequestMapping class, processes the request, and returns the login.jsp page, as follows:



@Controller
Public, class, HelloController {
@RequestMapping (value = ` /index.html ')
Public, String, loginPage () {
Return "login"";
}
}
6, username / password, business process layer on the username and password to check whether there is a database, so in the business layer also need to interact with the persistence layer. If the validation fails, it returns the login page, an error. If successful, obtaining user information, set the final visit IP, access time, new landing points, and updates to the database, and here also need persistence layer interaction.


7 establishes domain objects User and LoginLog. The former represents user information, the latter represents log information, and corresponds to data table t_user and t_login_log. domain objects respectively. Serializable interface.

is generally needed

8, according to the characteristics of the MVC model, is generally injected into the business class in the controller class, such as UserService; and in the UserService class, the DAO class is injected into the database to interact with the.


9, first define the interaction with the database layer such as UserDao. DAO, which provides a database query verification method according to the user name and password, username according to the method of obtaining User domain objects, updates to the database are as follows:

.


@Repository
Public class UserDao{
@Autowired
Private JdbcTemplate jdbcTemplate;
Public, int, getMatchCount (String, userName, String, password) {
String SQL = "select, count (*), from, t_user, where, user_name = and, password =?"";
Return, jdbcTemplate.queryForInt (SQL, new, Object[]{userName, password};
}
//todo other methods
}


configures scanning, data sources, and other Bean

in the spring configuration file
10, define the business layer UserService, as follows:


@Service
Public class UserService{
@AutoWired
Private UserDao userDao;
....
}
also requires configuration of scanned packages and transaction managers, and other namespaces in the spring configuration file


11, unit testing,



@RunWith (SpringJUnit4ClassRunner.class)
@ContextConfiguration (locations={"/applicationContext.xml"}) / / start spring container
Public class TestUserService{
@Autowired
Private UserService userService; / / injection in spring container Bean
@Test
Public, void, hasMatchUser () {
Boolean B1 = userService.func ();
AssertTrue (...)
}
}

Latest