JavaEE framework and programming assignments 01: spring entry

1, using the Spring framework:
(1) In Eclipse, create a Web project called chapter01, copy the Spring of four basic packages and commons-logging package of JAR into the lib directory, and posted to the class path, Figure 1-1.
Here Insert Picture Description
Figure 1-1 Import jar package
(2) in the src directory, create a edu.javaee.spring package, and creates an interface UserDao in the package, and then define a say () method in the interface, as shown in the file 1-1 .
File 1-1 UserDao.java

package edu.javaee.spring;

public interface UserDao {
	
	public void say();

}

(3) In edu.javaee.spring package, creating class that implements interface UserDaoImpI UserDao, need to implement the class say () interface method, and write an output statement in the method, such as files 1-2 shown in FIG.
File 1-2 UserDaoImpl.java

package edu.javaee.spring;

public class UserDaoImpl implements UserDao {

	@Override
	public void say() {
		System.out.println("userDao say hello World!");

	}

}

(4) in the src directory, create the applicationContext.xml Spring configuration file, and creates a configuration file - id is one of userDao Bean, such as files 1-3 shown in FIG.
File 1-3 applicationContext.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
        <!-- 将指定类配置给spring,让spring创建其对象的实例 -->
    <bean id="userDao" class="edu.javaee.spring.UserDaoImpl"></bean>
</beans>

In the file 1-3, 2-5 lines of code constraint Spring configuration. This configuration does not require the handwritten information can be found in the Spring of help documentation (see spring-framework-4.3.6.RELEASE \ docs \ spring-framework-reference \ index.html file html folder). Line 7 represents an id for the created userDao Bean instance, the class attribute is used to specify which instantiate class Bean Spring container.
Spring configuration file name can be customized, usually in the actual development, the configuration file will be named applicationContext.xml.
(5) In edu.javaee.spring package to create the test class TestIoc, and write main () method in the class. In the main () method, it is necessary to initialize Spring container, and loading a configuration file, and then acquires userDao instances (i.e. Java object) by the Spring container, say the last call instance () method, such as files 1-4.
File 1-4 TestIoc.java

package edu.javaee.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestIoc {

	public static void main(String[] args) {
		//1,初始化spring容器,加载配置文件
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		//2,通过容器获取userDao实例
		UserDao userDao = (UserDao) applicationContext.getBean("userDao");
		
		//3,调用实例方法
		userDao.say();

	}

}

(6) executing a program, the output of the console shown in Figure 1-2.
Here Insert Picture Description
Figure 1-2 the results of operation
2, the spring following advantages:
(1) non-intrusive design
Spring Framework is a non-invasive (non-invasive), which enables the application code to minimize dependence on the frame.
(2) facilitate decoupling, simplify the development of
Spring is a large factory, you can create all the objects and dependencies of all maintenance work to the Spring container management, greatly reducing the coupling between components.
(3) support AOP
the Spring provides support for AOP, which will allow some common tasks, such as security, transactions, logs and other centralized processing, thereby increasing the reusability of the program.
(4) support for declarative transaction
only need to complete the configuration management of affairs without manual programming.
Test (5) to facilitate the program
Spring provides support for Junit4, you can easily test the program by Spring comment.
(6) to facilitate integration of a variety of excellent framework
Spring does not exclude a variety of excellent open source framework, its interior provides direct support for a variety of excellent frameworks (such as Strutss Hibernate. MyBatiSs Quartz, etc.).
(7) to reduce the difficulty of using the Java EE API of
Spring for some API (such as JDBC, JavaMail, etc.) Java EE development is very difficult to use, it provides packaging so that these API application difficulty greatly reduced.
3, spring Ioc and DI of conceptual understanding
Same dependency injection (Dependency Injection, abbreviated DI) and reverse control (loC) meaning, but this is a concept with two call from two angles described.
(1) Inversion of Control (loC): After using the Spring framework, is no longer an object instance is created by the caller, but created by the Spring container, the relationship between the Spring container will be responsible for the control program, and not by calling by direct control of the program code. Thus, the control is transferred from the container application code to Spring, in control of the inversion, which is the inversion of control Spring.
(2) dependency injection (Dependency Injection, referred to as DI): from the point of view of the Spring container, Spring container will be responsible for dependent objects assigned to the member variables of the caller, which is equivalent to inject an instance of it depends on the caller, this Spring is a dependency injection.

Published an original article · won praise 0 · Views 9

Guess you like

Origin blog.csdn.net/cainiao1007/article/details/104683970