Spring-创建最简单的Bean-HelloWorld

坐标获取
http://maven.aliyun.com/mvn/search

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.spoon</groupId>
  <artifactId>spring-ioc</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.10.RELEASE</version>
    </dependency>
  </dependencies>
</project>

/spring-ioc-v1/src/main/java/beans/HelloService.java

package beans;
/**
 * 1.这个类的对象如何交给Spring管理?
 * Spring如何知道我们写了类?(xml)如何构建此类对象?如何存储此对象?
 * 2.我们使用此类对象时如何获取对象?
 */
public class HelloService {
    public void sayHello(){
        System.out.println("HelloWorld,I'm Hello Service");
    }
}

/spring-ioc-v1/src/main/resources/spring-configs.xml
<beans>定义信息,从官网获取

<?xml version="1.0" encoding="UTF-8"?>
<beans default-lazy-init="true"
    xmlns="http://www.springframework.org/schema/beans" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="  
       http://www.springframework.org/schema/beans   
       http://www.springframework.org/schema/beans/spring-beans-4.3.xsd  
       http://www.springframework.org/schema/mvc   
       http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd   
       http://www.springframework.org/schema/tx   
       http://www.springframework.org/schema/tx/spring-tx-4.3.xsd   
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
       http://www.springframework.org/schema/util 
       http://www.springframework.org/schema/util/spring-util-4.3.xsd
       http://www.springframework.org/schema/data/jpa 
       http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.3.xsd" >
       <!-- Spring通过bean标签描述由它管理的对象 
       id属性值为bean对象的唯一标识
       class属性的值是类全名(包名+类名)权限命名
       -->
       <bean id="helloService" class="beans.HelloService"></bean>
</beans>

/spring-ioc-v1/src/test/java/test/TestBeans01.java

package test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import beans.HelloService;

public class TestBeans01 {
    public static void main(String[] args) {
        //1.初始化spring框架容器(IOC)对象  .class文件所在的路径--类路径 从类路径读取xml
        //ApplicationContext应用程序上下文对象-场景对象 对象的创建,存储需要场景
        //上下文的创建需要关联工厂:(引用)上文(为后续提供什么服务)下文
        //让spring管理对象需要给它配置
        
        //1.1读取xml文件中的配置信息(xml解析)
        //1.2把解析出来的配置信息进行存储
        
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring-configs.xml");
        //2.获取bean对象(如HelloServcice)
        //2.1此对象从哪里获取?何时创建?
        
        //所有类都有唯一的字节码对象.class 这个T就是类型
        HelloService hService = ctx.getBean("helloService",HelloService.class);//Class<T>
        //3.应用bean对象(如访问对象的方法)
        hService.sayHello();
        //4.释放资源(一般会发生在服务器关闭或重启过程中)
        ctx.close();
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。