Spring Learning Path (i) Spring introduction

Source: Internet
Author: User

1. Introduction of JAR Package

Spring Core jar Package
Spring-beans-4.3.2.release.jar
Spring-core-4.3.2.release.jar
Spring-context-4.3.2.release.jar
Spring-expression-4.3.2.release.jar
Spring Log jar Package
Log4j-1.2.17.jar
Commons-logging-1.1.3.jar
Spring Annotations jar Package
Spring-aop-4.3.2.release.jar
Spring AOP Operations
Aopalliance-1.0.jar
Aspectjweaver-1.8.7.jar
Spring-aop-4.3.2.release.jar
Spring-aspects-4.3.2.release.jar
log File
log4j.properties-"placed under SRC
Spring-to-database operations
Spring-tx-4.3.2.release.jar
Spring-jdbc-4.3.2.release.jar
c3p0 Connection Pool
C3p0-0.9.2.1.jar
Mchange-commons-java-0.2.3.4.jar

2. Creating classes, writing methods in classes

 Public class User {    publicvoid  Add () {        System.out.println ("Add----------");    } // Public     static void Main (string[] args) {////         Original Practice //          User user = new user (); //         User.add (); //     }}

3. Configure the spring file, configure the class file

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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-bean S.xsd "><!--Getting started with IOC -    <!--Bean Tag id attribute: A custom value; class: The path to the classes when getting the value of the created object -    <BeanID= "User"class= "Com.ioc.User"></Bean></Beans>

4. Write code debugging test object creation

 PackageCom.junit;Import Staticorg.junit.assert.*;Importorg.junit.Test;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;ImportCom.ioc.User; Public classText {@Test Public voidTest () {//load Spring configuration fileApplicationContext context =NewClasspathxmlapplicationcontext ("Bean1.xml");//gets the object created by the configuration object user is the value of the bean tag id attribute in the spring configuration fileUser user = (user) Context.getbean ("user"));//gets to the User class, and executes the Add methodUser.add (); }}

Three ways to instantiate a bean

1, using the non-parametric construction method of the class to create (focus)-"above case;"

2. Create with Static factory

 PackageCom.ioc;Importorg.junit.Test;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;//class Public classbean2 { Public voidAdd () {System.out.println ("Add--------------"); }}//Static Factory Public classFactory {//returning a Bean object using a static method     Public Staticbean2 getBean2 () {return Newbean2 (); }}//Test@Test Public voidTest () {//load Spring configuration fileApplicationContext context =NewClasspathxmlapplicationcontext ("Bean1.xml");//get the object created by the configuration objectBean2 bean2 = (bean2) context.getbean ("Bean2"); System.out.println (bean2);}
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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 "><!--Getting started with IOC -    <!--Bean Tag id attribute: A custom value; class: The path to the classes when getting the value of the created object -    <BeanID= "User"class= "Com.ioc.User"></Bean>    <!--Destroy-method: Pointer to a static method name in Factory -    <BeanID= "Bean2"class= "Com.factory.Factory"Factory-method= "GetBean2"></Bean></Beans>
configuration file

3. Create with Instance factory

 PackageCom.ioc;Importorg.junit.Test;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;//class Public classbean2 { Public voidAdd () {System.out.println ("Add--------------"); }}//Instance factory class Public classFactory {//Common Methods     Publicbean2 getBean2 () {return Newbean2 (); }}/**Profile <!--instance Factory--<bean id= "bean2factory" class= "Com.factory.Factory" ></bean> <bean Id= "bean2" factory-bean= "Bean2factory" factory-method= "getBean2" ></bean>*///Test@Test Public voidTest () {//load Spring configuration fileApplicationContext context =NewClasspathxmlapplicationcontext ("Bean1.xml");//get the object created by the configuration objectBean2 bean2 = (bean2) context.getbean ("Bean2"); System.out.println (bean2);}

Spring Learning Path (i) Spring introduction

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.