The three ways spring instantiates beans are:
1,xml configuration using the Bean's class constructor
<bean id= "Personservice" class= "Cn.service.impl.PersonServiceBean" ></bean>
2,xml Configuring the +factory class, instantiating using a static factory method
<bean id= "PersonService2" class= "Cn.service.impl.PersonServiceBeanFactory" factory-method= " Createpersonservicebean "/>
3,xml Configuring +factory classes, instantiating using instance factory methods
<bean id= "Personservicefactory" class= "Cn.service.impl.PersonServiceBeanFactory"/>
<bean id= "PersonService3" factory-bean= "Personservicefactory" factory-method= "createPersonServiceBean2"/>
The use of the three spring instantiation bean methods,
The first method, created by the Bean's default constructor, can be used when the business logic of each bean is relatively independent of each other or is less correlated with the outside world.
The second approach, created using the static factory method, allows the creation of individual beans to be managed uniformly, such as when individual beans need the same initialization before they are created, and can be handled uniformly by this factory method.
The third method, using the instantiation of the factory method, is created, and the factory method is also used as a business bean to control.
Scope of the bean :
1, Default: Singleton (Singleton, each getbean () is the same bean)
<bean id= "Bean1" class= "Cn.transaction.Bean1" ></bean>
2,prototype: (Every time Getbean () gets a new bean)
<bean id= "Bean1" class= "Cn.transaction.Bean1" scope= "prototype" ></bean>
Bean's life cycle :
1, default: Singleton is instantiated when the container is instantiated;
1.1, more of this instantiation time:
<bean id= "Bean1" class= "Cn.transaction.Bean1" lazy-init= "true" ></bean>
If you want to defer initialization of all beans:
Configure this property in beans
1.2, configure the initialization method:
<bean id= "Bean1" class= "Cn.transaction.Bean1" lazy-init= "true" init-method= "Init" ></bean>
1.3, configuring the Destroy method
<bean id= "Bean1" class= "Cn.transaction.Bean1" lazy-init= "true" init-method= "Init" destroy-method= "destroy" > </bean>
When was the 1.4,bean destroyed?
The default is to create the bean when the spring container is initialized, so the bean is destroyed when the spring container is closed.
2,prototype: Instantiate when calling Getbean ();
--------------------------------------------------------------------------------------------------------------- --
Spring Learning to understand control inversion and the benefits that spring can bring in projects
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.
1 first understand the concept of inversion of control, such as the following program
public class Personservicebean {
Private Persondao Persondao = new Persondaobean ();
public void Save (person person) {
Persondao.save (person);
}
}
Persondao is a class of dependent objects, Persondaobean is created and maintained within the application. The so-called control inversion 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, and the transfer of control is the so-called reversal
2 The dependent object is assigned to the external container for creation, then the Personservicebean class can be changed to the following:
public class Personservicebean {
Private Persondao Persondao;
The constructor parameter allows the container to inject the created dependent object into the Personservicebean, or it can be injected using the setter method.
Public Personservicebean (Persondao Persondao) {
This.persondao=persondao;
}
public void Save (person person) {
Persondao.save (person);
}
}
The so-called dependency injection means that, at runtime, the dependent objects are dynamically injected into the component by the external container.
3. Ms. Li summarizes the following benefits that spring can bring to the project.
1) Reduce the coupling between components, to achieve the decoupling between the various layers of software.
Controller-->service-->dao
2) You can use many of the services provided by the container, such as transaction management services, message services, and so on. When we use containers to manage transactions, developers no longer need to manually control transactions. There is no need to handle complex transactional propagation.
3) Containers provide singleton mode support, and developers no longer need to write their own implementation code.
4) The container provides AOP technology, it is easy to implement such as permission interception, run-time monitoring and other functions.
5) The many auxiliary classes provided by the container, which can be used to speed up the development of the application, such as: JdbcTemplate, Hibernatetemplate.
6) Spring provides integrated support for the mainstream application framework, such as integrating Hibernate, JPA, struts, and so on for easier application development.
7) Use the services that spring containers can provide: Transaction management services, JMS services, spring core core services, persistence services, and more.
8) If you use spring, we no longer need to manually control transactions, such as the statement session.begintransaction () that controls transactions in hibernate; Session.gettransaction (). commit ();
9) Using spring eliminates the need for us to handle complex transactional propagation behavior.
Is it often unclear whether spring is a lightweight frame or a weight frame? Some people think that a project over 10M is heavyweight, and this division is obviously wrong, because it may contain most of the images, but with very little code. In fact, whether an application is lightweight or heavyweight, it mainly depends on how much service it uses. The more services you use, the more work the container will do for ordinary Java objects, which will inevitably affect the release time or performance of the application.
For spring containers, it provides a lot of services, but these services are not opened by default for the app, the application needs some kind of service, and you need to indicate the use of the service, if the application uses a few services, such as: Only the spring core services, then we can think that the application is lightweight, If the app uses most of the services provided by spring, then the app is heavyweight. Currently, the EJB container is heavyweight because it provides all the functionality of the EJB specification by default for the application.
--------------------------------------------------------------------------------------------------------------- ------
How the bean is instantiated and the scope of the bean
Use the jar required by spring
You can download spring to http://www.springsource.org/download, then unzip it, locate the following jar file in the Extract directory, and copy it to the classpath.
Dist/spring.jar
Lib/jakarta-commons/commons-logging.jar
If you are using slice programming (AOP), the following jar files are also required
Lib/aspectj/aspectjweaver.jar and Aspectjrt.jar
Lib/cglib/cglib-nodep-2.1_3.jar
If you use annotations in JSR-250, such as @resource/@PostConstruct/@PreDestroy, the following jar files are also required
Lib/j2ee/common-annotations.jar
Configuration templates for Beans.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 ">
.....
</beans>
The configuration template can be obtained from the Spring Reference manual or the spring example. The name of the configuration file can be arbitrary, the file can be stored in any directory, but considering the generality, generally placed under the classpath.
There are two common ways to instantiate a spring container:
Method One:
To instantiate a container by looking for a configuration file under the Classpath
ApplicationContext CTX = new Classpathxmlapplicationcontext (new string[]{"Beans.xml"});
Method Two:
To instantiate a container by looking for a configuration file under File system path
ApplicationContext CTX = new Filesystemxmlapplicationcontext (new string[]{"D://beans.xml"});
In general, the first way, spring's configuration file can be specified more than one, can be through a string array
Incoming.
When the spring container is started, because the spring container can manage the creation of Bean objects, destroy life
Cycle, so we just get the bean object directly from the container, instead of writing a code to create
The Bean object. The code to get the Bean object from the container is as follows:
ApplicationContext CTX = new Classpathxmlapplicationcontext ("Beans.xml");
OrderService service = (OrderService) ctx.getbean ("Personservice");
three ways to instantiate beans:
1. Instantiate the
<bean id= "OrderService" class= "Using the Class Builder Cn.itcast.OrderServiceBean "/>
2. Instantiate with static Factory method
<bean id=" Personservice "class=" Cn.itcast.service.OrderFactory "factory-method=" Createorder "/>
public class Orderfactory {
public Static Orderservicebean Createorder () {
return new Orderservicebean ();
}
}
3. Instantiate using instance Factory method:
<bean id= "personservicefactory" class= "Cn.itcast.service.OrderFactory"/>
<bean id= "Personservice" factory-bean= "Personservicefactory" factory-method= "Createorder"/>
Public Class Orderfactory {
public orderservicebean createorder () {
return new Orderservicebean ();
}
}
Scope of the Bean
. Singleton (single case)
In each spring IOC container, a bean defines only one instance of an object. By default, the bean is initialized when the container starts, but we can specify the bean node's lazy-init= "true" to defer the initialization of the bean, which is the first time that the bean is initialized. Such as:
<bean id= "xxx" class= "Cn.itcast.OrderServiceBean" lazy-init= "true"/>
If you want to apply lazy initialization to all beans, you can set default-lazy-init= "true" at the root node beans, as follows:
<beans default-lazy-init= "true" ...>
This property is not set to true in the actual application.
. prototype (prototype)
Each fetch of the bean from the container is a new object. Each time the Getbean method is called, a new instance is fetched.
The bean is instantiated when the call to the Getbean method is called
. Request
. session
. Global session
Specifying the bean's initialization method and method of destruction in the configuration file
<bean id= "xxx" class= "Cn.itcast.OrderServiceBean" init-method= "Init" destroy-method= "Close"/>
After the bean is instantiated, the Init method is executed, and the spring container is invoked through a reflection mechanism.
Abstractapplicationcontext ctx=new classpathxmlapplicationcontext ("Beans.xml");
Ctx.close ();//gracefully close the spring container.
Control reversal and the benefits that spring can bring in a project