Implementation of springioc's first classroom case

Source: Internet
Author: User

 I,
DefinitionGreetingserviceInterface

PackageCN. csdn. hr. Service;

PublicInterfaceGreetingservice {

PublicVoidSaygeeting ();

}

 

II,
DefinitionGreetingserviceimplClass and implementGreetingserviceInterface

PackageCN. csdn. hr. Service;

PublicClassGeetingserviceImplImplements
Greetingservice {

PrivateString
Greeting;

 

/* BeanConfiguration FilePropertyAttributeName
AndGreetingConsistent, automatically passedSetMethod Injection*/

//Dependency Injection

PublicVoidSetgreeting (string greeting ){

This.Greeting= Greeting;

}

PublicVoidSaygeeting (){

System.Out. Println (Greeting);

}

}

 

III,
DefinitionApplicationcontext. 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-2.5.xsd"
>

 <Bean
ID="Greetingservicebean" 
Class="Cn. csdn. hr. Service. geetingserviceImpl"
Scope="Singleton">

<! --
PropertybeanAttribute Value in-->

<Property
Name="Greeting">

<Value>Hi!!</Value>

</Property> 

</Bean>

</Beans>

IV,
Test

Package CN. csdn. hr. JUnit;

Import org. JUnit. test;

Import org. springframework. Beans. Factory. beanfactory;

Import org. springframework. Beans. Factory. xml. xmlbeanfactory;

Import org. springframework. Context. applicationcontext;

Import org. springframework. Context. Support. classpathxmlapplicationcontext;

Import org. springframework. Context. Support. filesystemxmlapplicationcontext;

Import org. springframework. Core. Io. classpathresource;

Import org. springframework. Core. Io. filesystemresource;

Import org. springframework. Core. Io. resource;

Import CN. csdn. hr. Service. geetingservicebean;

Import CN. csdn. hr. Service. greetingservice;

Public class appmain {

@ Test

Public void test (){

//AssemblyBean

Applicationcontext context = new filesystemxmlapplicationcontext ("D: \ Users \ Administrator \ workspace \ day \ SRC \ applicationcontext. xml ");

//CreateBeanfacoryFactory

//ObtainBeanfactoryCreated by the factoryBeanObject

Greetingservice = (greetingservice) Context. getbean ("greetingservicebean"); // XMLResource fileClassbean

//UseBeanInstance

Greetingservice. saygeeting ();


}

@ Test

Public void test1 (){

//AssemblyBean

//Create a dependent resource file

Applicationcontext = new classpathxmlapplicationcontext ("applicationcontext. xml ");

//ObtainBeanfactoryCreated by the factoryBeanObject

Greetingservice = (greetingservice) applicationcontext. getbean ("greetingservicebean"); // XMLResource fileClassbeanOfIDName

//UseBeanInstance

Greetingservice. saygeeting ();

}

}

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.