1. 导入Spring等jar包

2. 配置文件 applicationContext.xml

<?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:context="http://www.springframework.org/schema/context"

    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <!--  <context:component-scan base-package="com.chinasofti.bean"></context:component-scan>  -->  

    <!-- 构造器方式 -->
    <bean id="dog" class="day1.Dog">
        <!-- 有参的类: constructor-arg 标签代表有参构造,index对应第几个参数,value是值 -->
        <constructor-arg index="0" value="杀魔爷"></constructor-arg>
        <constructor-arg index="1" value="888"></constructor-arg>

    </bean>

    <!-- 静态工厂方式  -->
    <bean id="product" class="beans.staticFactorys" factory-method="getProduct"></bean>

    <!-- 非公共类 -->
    <bean id="div" class="beans.Div"></bean>

    <!-- 普通类的方法 返回的对象 -->
    <bean id="factory" class="beans.Factory" ></bean>
    <bean id="product2" factory-bean="factory" factory-method="getProduct"></bean>
</beans>

3. 对应的bean  如

package day1;

public class Dog {
    private String name;
    int age;

    public Dog(){ //无参构造
        System.out.println("初始化,我是无参构造");
    }

    public Dog(String name){
        this.name=name;
        System.out.println("初始化1");
    }

    public Dog(String name,int age){
        this.name=name;
        this.age=age;

        System.out.println("初始化Dog,name:"+name);
    }

    public void shout(){ // 方法
        System.out.println("时间,不在于你拥有多少,而在于你使用多少");
    }

    public String getName() {
        return name;
    }

    public void setName(String s) {
        this.name =s;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Dog [name=" + name + ", age=" + age + "]";
    }

    public void destory(){
        System.out.println("狗不在了");
    }

}

4. 获取bean

package day1;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DogTest {
    public static void main(String[] args) {

        String path="applicationContext.xml";
        ApplicationContext ac=new ClassPathXmlApplicationContext(path);//实例化容器
        Dog dog=(Dog) ac.getBean("dog");// 向容器ac 索取id名为 dog的bean 对象
            // 容器查找bean 配置里的id 为dog 的bean 标签, 然后根据bean 标签里的class路径对应的类  来实例化对应的对象
        //dog.shout();
        System.out.println(dog.getName());
        System.out.println(dog.getAge());

    }
}

Spring最基础使用1的更多相关文章

  1. [Spring框架]Spring AOP基础入门总结二:Spring基于AspectJ的AOP的开发.

    前言: 在上一篇中: [Spring框架]Spring AOP基础入门总结一. 中 我们已经知道了一个Spring AOP程序是如何开发的, 在这里呢我们将基于AspectJ来进行AOP 的总结和学习 ...

  2. [Spring框架]Spring AOP基础入门总结一.

    前言:前面已经有两篇文章讲了Spring IOC/DI 以及 使用xml和注解两种方法开发的案例, 下面就来梳理一下Spring的另一核心AOP. 一, 什么是AOP 在软件业,AOP为Aspect ...

  3. spring中基础核心接口总结

    spring中基础核心接口总结理解这几个接口,及其实现类就可以快速了解spring,具体的用法参考其他spring资料 1.BeanFactory最基础最核心的接口重要的实现类有:XmlBeanFac ...

  4. Spring Boot 基础

    Spring Boot 基础 Spring Boot 项目(参考1) 提供了一个类似ASP.NET MVC的默认模板一样的标准样板,直接集成了一系列的组件并使用了默认的配置.使用Spring Boot ...

  5. Spring MVC 基础

    Spring MVC 基础 1.Web MVC基础 MVC的本质是表现层模式,我们以视图模型为中心,将视图和控制器分离出来.就如同分层模式一样,我们以业务逻辑为中心,把表现层和数据访问层代码分离出来是 ...

  6. Spring Boot 基础教程系列学习文档

    Spring Boot基础教程1-Spring Tool Suite工具的安装 Spring Boot基础教程2-RESTfull API简单项目的快速搭建 Spring Boot基础教程3-配置文件 ...

  7. CgLib动态代理学习【Spring AOP基础之一】

    如果不了解JDK中proxy动态代理机制的可以先查看上篇文章的内容:Java动态代理学习[Spring AOP基础之一] 由于Java动态代理Proxy.newProxyInstance()的时候会发 ...

  8. spring boot基础 入门

    spring boot基础 spring boot 的简单搭建 spring boot 的基本用法 spring boot 基本用法 自动配置 技术集成 性能监控 源码解析 工程的构建 创建一个mav ...

  9. Spring的基础注解

    Spring的基础注解 1.注解的概述 注解是为了便于程序的调试而用于代替配置文件的一种程序语法,与配置文件具有互换性.通常基于注解编程的程序更加简洁. (注:使用Spring注解必须导入aop包) ...

  10. Spring Cloud基础教程

    Spring Cloud基础教程  2017-04-04 被围观 90375 次 该教程内容不定时更新,如您对这些内容感兴趣,可以关注我的博客或微信公众号! 本教程示例代码: GitHub:https ...

随机推荐

  1. springCloud 之 Eureka服务治理机制及代码运行

    服务提供者 服务注册: 服务提供者在启动的时候通过发送Rest请求的方式将自己注册到Eureka Server上,同时带上了自身服务的一些元数据信息.Eureka Server在收到这个请求后,将元数 ...

  2. Console-terminal-tty-shell-kernel

    Console-terminal-tty-shell-kernel 1. 先看图表     1.1 简表     1.2 shell与内核的示意图     1.3 Console-terminal-t ...

  3. 013、MySQL取本月最后日期,取每个月的最后一天日期

    #取本月最后一天 SELECT last_day( curdate( ) ); #取上个月最后一天 , INTERVAL MONTH ) ); , INTERVAL MONTH ) ); , INTE ...

  4. 014-查看PHP的环境变量

    <?php print("你正在用文件的名字为: "); print(__FILE__); print(" <BR>\n"); print(& ...

  5. hbase hbck及region RIT处理

    hbase hbck主要用来检查hbase集群region的状态以及对有问题的region进行修复. hbase hbck :检查hbase所有表的一致性,如果正常,就会Print OK hbase ...

  6. java中关键字super

    super关键字的作用 java中的super关键字是一个引用变量,用于引用父类对象.关键字“super”以继承的概念出现在类中. 主要用于以下情况:1.调用父类的方法   2.调用父类的变量  3. ...

  7. 验证试验 更改了从机CAN通信的MAC地址 从机新挂CAN网络 上电自检通过

    更改前 该之后 主机程序 与 从机 程序 已经上传到网盘上 ,主机和从机程序基本一致, 唯一的区别是 从机更好了MAC地址 为0X10  主机的固定MAC地址为 0X1F 改程序的配置上设置的是双滤波 ...

  8. ArrayList与LindedList区别

    1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构. 2.对于随机访问get和set,ArrayList觉得优于LinkedList,因为LinkedList ...

  9. 对plotTree的解释

    1.>>>a = 1/2/2   >>>a >>>0.25 2.def plotMidText(cntrPt,parentPt,txtString ...

  10. css实现三角箭头(兼容IE6)

    纯css实现三角箭头有几种方式,常规的方式用css3的rotate把元素旋转45度角,无法兼容ie的主要原因是ie不支持边框透明, 第二种方法,使用chroma滤镜透明,经尝试在ie下会出现activ ...