Spring core notes (1)

Source: Internet
Author: User

Technorati flag: spring, java I. Container Overview 1. Metadata Configuration

Metadata configuration can be divided into three methods: the simplest and most direct method is to use the xml configuration file, and the other two are spring annotation configuration and java annotation configuration. This time, we will start with xml and gradually go into depth.

In xml, <bean> and </bean> are used to define a metadata. Generally, you can define objects at the service layer, data access objects, presentation layer objects, such as Structs actions. The basic structure of the metadata configured in xml is as follows:

<? 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.xsd"> <bean id = "... "class = "... "> <! -- Configure bean dependency object and attribute settings --> </bean> <bean id = "..." class = "..."> <! --... --> </Bean> </beans>

2. Change the container

In spring, the most typical container is ApplicationContext. Its Initialization is as follows:

ApplicationContext context =    new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});

The service. xml service layer object instance is as follows:

<? 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.xsd"> <! -- Services --> <bean id = "petStore" class = "org. springframework. samples. jpetstore. services. petStoreServiceImpl "> <property name =" accountDao "ref =" accountDao "/> <property name =" itemDao "ref =" itemDao "/> <! -- Other dependent objects and basic configurations --> </bean> <! -- Configure more service objects --> </beans>

The following figure shows the daos. xml Data Access Object:

? 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.xsd"> <bean id = "accountDao" class = "org. springframework. samples. jpetstore. dao. ibatis. sqlMapAccountDao "> <! -- Other dependent objects and basic configurations --> </bean> <bean id = "itemDao" class = "org. springframework. samples. jpetstore. dao. ibatis. SqlMapItemDao"> <! --... --> </Bean> <! -- Configure more data access objects --> </beans>

In the preceding Demo, the service layer object is a PetStoreServiceImpl class object. The two data access objects are SqlMapAccountDao and SqlMapItemDao class objects, both of which are based on the iBatics ORM framework.

If there are multiple xml Metadata configuration files, you can combine them into an xml-based configuration file, as shown below:

<beans>    <import resource="services.xml"/>    <import resource="resources/messageSource.xml"/>    <import resource="/resources/themeSource.xml"/>    <bean id="bean1" class="..."/>    <bean id="bean2" class="..."/></beans>

As shown above, three external files, services. xml, messageSource. xml and themeSource. xml, where services. xml must be in the classpath path, while messageSource. xml and themeSource. xml must be in the resources folder.

3. Use container

The purpose of all configurations is to use the container ApplicationContext. ApplicationContext maintains a registry for each Bean and its dependencies. Use T getBean (String name, Class <T> requiredType> to obtain the corresponding Bean instance. As follows:

// Create the container ApplicationContext context = new ClassPathXmlApplicationContext (new String [] {"services. xml "," daos. xml "}); // obtain the instance PetStoreServiceImpl service = context for the Bean configuration. getBean ("petStore", PetStoreServiceImpl. class); // use instance List userList = service. getUsernameList ();

Ii. Bean Overview

The ioc container manages one or more beans. These beans can be provided to the ioc container through the specified configuration method. As mentioned above, the xml configuration method <bean/>

Compared with the container itself, these beans are used as BeanDefinition objects and generally contain the following metadata:

  • Fully Qualified name package + class name): java bean class name
  • Bean behavior: Specifies the bean behavior in the container, including scope and lifecycle callback methods.
  • Bean dependent object
  • Other property configurations: such as the number of connections in the connection pool

The following are some common bean attributes: class, name, scope, constructor arguments, properties, autowiring mode, lazy-initialization mode, initialization method, destruction method.

1. Name Bean

In the xml configuration file, you can use the property id or name to name a bean. The id is the unique Tag Name of the bean, which is unique throughout the xml document. Attribute name is used to specify one or more aliases for the id. Multiple aliases are separated by commas (,), semicolons (,), or spaces.

When you do not specify an id or name for the bean, the container automatically specifies a unique flag name for the bean. Of course, if you need ref to point to this bean, you must specify a name for it.

2. bean alias

<alias name="fromName" alias="toName"/>

3. instantiate bean

Spring provides three ways to instantiate a bean. For xml-based configuration, you can use the class constructor, static factory method, and instance factory method.

  • Instantiate the bean using the class constructor as follows:
<bean id="exampleBean" class="examples.ExampleBean"/><bean name="anotherExample" class="examples.ExampleBeanTwo"/>

  • Use static factory Method

The static factory method requirements class must containStaticFactory method, and specify the factory method in the xml configuration file as follows: createInstance must be a static method)

<bean id="clientService" class="examples.ClientService"      factory-method="createInstance"/>

The ClientService class is designed as follows:

public class ClientService {  private static ClientService clientService = new ClientService();  private ClientService() {}  public static ClientService createInstance() {    return clientService;  }}

  • Instance factory Method

Similar to the static factory method, the instantiation factory method uses the non-static method of factory bean already exists in the container to instantiate a new bean. To instantiate a bean in this way, set the property name to null, set factory-bean to specify factoryBean, and set the property factory-method to specify the instantiation method, as shown below:

<! -- FactoryBean contains a non-static createInstance () --> <bean id = "serviceLocator" class = "examples. DefaultServiceLocator"> <! -- Inject other dependent objects --> </bean> <! -- Bean instantiated through factoryBean --> <bean id = "clientService" factory-bean = "serviceLocator" factory-method = "createClientServiceInstance"/>


Defaservicservicelocator is designed as follows:

public class DefaultServiceLocator {  private static ClientService clientService = new ClientServiceImpl();  private DefaultServiceLocator() {}  public ClientService createClientServiceInstance() {    return clientService;  }}

A deeper discussion is that a factory bean can have multiple factory methods, as shown below:
<Bean id = "serviceLocator" class = "examples. DefaultServiceLocator"> <! -- Inject other dependent objects --> </bean> <bean id = "clientService" factory-bean = "serviceLocator" factory-method = "createClientServiceInstance"/> <bean id = "accountService "factory-bean =" serviceLocator "factory-method =" createAccountServiceInstance "/>


Defaservicservicelocator is designed as follows:
public class DefaultServiceLocator {  private static ClientService clientService = new ClientServiceImpl();  private static AccountService accountService = new AccountServiceImpl();  private DefaultServiceLocator() {}  public ClientService createClientServiceInstance() {    return clientService;  }  public AccountService createAccountServiceInstance() {    return accountService;  }}


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.