spring Bean使用案例 无参数构造方式

1、导入spring包(四个基本核心包)


2、在src目录新建xml

3、xml导入schema约束

<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">

4、新建一个测试类

package spring_day_1;

public class TastDome {
        public void add(){
            System.out.println("sssss");
        }
}

5、编辑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"    
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
        <!-- 无参数构造 -->
        <bean id="bean" class="spring_day_1.TastDome"/>

id 属性  名字

class属性  创建类的包名

name属性 和id一样 却别 能包含一些特殊符号

scope属性 bean的作用范围

    singleton 默认值 单实例

    prototype 多实例的

    request 在web项目中,spring创建一个bean的对象,将对象存入request对象中

    session 在web项目中,spring创建一个bean的对象,将对象存入session对象中

     golbalSession 在web项目中,应用在porlet环境,如果没有porlet环境那么golbalSession相当于session

6、使用

package spring_day_1;

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

public class Spring_dome {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ApplicationContext context=new ClassPathXmlApplicationContext("MyXml.xml");
        TastDome user=(TastDome) context.getBean("bean");
        user.add();
        
    }

}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值