[Spring practice series] (6) configure the Bean of the Spring IOC container

Source: Internet
Author: User

[Spring practice series] (6) configure the Bean of the Spring IOC container
1. IntroductionSpring provides a powerful IOC container to manage the beans that make up applications. To use the container service, you must configure the Bean running in the Spring IOC container.2. SolutionYou can use XML files, attribute files, annotations, and even APIs to set beans in Spring IOC containers. Spring allows you to configure beans in one or more bean configuration files. For simple applications, you can configure beans in a single configuration file. However, for large applications with many beans, you should split them into multiple configuration files based on their functions.3. Create Spring ConfigurationAs mentioned above, Spring is a container-based framework. If Spring is not configured, it is an empty container, which is useless to us. Therefore, we need to configure Spring to tell the container which Beans it needs to load and how to assemble them so that they can collaborate with each other. From Spring 3.0, Spring containers provide two Bean configuration methods. Traditionally, Spring uses one or more XML files as configuration files, while Spring 3.0 also providesJava annotation-Based Configuration. Here we first useTraditional XML file configuration methodsLater, we will use the annotation configuration method. When a Bean is declared in an XML file, the root element of the Spring configuration file is defined by the Spring beans namespace. Element. The following is a typical Spring XML configuration file:


   
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-3.0.xsd">   In Element, you can place all Spring configuration information, including Element declaration. However, the beans namespace is not the only Spring namespace you encounter. Spring's core framework comes with 10 namespaces, as shown in the following table:
Namespace Description
AOP The configuration element is provided for the declaration plane and the class proxy of @ AspectJ annotation for the Spring plane.
beans Bean declaration and Bean assembly are supported, which is the core and original namespace of Spring.
context Configuration elements are provided for configuring the Spring application context, including automatic detection and Bean assembly, and injection of objects not directly managed by Spring.
jee It provides integration with Java EE APIs, such as JNDI and EJB.
jms The configuration element is provided for the message-driven POJO.
lang Supports Bean configuration implemented by scripts such as Groovy, JRuby, and BeanShell.
mvc The ability to enable Spring MVC, such as a controller for aspect annotation, attempts to controller and interceptor.
oxm Supports Spring object-to-XML ing configuration.
tx Provides declarative transaction configuration.
util Provides a variety of tool-class elements, including configuring integration as beans and supporting attribute placeholder elements.
  Here, we mainly learn how to configure bean and learn other things.  4. Declare Bean 
package com.sjf.bean;
/** * Student Entity * @author sjf0115 * */public class Student { private String name; private int age; private boolean sex; public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public void setSex(boolean sex) { this.sex = sex; } @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append("name:" + name + "\n"); builder.append("age:" + age + "\n"); builder.append("sex:" + (sex ? "boy" : "girl") + "\n"); return builder.toString(); }}   As you can see, we have implemented a Student Entity class Student that provides its name, age, and Gender attributes. We can use the set method to set values for its properties. Now that the Student class is defined, please invite our first Student Yoona. Yoona is defined as a Spring Bean and declared in the configuration file applicationContext. xml:
 
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">   An element is the most basic configuration unit in Spring. Through this element, Spring creates an object. Here, a Bean named yoona managed by the Spring container is created. This may be the simplest Configuration method. The id attribute defines the Bean name and serves as a reference for the Bean in the Spring container. This Bean is called yoona. You can also learn that yoona is a Student based on the class attribute. You can specify Element. Spring will try to convert the value you specified to the declared type of this attribute. You can use Element, and specify the attribute name in its name feature. Each The bean must contain a corresponding setting method.   To print the details of Student, you can use the following code to load the Spring application context:
// 1. Create a Spring IOC container
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); // 2. Obtain the Bean instance from the IOC containerStudent stu = (Student)context.getBean("yoona");// 3. Call the toString MethodSystem.out.println(stu.toString());  

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.