Getting Started with spring

Source: Internet
Author: User

First, before Spring was born, there was also a container called EJB now some companies are still using this container. This container is heavyweight and highly intrusive, so-called intrusion is the need to inherit or implement the specified classes and interfaces, and it is very expensive to sell.

Spring is a lightweight IOC and AOP container framework:

A, lightweight: the implementation of the program is not very complex, the code is not many, the use of resources is not many, no intrusion, (usually does not require the inheritance of specific classes or implementation of specific interfaces);

B, IoC (inversion of control inversion): Object creation responsibility reversal (focus, core); Dependency injection (DI) dependency injection;

C, AOP (Aspect oriented programming): A way of thinking for cross-sectional programming, which can be expanded in function, and oriented to cross-sectional (AOP) programming;

D, container: Can hold the object, and can control the life cycle of the object;

Second, the use of steps

1, create a Java project, and create a Lib file in the project, the Spring jar package and spring-dependent jar package into the Lib file, select all Jar package right mouse button build Path; (note: Spring's jar packages and spring-dependent jar packages can be downloaded to the official website .) )

2. Right click on Project Engineering--myeclipse--project Facets--install Spring facet--add spring capabilites ..., create write the core configuration file of the framework, stored in the SRC directory: applicationcontext.xml;

<?xml version= "1.0" encoding= "UTF-8"? ><beansxmlns= "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.1.xsd "></beans>

Third, setter injection

This is the simplest way to inject.
1, first create a good three classes: User,
User1, User2
Package Cn.com.beans;public class User1 {public void Ceshi1 () {System.out.println ("User1 Ceshi1 Method!) ");}}

  

Package Cn.com.beans;public class User2 {public void Ceshi2 () {System.out.println ("User2 Ceshi2 Method!) ");}}

  

Package Cn.com.beans;public class User {private User1 user1;  User1 class private User2 user2; User2 class
Get,set method for creating properties Public User1 GetUser1 () {return user1;} public void SetUser1 (User1 user1) {this.user1 = user1;} Public User2 GetUser2 () {return user2;} public void SetUser2 (User2 user2) {this.user2 = User2;} public void Ceshi () {user1.ceshi1 (); User2.ceshi2 (); System.out.println ("User's Ceshi Method! ");}}

2. Inject in config file applicationcontext.xml

<!--configuration Bean, which is configured with spring Management-<bean id= "user" class= "cn.com.beans.User" autowire= "Bytype" ></bean> <!--(1) Dependency injection, configuring the corresponding property in the current class--<property name= "Springdao" ref= "Springdao" > </property> </be An>
<?xml version= "1.0" encoding= "UTF-8"? ><beansxmlns= "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/beanshttp://www.springframework.org/schema/beans/ Spring-beans-3.1.xsd "><!--inject entity classes into the Spring container, class is the full path of the entity class--><bean id=" user "class=" Cn.com.beans.User " Autowire= "Bytype" ></bean><bean id= "user1" class= "Cn.com.beans.User1" ></bean><bean id= " User2 "class=" Cn.com.beans.User2 "></bean></beans>

3. Write Test class

Package Cn.com.beans;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;public class Test {public static void main (String [] args) {//new A Classpathxmlapplicationcontext object, negative to ctxapplicationcontext CTX = new Classpathxmlapplicationcontext ("Applicationcontext.xml");//Call the Getbean () method, User.class is the mapping of the entity class, user is the Iduser user=ctx.getbean (user.class, "user") of the injected class in applicationcontext.xml;//Call Ceshi () in the entity class Method User.ceshi ();}}

4. Test results

Getting Started with spring

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.