The spring framework of Java Beauty [from rookie to master evolution]

Source: Internet
Author: User
Tags aop

New Lightweight Java Open source Framework---Spring

Author: Egg

Micro Blog: HTTP://WEIBO.COM/XTFGGEF

Source: http://blog.csdn.net/zhangerqing

Spring is a lightweight Java framework whose core idea is Di (Dependency injection, Dependency injection) and the IOC (inversion control, which controls inversion), because of its open source, low intrusion, It has now swept a large part of the market, and its biggest competitor is the Java EE Framework EJB. EJB3.0 previously, because of its bulky and cumbersome, was abandoned by people, replaced by spring, but Spring has its limitations, that is, spring on the distributed support is not good, but the EJB is very advantageous in this area, and the current EJB3.0 simplifies the operation, the use is no longer cumbersome, further enhance the competitiveness 。 So, a lot of the current lightweight javaweb projects are used by spring, and we need to learn it well. This chapter is the first of the spring framework of the Java beauty [from rookie to master evolution] series , and I hope that you and I can revisit the basics of spring and make valuable comments and suggestions. This study uses the Spring4.1 version. Modules



From this picture we can see that the overall architecture of spring, mainly divided into six major modules core Container cores are divided into 4 blocks, Spring-core, Spring-beans, Spring-context, Spring-expression. Core and Bean are the cores of the entire framework, providing the basic DI and IOC capabilities. The context is built on top of the core and beans modules and provides a way to manipulate objects in a way that is similar to Jndi. The context module inherits its functionality from the Beans module while increasing internationalization support, such as resource bindings, while the context module also supports Java EE features such as EJB,JMX and basic remote invocation. The ApplicationContext interface is the focus of the context module. Expression is a powerful expression language that supports querying and manipulating object properties at run time, and we'll take some examples in later articles to illustrate the use of spring expression language. AOP and Instrumentation

AOP modules provide the implementation of aspect-oriented programming, and ASPECTJ integration. Messaging

Messaging is a newly added module of Spring4, which contains some of the main implementations of the message based applications. Data access/integration

Data access, as its name suggests, is the support that spring provides to the data tier and is a more functional module. Provides a series of implementations including JDBC, things, ORM,JMS, etc. Web

Web modules primarily provide web-oriented implementations such as multiple file uploads, servlet listeners, and spring MVC support. Test

The test module does a variety of tests for each module of spring, including unit testing, integration testing, and so on.


On the specific explanations and functions of each module we will refer to it in a later article, and also invite interested readers to consult the spring official documentation for more detailed instructions.


First Spring applet

Create a new 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 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"
Com.adam.java.spring.dao.UserDao ">
	<property name=" user "ref=" user "/>
</bean>
</ Beans>

The test class is executed and the following output is obtained:

Add from Userdao
tostring from user


Related Article

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.