Getting started with the 1.Spring framework

Source: Internet
Author: User
Tags log4j

**spring Framework's IOC core feature QuickStart (must master the development steps) **0. What is the function of the IOC? * ioc--inverse of control, inversion, reverse the creation of objects to spring!! * Use IOC to solve the problem of high coupling of programs!! 1. Step one: Download the Spring Framework development Package * The download path is: http://repo.spring.io/webapp/search/artifact/?0&q=spring-framework* the directory structure following the decompression * docs--API and Development specification * libs--JAR package and Source * Spring Framework's jar package is characterized by a jar package with 3 (using Jar package, document jar package and source code jar package) * schema--constraint 2. Step Two: Create the Javaweb project, introduce spring's development package * Introduce the Spring framework IOC core functionality required by the specific JAR package * Spring Framework IOC features, then according to the spring framework of the architecture diagram can see, only need to introduce the following jar package * Under spring-framework-3.2.0.release/libs/* beans* core* context* Expression language* Spring Framework also requires the introduction of log-related jar packages * In spring-framework-3.0.2.release-dependencies/org.apache.commons/com.springsource.org.apache.commons.logging/ 1.1.1* com.springsource.org.apache.commons.logging-1.1.1.jar* also needs to introduce LOG4J's jar package spring-framework-3.0.2.release-dependencies\org.apache.log4j\com.springsource.org.apache.log4j\1.2.15* Com.springsource.org.apache.log4j-1.2.15.jar3. Step three: Create the corresponding package structure, write Java classes, note: The future use of spring framework for development, all need to write interface and implementation Class!! * Com.itcast.demo1* userservice--interface * userserviceimpl--Specific implementation Class 4. Step four: To create the Userserviceimpl implementation class to the spring framework to manage, you need to create a spring framework configuration file, complete configuration * 1. In the SRC directory to create the Applicationcontext.xml configuration file, the name can be arbitrary, but will generally use the default name!! * 2. To introduce the constraints of spring, we need to find the specific constraint header information first!! * spring-framework-3.2.0.release\docs\spring-framework-reference\html\xsd-config.html* specific constraints are as follows: <beans xmlns= " Http://www.springframework.org/schema/beans "xmlns:xsi=" Http://www.w3.org/2001/XMLSchema-instance "Xsi:schem alocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsd "></beans>* 3. Complete UserService Configuration <!--Spring QuickStart--><bean id= "UserService" class= "Com.itcast.demo1.UserServiceImpl"/> 5. Step five: Write a test program, using the Spring Framework factory way to get to the UserService interface specific implementation Class!! * The specific code is as follows: public void Demo2 () {//factory using spring: applicationcontext ApplicationContext = new Classpathxmlapplicationcontext ("Applicationcontext.xml");//factory-acquired class: UserService UserService = (userservice) Applicationcontext.getbean ("UserService"); Userservice.sayhello ();

Applicationcontext.xml:

<?xml version= "1.0" encoding= "UTF-8"?><!--introduce schema constraints--><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 "><!--need to configure a specific bean--    <bean id=" UserService "class=" demo1. Userserviceimpl "></bean>    <bean id=" UserService2 "class=" demo1. USERSERVICEIMPL2 "></bean></beans>

UserService:

Package Demo1;public interface UserService {public void Save ();}

Userserviceimpl:

Package Demo1;public class Userserviceimpl implements Userservice{public void Save () {System.out.println (" Userserviceimpl executed ");}}

SpringDemo1: (Test)

Package Demo1;import Org.junit.test;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;public class SpringDemo1 {/** * traditional way */@ testpublic void Run1 () {///Traditional way UserService us=new Userserviceimpl (); Us.save ();} /** * Using Spring's IOC */@Testpublic void run2 () {//Get Spring Factory object ApplicationContext ac=new Classpathxmlapplicationcontext ("Applicationcontext.xml");//Gets the specific Bean instance object from the factory, the parameter is the ID value in the configuration file userservice US = ( UserService) Ac.getbean ("UserService"); Us.save ();}}

  

Getting started with the 1.Spring framework

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.