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

news/2024/8/25 17:23:06

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();
        
    }

}

http://www.niftyadmin.cn/n/3863234.html

相关文章

[置顶]白话贝叶斯理论及在足球比赛结果预测中的应用和C#实现

离去年“马尔可夫链进行彩票预测”已经一年了&#xff0c;同时我也计划了一个彩票数据框架的搭建&#xff0c;分析和预测的框架&#xff0c;会在今年逐步发表&#xff0c;拟定了一个目录&#xff0c;大家有什么样的意见和和问题&#xff0c;可以看看&#xff0c;留言我会在后面…

spring Bean使用案例 静态方法方式 和工厂模式

参考上一篇文章spring Bean使用案例 无参数构造方式 静态方法 1、新建一个类 package spring_day_1; /** * * author linwen *静态方法创建对象 */ public class TastDome2 { public static TastDome add(){ return new TastDome(); } } 2、编写xml…

sublime开启vim模式

学习目标&#xff1a;在sublime下开启vim模式&#xff0c;了解基本vim的编辑快捷键。 下载安装Sublime Text 3 &#xff1a;http://www.sublimetext.com/3Vim/Vi&#xff1a; Vim/Vi 是一个文本编辑器&#xff0c;没有菜单&#xff0c;只有命令&#xff0c;适合写前端代码。高效…

Spring官方文档翻译(1~6章)

spring官方文档&#xff1a;http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/ 一、Spring框架概述 Spring框架是一个轻量级的解决方案&#xff0c;可以一站式地构建企业级应用。Spring是模块化的&#xff0c;所以可以只使用其中需要的部分。…

Spring官方文档翻译(7章)

三、核心技术 这部分的文档覆盖了spring完整的技术。 在这些技术中最重要的要属Spring的控制反转&#xff08;IoC&#xff09;容器了&#xff0c;紧随其后的是全面覆盖的面向切面编程&#xff08;AOP&#xff09;技术。Spring有它自己的AOP框架&#xff0c;它很容易理解&…

spring使用案例 属性注入

构造方法注入 1、新建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…

Hashtable无序,用Dictionary代替

//输出1234 public void TestDictionary() {Dictionary<string, int> dic new Dictionary<string, int>();dic.Add("One", 1);dic.Add("Two", 2);dic.Add("Three", 3);dic.Add("Four", 4);foreach (var r in dic.Keys) …

SQL性能优化(不断总结)

1.查询的模糊匹配 尽量避免在一个复杂查询里面使用 LIKE %parm1%—— 红色标识位置的百分号会导致相关列的索引无法使用&#xff0c;最好不要用. 解决办法: 其实只需要对该脚本略做改进&#xff0c;查询速度便会提高近百倍。改进方法如下&#xff1a; a、修改前台…