The beauty of Java [from cainiao to masters]: spring framework's first Knowledge
A lightweight Java open-source framework --- Spring
Author: egg
Weibo: http://weibo.com/xtfggef
Source: http://blog.csdn.net/zhangerqing
Spring is a lightweight Java framework. Its core idea is DI (Dependency Injection, that is, Dependency Injection) and IoC (Inversion of Control, that is, Control Inversion, it has swept a large part of the market, and its biggest competitor is the JavaEE framework EJB. Before EJB3.0, due to its heavy lifting and cumbersome use, it was abandoned and replaced by spring. However, spring also has its limitations, that is, spring has poor support for distributed systems, however, EJB3.0 is very advantageous in this regard, and it simplifies operations, so it is no longer cumbersome to use, and its competitiveness is further improved. Therefore, many of the current lightweight Java Web projects use spring. We need to learn it well! This chapterThe beauty of Java [from cainiao to masters] SeriesI hope everyone will review the basic knowledge of spring and provide valuable comments and suggestions with me! This course uses Spring4.1.
Modules
From this figure, we can see that the overall architecture of Spring is mainly divided into six modules.
Core iner consists of four core parts: spring-Core, spring-beans, spring-context, and spring-expression. core and bean are the core of the entire framework and provide basic DI and IoC functions. Context is built on the core and beans modules, providing a method similar to JNDI and using a framework to operate objects. The Context module inherits its functions from the beans module and provides international support, such as resource binding. At the same time, the Context module also supports JavaEE functions, such as EJB, JMX, and Basic remote calls. The ApplicationContext interface is the focus of the context module. Expression is a powerful expression language that supports querying and manipulating the attributes of objects at runtime. We will give some examples in the following articles to illustrate the usage of spring expression language. AOP and instrumentation
The Aop module provides the implementation of Aspect-Oriented Programming and is integrated with AspectJ.
Messaging
Messaging is a newly added module in spring4, which includes some of the main implementation of message-based applications.
Data Access/Integration
Data access, as its name implies, is spring's support for the Data layer and a rich array of functions. It provides a series of implementations including JDBC, things, ORM, and JMS.
Web
The Web module provides web-oriented implementations, such as Multifile upload, servlet listeners, and spring mvc.
Test
The Test module performs various tests on various modules of spring, including unit tests and integration tests.
We will mention the specific explanations and functions of each module in the subsequent articles. Interested readers are also advised to go to the official spring documents for more detailed instructions.
The first Spring Applet
Create a User class, a UserDao class, and a test class:
User. java
package com.adam.java.spring.po;public class User {private String name;private int age;private int score;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public int getScore() {return score;}public void setScore(int score) {this.score = score;}public String toString(){return "tostring from user";}}
UserDao. java
package com.adam.java.spring.dao;import com.adam.java.spring.po.User;public class UserDao {private User user;public void add(){System.out.println("add from userdao");System.out.println(user.toString());}public User getUser() {return user;}public void setUser(User user) {this.user = user;}}
DITest. java
package com.adam.java.spring;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.adam.java.spring.dao.UserDao;public class DITest {@SuppressWarnings("resource")public static void main(String[] args) {ApplicationContext atx = new ClassPathXmlApplicationContext("beans.xml");UserDao userDao = (UserDao) atx.getBean("userDao");userDao.add();}}
A configuration file named 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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="user" class="com.adam.java.spring.po.User"/><bean id="userDao" class="com.adam.java.spring.dao.UserDao"><property name="user" ref="user"/></bean></beans>
Run the test class to get the following output:
add from userdaotostring from user