Spring class constructor creates Bean

Source: Internet
Author: User

I. Create an Interface

 

Public interface personservice {<br/> Public void save (); <br/>}

 

Ii. Create an implementation class

Public class personservicebean implements personservice {<br/> Public void save () {<br/> system. Out. println ("Save method is running !. "); <Br/>}< br/>}

 

 

3. Create a test class

Import Org. JUnit. beforeclass; <br/> Import Org. JUnit. test; <br/> Import Org. springframework. context. applicationcontext; <br/> Import Org. springframework. context. support. classpathxmlapplicationcontext; <br/> public class personservicebeantest {<br/> @ beforeclass <br/> Public static void setupbeforeclass () throws exception {</P> <p >}</P> <p> @ test <br/> Public void instancespring () {<br/> applicationcontext context = new classpathxmlapplicationcontext ("beans. XML "); <br/> personservice = (personservice) context. getbean ("personservice"); <br/> personservice. save (); <br/>}< br/>} 

 

 

4. Beans. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <beans xmlns = "http://www.springframework.org/schema/beans" <br/> xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" <br/> xsi: schemalocation = "http://www.springframework.org/schema/beans <br/> http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <br/> <bean id = "personservice" class = "com. test. spring. test. personservicebean "> </bean> <br/> </beans>

 

 

V. Description

I think the bean in spring is to configure the class to be called to implement the corresponding interface. Before using spring, we create it manually, for example: interfacename INAME = new interfacenameimplement (); after spring is used, we will write this guy in the 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.