Spring Study Notes (1) Environment establishment and test and development environment

Source: Internet
Author: User
Tags xml parser

Integrated development with Myeclipse 8.5 is available in its integrated environment.

Select a directory and click MyEclipse --> Add Spring Capabilities on the menu bar.

In the new window, select the Spring version to be integrated (3.0 here). If you want to integrate Struts and hibernate, you can select Spring 3.0 AOP Libraries, Spring 3.0 Core Libraries, Srping 3.0 Persistence Core Libraries, Spring 3.0 Persistence JDBC Libraries, and Spring 3.0 Web Libraries in the selection box.

In the JAR Library Installation column, select Copy checked Library contents to project folder and click Next>. On the Next page, select Specify new or existing Spring bean configuration file? Click Finish. Then, MyEclipse generates an applicationContext. xml file under the src directory. That is, the Spring configuration file.

After the development environment is set up, you can write test classes.

First, a New interface UsersDao:

package com.lz.service;

public interface UsersDao {
public void save();
}

Then, a New implementation interface of UsersDao is provided:

Package com. lz. service. imp;

Import com. lz. service. UsersDao;

Public class UserImpDao implements UsersDao {

Public void save (){
System. out. println ("development environment test successful! ");
}
}

Configure applicationContext. xml in the Spring configuration file:

Add the bean class to be handed over to Sping management to the applicationContext. xml file.

<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="userImpDao" class="com.lz.service.imp.UserImpDao"></bean>
</beans>

Note: The difference between id and name in the bean attribute: they are all names of the current bean. Why do IDS provide a name attribute, because id itself is an attribute of xml, it will be verified by the xml parser. In this process, it cannot contain any special characters such :"/", however, in some cases, we need to use some special characters for the bean name. At this time, we need to use the name attribute.

The basic work has been completed. Now we can test Spring.

First, a New tool for testing JUnit Test Case:

I am using JUnit 4.

Right-click the project --> New --> Other --> JUnit/JUnit Test Case. In the New panel, select New JUnit 4 Test --> Finish.

First instantiate the Spring container. Generally, there are two ways to instantiate the Sping container:

1. Search for the configuration file in the class path to instantiate the container

ApplicationContext act = new ClassPathXmlApplicationContext(String[]{"applicationContext.xml"});

2. Search for a configuration file in the file system directory to instantiate the container.

ApplicationContext act = new ClassPathXmlApplicationContext(String[]{"d:\\applicationContext.xml"});

Multiple Spring configuration files can be specified and passed in through arrays.

Write in the test tool class:

package com.lz.test;

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.lz.service.UsersDao;


public class UserTest {
private static UsersDao usersDao;

@BeforeClass
public static void setUpBeforeClass() throws Exception {
ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
usersDao =(UsersDao)act.getBean("userImpDao");
}

@Test
public void test(){
usersDao.save();
}
}

In the Outline view, right-click the test method --> Run As --> JUnit Test.

In this case, if "the development environment test is successful" is output on the console !" You can.

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.