首页 > 程序开发 > 软件开发 > Java >

spring配置教程bean部分解析

2017-11-02

spring配置教程bean部分解析。

bean.xml



      
        
    

        
        

        
        
            
            

            
            

            
            

            
            

            
            

            
            
                
            

            
            
                
            

            
            
                
            

            

                
                

                    
                    

                

            
            
                properties元素的value
            
        

    

scope属性演示:

bean.xml



      

     

     

     

     

Person.java

package cn.xiedacon.test.domain;

public class Person {

    private int id;
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

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

}

Test1.java

package cn.xiedacon.test;


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

import cn.xiedacon.test.domain.Person;
public class Test1 {

    @Test
    public void test() throws Exception {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        Person person1 = (Person) context.getBean("singletonPerson");
        person1.setId(1);
        person1.setName("aaa");
        Person person2 = (Person) context.getBean("singletonPerson");
        Person person3 = (Person) context.getBean("prototypePerson");
        person3.setId(2);
        person3.setName("bbb");
        Person person4 = (Person) context.getBean("prototypePerson");

        System.out.println("singletonPerson1:"+person1);
        System.out.println("singletonPerson2:"+person2);
        System.out.println(person1.equals(person2));

        System.out.println("prototypePerson1:"+person3);
        System.out.println("prototypePerson2:"+person4);
        System.out.println(person3.equals(person4));

    }

}

输出结果:
singletonPerson1:Person [id=1, name=aaa]
singletonPerson2:Person [id=1, name=aaa]
true
prototypePerson1:Person [id=2, name=bbb]
prototypePerson2:Person [id=0, name=null]
false

由上可知:
scope=”singleton”时,返回同一个对象
scope=”prototype”时,返回不同对象
scope=”request”和scope=”session”,需要到web容器中演示

init-method,destroy-method属性演示

bean.xml



      

     

Person.java

package cn.xiedacon.test.domain;

public class Person {

    private int id;
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

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

    public void init(){
        System.out.println("对象被创建啦!!!");
    }

    public void destroy(){
        System.out.println("对象被销毁啦!!!");
    }
}

Test1.java

package cn.xiedacon.test;


import org.junit.Test;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.xiedacon.test.domain.Person;
public class Test1 {

    @Test
    public void test() {
        System.out.println("创建对象");
        ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Person person = (Person) context.getBean("person");
        person.setId(1);
        person.setName("aaa");
        System.out.println(person);
        System.out.println("销毁对象");
        context.close();
    }
}

输出结果:
创建对象
对象被创建啦!!!
Person [id=1, name=aaa]
销毁对象
对象被销毁啦!!!

factory-bean,factory-method属性演示

bean.xml



      

     

     
        
        
     

     

Person.java

package cn.xiedacon.test.domain;

public class Person {

    private int id;
    private String name;

    public Person() {
        super();
    }

    public Person(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

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

Factory.java

package cn.xiedacon.test;

import cn.xiedacon.test.domain.Person;

public class Factory {

    public Person getPerson(){
        return new Person();
    }

    public Person getPerson(int id,String name){
        return new Person(id, name);
    }
}

Test1.java

package cn.xiedacon.test;


import org.junit.Test;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.xiedacon.test.domain.Person;
public class Test1 {

    @Test
    public void test() {
        ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        Person person1 = (Person) context.getBean("person1");
        Person person2 = (Person) context.getBean("person2");

        System.out.println(person1);
        System.out.println(person2);
    }
}

输出结果:
Person [id=0, name=null]
Person [id=1, name=aaa]

merge属性演示

bean.xml



      

    
        
            
                1
                2
                3
            
        
    

    
        
        

        
            
                1
                2
                3
            
        
     

     
        
        

        
            
                1
                2
                3
            
        
     

     
        
        

        
            
                1
                2
                3
            
        
     

Person.java

package cn.xiedacon.test.domain;

import java.util.List;

public class Person {

    private int id;
    private String name;
    private List list;
    public Person() {
        super();
    }

    public Person(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "Person [id=" + id + ", name=" + name + ", list=" + list + "]";
    }
}

Test1.java

package cn.xiedacon.test;

import org.junit.Test;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.xiedacon.test.domain.Person;

public class Test1 {

    @Test
    public void test() {
        ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        Person person1 = (Person) context.getBean("person1");
        Person person2 = (Person) context.getBean("person2");
        Person person3 = (Person) context.getBean("person3");

        System.out.println("merge=default:" + person1);
        System.out.println("merge=false:" + person2);
        System.out.println("merge=true:" + person3);
    }
}

输出结果:
merge=default:Person [id=1, name=aaa, list=[1, 2, 3]]
merge=false:Person [id=1, name=aaa, list=[1, 2, 3]]
merge=true:Person [id=1, name=aaa, list=[1, 2, 3, 1, 2, 3]]

相关文章
最新文章
热点推荐