Spring Framework5.0 Learning (3) Three forms of--spring configuration file

Source: Internet
Author: User

The Spring Framework is the practice of the IOC (inversion of control inversion) principle. IoC is also known as Dependency Injection (DI dependency injection).

Org.springframework.beans and org.springframework.context Two packages implement the IOC container. BeanFactory接口的子接口 ApplicationContext defines the basic functionality of the container. If the Web app is using the WebApplicationContext .

The function of this container is to parse and instantiate the Pojo relationship defined in the configuration file and apply the business system.

Pojo:plain old Java Object.

The spring configuration file has the following three forms:

    • The traditional XML
    • New annotation configuration after Spring 2.5
    • Spring 3.0 New Javaconfig
1.0 Legacy XML Configuration:

Build.gradle

 Apply plugin: ' java ' apply plugin:  ' idea ' //   Mainclassname is a property of application, otherwise it will be an error  Apply plugin: ' Application ' mainclassname  = ' Xmlconfig.helloworld '  sourcecompatibility  = 1.8targetcompatibility  = 1.8repositories {mavencentral ()}dependencies {compile " joda-time:joda-time:2.2 " compile  ' org.springframework:spring-context:5.0.0.release ' }  Span style= "COLOR: #008000" >//  The name and version of the jar package generated by the project, such as Gs-gradle-0.1.0.jar  Span style= "COLOR: #000000" >jar {baseName  = ' gs-gradle '  version  = ' 0.1. 0 ' }  

Create Helloworld.xml in the Src/main/resources directory, this file name is optional

<?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 ">    <BeanID= "HelloWorld"name= "HelloWorld2"class= "Xmlconfig.helloworld"/></Beans>
Helloworld.java
 PackageXmlconfig;ImportOrg.springframework.beans.factory.xml.XmlBeanDefinitionReader;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;ImportOrg.springframework.context.support.GenericApplicationContext;/*** Created by sheting on 10/22/2017*/ Public classHelloWorld { Public voidSayHello () {System.out.println ("Hello World"); }     Public Static voidMain (string[] args) {ApplicationContext context=NewClasspathxmlapplicationcontext ("Helloworld.xml"); HelloWorld helloWorld2= Context.getbean ("HelloWorld2", HelloWorld.class);        Helloworld2.sayhello (); HelloWorld HelloWorld= Context.getbean ("HelloWorld", HelloWorld.class);        Helloworld.sayhello (); Genericapplicationcontext context2=NewGenericapplicationcontext (); NewXmlbeandefinitionreader (CONTEXT2). Loadbeandefinitions ("Helloworld.xml");        Context2.refresh (); HelloWorld Context2bean= Context2.getbean ("HelloWorld", HelloWorld.class);    Context2bean.sayhello (); }}

Operation Result:

2.0 Annotation Configuration

Gradle.build

Apply plugin: ' java 'idea '//  Mainclassname is a property of application, otherwise it will be an error to Apply plugin: ' Application '= ' annotationconfig.test '= 1.8= 1.8Repositories {    mavencentral ()} dependencies {    "joda-time:joda-time:2.2"    ' org.springframework:spring-context:5.0.0. Release '}//  the name and version of the jar package generated by the project, such as Gs-gradle-0.1.0.jarjar {    = ' Gs-gradle '    =  ' 0.1.0 '}
Helloworld.java
 PackageAnnotationconfig;Importorg.springframework.stereotype.Component;/*** Created by sheting on 10/22/2017*//*** If the attribute name is Value,value can be omitted. * If you do not specify value, the default value is the class name first letter becomes lowercase. * @Component (value= "Beanid") is the instantiation of the current class. Equivalent to <bean id= "Beanid" >*/@Component Public classHelloWorld { Public voidSayHello () {System.out.println ("Hello World"); }}

Test.java

 PackageAnnotationconfig;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;/*** Created by sheting on 10/22/2017*/ Public classTest { Public Static voidMain (string[] args) {ApplicationContext context=NewClasspathxmlapplicationcontext ("Helloworld.xml"); HelloWorld HelloWorld= Context.getbean ("HelloWorld", HelloWorld.class);    Helloworld.sayhello (); }}

Helloworld.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:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans. XSD Http://www.springframework.org/schema/context http://www.springframework.org/schema/cont Ext/spring-context.xsd ">

<!--open annotation Scan--<Context:component-scanBase-package= "Annotationconfig"/></Beans>

Operation Result:

3.0 Java Config

Gradle.build

Apply plugin: ' java 'idea '//  Mainclassname is a property of application, otherwise it will be an error to Apply plugin: ' Application '= ' javaconfig.test '= 1.8= 1.8Repositories {    mavencentral ()} dependencies {    "joda-time:joda-time:2.2"    ' org.springframework:spring-context:5.0.0. Release '}//  the name and version of the jar package generated by the project, such as Gs-gradle-0.1.0.jarjar {    = ' Gs-gradle '    =  ' 0.1.0 '}
Myservice.java
 Package Javaconfig; /**  */Publicinterface  myservice    {void  SayHello ();}
Myserviceimpl.java
 Package Javaconfig; /**  */Publicclassimplements  myservice    {public void SayHello () {        System.out.println ("Hello World");}    }
Appconfig.java
 PackageJavaconfig;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Configuration;/*** Created by sheting on 10/22/2017 * * This class is similar to the following configuration * <beans> * <bean id= "MyService" class= "Com.acme.serv Ices. Myserviceimpl "/> * </beans> * * Java configuration is implemented through @configuration and @bean. * @Configuartion declares that the current class is a configuration class, equivalent to the <beans></beans> of a spring-configured XML file. * @Bean annotations on a method, declares that the return value of the current method is a Bean,bean name for the method name. * @ComponentScan automatically scan all @Service in the package name @Component @Repository @Controller class and register as a bean. * */@Configuration Public classAppConfig {@Bean PublicMyService MyService () {return NewMyserviceimpl (); }}

Test.java

 PackageJavaconfig;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.annotation.AnnotationConfigApplicationContext;/*** Created by sheting on 10/22/2017*/ Public classTest { Public Static voidMain (string[] args) {//Load ConfigurationApplicationContext CTX =NewAnnotationconfigapplicationcontext (AppConfig.class); MyService MyService= Ctx.getbean (MyService.class);    Myservice.sayhello (); }}

Operation Result:

Spring Framework5.0 Learning (3) Three forms of--spring configuration file

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.