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 --!