What is spring?
Spring is an open source controlled inversion (inversion of Control, IoC) and facet-oriented (AOP) container Framework . Its main goal is to simplify the development of the Enterprise .
Why use spring?
- Help us manage JavaBean.
- IOC,DI-dependency injection inverse Object Control Dependency injection
- Lightweight framework
- Low coupling degree
S
pring Two core content introduction
IOC: The so-called inversion of control is that the application itself is not responsible for the creation and maintenance of dependent objects, and the creation and maintenance of dependent objects is the responsibility of the external container. So control is transferred from the application to the external container,
The transfer of control is called reversal. Also known as DI, Dependency injection, the container is responsible for injecting the dependent object into the upper object, and the upper object only needs to provide an interface declaration.
Similar to hibernate one -to-many relationship inside the inverse, the responsibility of the relationship maintenance to each other, their own focus on business, and how the other side of the change, all by each other (spring Container)
for maintenance.
AOP: Tangent-oriented programming. Many functions need to have such a business, and the business is basically the same for all functions, you can consider the business as a facet.
like in the servlet. Filter , in struts. Interceptor and so on are some good ways, AOP is a Spring provide the solution, their bottom-up is the proxy mode.
using the Spring Framework development Steps
Download the Spring Project to the official website first, using 3.x,4.x are available. It is best to use the newest, 4.1.5 version I have adopted .
Url:
http://repo.springsource.org/libs-release-local/org/springframework/spring/
Typical steps for Spring development
- Create a project
- Add to Project Spring of the Jar packages and configuration files, add as needed, the generic core jar package needs to be added
- Editing a configuration file ( configuration Bean)
Note that the file name is not wrong, applicationcontext.xml
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd" > <BeanID= "Userdao"class= "Ytu.icoding.dao.UserDao"></Bean><BeanID= "Useraction"class= "Ytu.icoding.action.UserAction"> < Propertyname= "DAO"ref= "Userdao"></ Property> <!--This property name is to correspond to the custom property names in the associated class . -</Bean><BeanID= "User"class= "Ytu.icoding.entity.User"> <Constructor-argname= "Name"value= "Zhang San"></Constructor-arg> <Constructor-argname= "Age"value= "+"></Constructor-arg></Bean></Beans>
Build the test class you want to use:
Package Ytu.icoding.dao; Public class Userdao { publicvoid GetUser () { System.out.println ("I get a User") ; }}
Packageytu.icoding.action;ImportYtu.icoding.dao.UserDao; Public classuseraction {PrivateUserdao DAO; PublicUserdao Getdao () {returnDAO; } Public voidSetdao (Userdao dao) { This. DAO =DAO; } Public voidGetUser () {dao.getuser (); }}
Packageytu.icoding.entity; Public classUser {PrivateString name; Private intAge ; PublicUser () {} PublicUser (String name,intAge ) { Super(); This. Name =name; This. Age =Age ; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } }
Packageytu.icoding.test;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;Importytu.icoding.action.UserAction;ImportYtu.icoding.entity.User; Public classTest { Public Static voidMain (string[] args) {ApplicationContext ctx=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); Useraction User= (useraction) ctx.getbean ("Useraction"); User.getuser (); System.out.println ("-------------"); User u= (user) Ctx.getbean ("user")); System.out.println (U.getname ()); System.out.println (U.getage ()); System.out.println ("----------------"); }}
In the configuration file, a dependency injection is used to inject Userdao into the useraction, note that the Userdao attribute is declared in Useraction and the Get,set method is generated, in the configuration
<property name= "DAO" ref= "Userdao" ></property> name,ref must correspond well.
The constructor method is used to inject the user with the attribute and the corresponding value values.
To instantiate a container by looking for a configuration file under the Classpath
ApplicationContext ctx = new Classpathxmlapplicationcontext (New string[]{"Applicationcontext.xml"});
Method Two:
To instantiate a container by looking for a configuration file under File system path
ApplicationContext ctx = new Filesystemxmlapplicationcontext (New string[]{"D:\\applicationcontext.xml"});
Spring's configuration file can be specified in multiple, and can be passed in through a string array.
Attention:
1. The Bean tag has a scope attribute, and the value is singleton,prototype,request,session
Each time the Spring Framework is used to obtain JavaBean, one instance at a time, one instance at a time, each request for an instance, and the other eachsession.
2. Three ways to instantiate a bean :
(1. Instantiating using the Class builder
<bean id="UserService" class="Com.jmt.service.impl.UserServiceBean"></bean>
(2. Instantiating using the static factory method
<bean id="UserService2" class="Com.jmt.service.impl.UserServiceBeanFactory "
factory-method="Createuserservicebean"/>
(3. Instantiate using the instance factory method:
<bean id="userservicefactory" class="com.jmt.service.impl.UserServiceBeanFactory"/ >
<bean id="UserService3" factory-bean="userservicefactory " factory-method= " CreateUserServiceBean2 "/>
In general, the first type is used.
Getting Started with spring framework