Spring Introduction 1: Spring introduction and introduction
1. What is Spring? II. Specific description of Spring 3. Setting up the Spring Environment
3. Spring configuration file: one or more Bean configuration files need to be created for a typical Spring project. These configuration files are used to configure beans in the Spring IOC container. Bean configuration files can be stored in classpath or other directories.
4. Create a Spring Project and write HelloWorld:
package com.atguigu.spring.beans;public class HelloWorld { private String name; public void setName(String name) { System.out.println("setName..."); this.name = name; } public void hello(){ System.out.println("Hello " + name); } public HelloWorld() { System.out.println("HelloWorld's construct..."); } }
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <bean id="helloworld" class="com.atguigu.spring.beans.HelloWorld"> <property name="name" value="spring"></property> </bean></beans>
Package com. atguigu. spring. beans; import org. springframework. context. ApplicationContext; import org. springframework. context. support. ClassPathXmlApplicationContext;
Public class Main {public static void main (String [] args) {/* HelloWorld helloWorld = new HelloWorld (); helloWorld. setName ("spring"); helloWorld. hello (); * // 1. create container ApplicationContext ctx = new ClassPathXmlApplicationContext ("appliactionContext. xml ");
// 2. Obtain bean HelloWorld hello = (HelloWorld) ctx. getBean ("helloworld") from the container ");
// 3. Call the bean method hello. hello ();}}
5. Test Results