A preface

The knowledge seeker plans to revisit Spring, maybe once a week in the future.

Inheriting the spirit of open Source, Spreading technology knowledge;

Several ways to get context

  • AnnotationConfigApplicationContext: from one or more configuration based on Java class loading Spring application context.
  • AnnotationConfigWebApplicationContext: from one or more configuration based on Java class loading Spring Web application context.
  • ClassPathXmlApplicationContext: from the classpath of one or more loading context defined in the XML configuration files.
  • FileSystemXmlapplicationcontext: from the file system under one or more loading context defined in the XML configuration files.
  • XmlWebApplicationContext: Loads the context definition from one or more XML configuration files under the Web application

2.1 Preparations

Sheet entities

public class Sheet {

    / / color
    private String color;
    / / the length
    private String length;
    // omit set get
}    
Copy the code

Sheet. XML is injected with Bean sheet, and the default color is initialized to red;

<?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="sheet" class="com.zszxz.bean.Sheet">
        <property name="color" value="pink"></property>
    </bean>


</beans>
Copy the code

2.2 FileSystemXmlapplicationcontext for context

FileSystemXmlApplicationContext constructor parameters need to be specified in the sheet. The XML file system path; Get the Bean Sheet using the getBean method after getting the context; Get the object and print the color pink using getColor.

    public static void main(String[] args) {
        / / XML path
        String path = "C:\\java\\workspaceforresource\\study-spring\\obtain-bean-way\\src\\main\\resources\\sheet.xml";
        // Get the context from the file system
        ApplicationContext applicationContext = new FileSystemXmlApplicationContext(path);
        / / get a bean
        Sheet sheet = (Sheet) applicationContext.getBean("sheet");
        // pink
        System.out.println(sheet.getColor());
    }
Copy the code

2.3 ClassPathXmlApplicationContext for context

ClassPathXmlApplicationContext incoming parameters is the classpath sheet. The XML path;

    public static void main(String[] args) {
        // Get the context
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("sheet.xml");
        // Get the instance
        Sheet sheet = (Sheet) applicationContext.getBean("sheet");
        // pink
        System.out.println(sheet.getColor());
    }
Copy the code

2.4 AnnotationConfigApplicationContext for context

AnnotationConfigApplicationContext context, is the way through the Java configuration access context; The knowledge seeker side needs Java configuration, which is as follows, the same as sheet. XML before

/ * * *@AuthorThe LSC * <p> sheet configuration class is equivalent to sheet.xml</p> */
@Configuration
public class SeetConfig {

    // Inject beans into the configuration class
    @Bean
    public Sheet sheet(a){
        // Create an object
        Sheet sheet = new Sheet();
        // Set the properties
        sheet.setColor("pink");
        returnsheet; }}Copy the code

Access method is as follows, the incoming AnnotationConfigApplicationContext parameter is SeetConfig. The class

    public static void main(String[] args) {
        // Get the context
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SeetConfig.class);
        // Get the instance
        Sheet sheet = (Sheet) applicationContext.getBean("sheet");
        // pink
        System.out.println(sheet.getColor());
    }
Copy the code