我的Spring初尝试

有关Bean的 依赖设置有两种方法:

1.常用的Setter()方法,它是在建立好对象后再完全依赖注入.

2.使用Constructor方法,它在构造对象是一并使用.

下面我用第一种方法来写。

1.Student1类:

package com.lzw;

public   class  Stu1 implements student  {

    
private String name;
    
private String sex;
    
private int age;

    
public void printInfo() {
        
// TODO Auto-generated method stub
        System.out.println("姓名:" + name);
        System.
out.println("性别:" + sex);
        System.
out.println("年龄:" + age);
    }


    
public String getName() {
        
return name;
    }


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


    
public String getSex() {
        
return sex;
    }


    
public void setSex(String sex) {
        
this.sex = sex;
    }


    
public int getAge() {
        
return age;
    }


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


}

2.student2类:

 

package com.lzw;

public   class  Stu2 implements student  {


    
private String name;
    
private String sex;
    
private int age;
    
    
public void printInfo() {
        
// TODO Auto-generated method stub
        System.out.println("姓名:" + name);
        System.
out.println("性别:" + sex);
        System.
out.println("年龄:" + age);
    }


    
public String getName() {
        
return name;
    }


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


    
public String getSex() {
        
return sex;
    }


    
public void setSex(String sex) {
        
this.sex = sex;
    }


    
public int getAge() {
        
return age;
    }


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


}

3.一个student接口:

 

package com.lzw;

public   interface  student  {

        
public void printInfo();
}

测试类:

 

package com.lzw;

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

public   class  Info  {

    
/**
     * 有关实例化BeanFactory常用的两种方法
     
*/

    
    
public static void main(String[] args) {
        
/**
         * 方法一:
         
*/
 
    
        
//final ClassPathResource rs=new ClassPathResource("applicationContext.xml");
        
//XmlBeanFactory be=new XmlBeanFactory(rs);
        
//student stu=(student) be.getBean("stu1");
        
//stu.printInfo();
        
//student stu1=(student) be.getBean("stu2");
        
//stu1.printInfo();
        /**
         * 方法二:
         
*/
 
        ApplicationContext cont
=new ClassPathXmlApplicationContext("applicationContext.xml");
        student stu1
=(student) cont.getBean("stu1");
        stu1.printInfo();
        student stu2
=(student) cont.getBean("stu2");
        stu2.printInfo();
        
    
        
    }


}

Web配置:

 

<? 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-2.0.xsd " >
    
< bean id = " stu1 "   class = " com.lzw.Stu1 " >
        
< property name = " name " >
            
< value > 飞龙 </ value >
        
</ property >
        
< property name = " sex " >
            
< value > </ value >
        
</ property >
        
< property name = " age " >
            
< value > 26 </ value >
        
</ property >
    
</ bean >
    

< bean id = " stu2 "   class = " com.lzw.Stu2 " >
    
< property name = " name " >
        
< value > 欣欣 </ value >
    
</ property >
    
< property name = " sex " >
        
< value > </ value >
    
</ property >
    
< property name = " age " >
        
< value > 22 </ value >
    
</ property >
</ bean >
    
</ beans >


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值