Notes on Spring basic configuration in Java

Source: Internet
Author: User

This article will introduce you to a learning note on Spring basic configuration in Java. I hope it will be helpful to you.


1. Download spring framework http://www.springsource.org/download/community

2. Copy the packages under the spring lib directory to the lib of our application.

Commons-logging.jar
Spring-beans-4.0.0.M1.jar
Spring-context-4.0.0.M1.jar
Spring-core-4.0.0.M1.jar
Spring-expression-4.0.0.M1.jar


3. Create a bean class

The Code is as follows: Copy code

Package org. Rudiment. spring;

Public class PersonService
{

Private String name;

Public void setName (String name)
{
This. name = name;
}

Public void info ()
{
System. out. println ("this person's name" + name );
}
}


4. Create a test class

The Code is as follows: Copy code

Import org. Rudiment. spring. PersonService;
Import org. springframework. context. ApplicationContext;
Import org. springframework. context. support. ClassPathXmlApplicationContext;


Public class SpringTest
{

/**
* @ Param args
*/
Public static void main (String [] args)
{
ApplicationContext ac = new ClassPathXmlApplicationContext ("applicationContext. xml ");
PersonService p = ac. getBean ("personService", PersonService. class );
P.info ();
}

}


5. Create a Spring configuration file (applicationContext. xml), which is placed under the classes. If you are developing in myeclipse, put it directly under the src directory (IDE will help us move to the classes directory)

The Code is as follows: Copy code

<? Xml version = "1.0" encoding = "GBK"?>
<Beans xmlns = "http://www.springframework.org/schema/beans"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xmlns: aop = "http://www.springframework.org/schema/aop"
Xmlns: tx = "http://www.springframework.org/schema/tx"
Xsi: schemaLocation ="
Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
Http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd ">

<! -- Configure our PersonService class -->
<Bean id = "personService" class = "org. Rudiment. spring. PersonService">
<Property name = "name" value = "wawa"/>
</Bean>

</Beans>


So far, our spring configuration is complete.
One of the most difficult problems is. The spring framework does not seem to provide a configuration file template, so I sent this configuration file to me as a QQ friend and then modified it --!

 

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.