您的位置:首页 > 编程语言 > Java开发

Spring 从零开始-02

2015-07-04 10:14 357 查看
上一篇看到了Spring的简单例子,现在继续。

注入bean有三种方式

①setter注入

②构造方法注入

③注解方式注入

setter方法最一般的方法,也是上一篇博客使用的方法,即对类中的属性注入使用其封装的setter方法,对应上一个例子就是setAge(),所以一定记住编写setter方法。

构造器的方式,即使用类中的构造方法,来个例子

[code]public class User {
    private String name;
    private int age;

    public User(String name,int age){
        this.name = name;
        this.age = age;
    } 
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}


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

        //  User user = new User();
        //  user.setAge(10);
        //  System.out.println(user.getAge());
        ApplicationContext ac = new ClassPathXmlApplicationContext("blog2/bean.xml");
        User user = (User)ac.getBean("user");
        System.out.println(user.getName());
        System.out.println(user.getAge());

    }
}


[code]<bean id="user" class="blog2.User">
        <constructor-arg value="spirit"/>
        <constructor-arg value="12"/>
    </bean>


构造器需要使用constructor-arg来指定构造器的参数。

第三种方式注解方式比较繁琐,现在先不解释,以后再来。

再来说一下bean的作用域,共有5个选择singleton、prototype、request、session、global-session。其中只有前两个是现在可以学习的,剩下的都是编写web后台时才会使用到的。

bean的默认属性是singleton,意思就是一个bean只有一个实例化对象,有点像Java的单例,而prototype则相反。默认也就是singleton的情况下看一下这个代码

[code]ApplicationContext ac = new ClassPathXmlApplicationContext("blog2/bean.xml");
        User user = (User)ac.getBean("user");
        User user2 = (User) ac.getBean("user");
        user.setAge(15);
        System.out.println(user.getName());
        System.out.println(user.getAge());
        System.out.println(user2.getAge());


(由于使用的构造器进行注入,所以改动了一下age以示区别)user2的age还是15,而指定prototype

[code]<bean id="user" class="blog2.User" scope="prototype">
        <constructor-arg value="spirit"/>
        <constructor-arg value="12"/>
    </bean>
``
这时user2的age就是12了,很简单。
注意这里的单例和Java的单例不一样,这只是说在这个特定的应用上下文ApplicationContext ac中user名称的bean只会有一个实例。
如果需要在实例化bean之前需要做很多事情,并且在销毁bean的时候也要做一些事情,那么就需要好好学习一下bean的生命周期,这个有机会在后面深入讲解,现在先了解一下简单的。在bean的配置中为我们提供了init-method和destroy-method,分别表示在实例化前和销毁时做的事情


public class User {

private String name;

private int age;

[code]public void born(){
    System.out.println("user born");
}

public void death(){
    System.out.println("user death");
}

public User(String name,int age){
    this.name = name;
    this.age = age;
} 
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public int getAge() {
    return age;
}
public void setAge(int age) {
    this.age = age;
}
}


[code]```
<bean id="user" class="blog2.User" scope="prototype"
        init-method="born" destroy-method="death">
        <constructor-arg value="spirit"/>
        <constructor-arg value="12"/>
    </bean>


如果定义全局的init和des方法可以这样

[code]<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans.xsd      http://www.springframework.org/schema/aop      http://www.springframework.org/schema/aop/spring-aop.xsd"      default-init-method="born"
     default-destroy-method="death">

    <bean id="user" class="blog2.User" scope="prototype">
        <constructor-arg value="spirit"/>
        <constructor-arg value="12"/>
    </bean>
    <!--  
    <bean id="before" class="blog2.beforeMethod"/>
    <bean id="after" class="blog2.afterMethod"/>
     -->


当然你还可以使用两个接口来实现InitializingBean和DisposableBean,并实现接口的方法

[code]public class beforeMethod implements InitializingBean{

    @Override
    public void afterPropertiesSet() throws Exception {
        // TODO Auto-generated method stub
        System.out.println("before");
    }

}


[code]public class afterMethod implements DisposableBean{

    @Override
    public void destroy() throws Exception {
        // TODO Auto-generated method stub
        System.out.println("after");
    }

}


当然user类需要继承这两个类!一定记住。

Spring除了注入一些简单的Java类型之外还可以在bean中注入其他的bean,在user中加入一个education的类

[code]public class Education {
    private String junior;
    private String senior;
    private String college;

    public void selfIntro(){
        System.out.println("初中"+this.junior+"高中"+this.senior+"大学"+this.college);
    }
    public String getJunior() {
        return junior;
    }
    public void setJunior(String junior) {
        this.junior = junior;
    }
    public String getSenior() {
        return senior;
    }
    public void setSenior(String senior) {
        this.senior = senior;
    }
    public String getCollege() {
        return college;
    }
    public void setCollege(String college) {
        this.college = college;
    }

}


[code]public class User extends beforeMethod{
    private String name;
    private int age;
    private Education edu;

    public Education getEdu() {
        return edu;
    }

    public void setEdu(Education edu) {
        this.edu = edu;
    }

    public void born(){
        System.out.println("user born");
    }

    public void death(){
        System.out.println("user death");
    }

//  public User(String name,int age){
//      this.name = name;
//      this.age = age;
//  } 
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}


[code]<bean id="user" class="blog2.User" >
        <property name="name" value="spirit"></property>
        <property name="age" value="23"></property>
        <property name="edu" ref="education"></property>
    </bean>

    <bean id="education" class="blog2.Education" >
        <property name="junior" value="汉光"></property>
        <property name="senior" value="一中"></property>
        <property name="college" value="西电"></property>
    </bean>


主要看bean文件使用了ref引用装配了id为education的bean

这里除了使用ref引用,还可以定义内部bean(类似于内部类)。

[code]    <bean id="user" class="blog2.User" >
        <property name="name" value="spirit"></property>
        <property name="age" value="23"></property>
        <property name="edu">
            <bean class="blog2.Education">
                <property name="junior" value="汉光"></property>
                <property name="senior" value="一中"></property>
                <property name="college" value="西电"></property>
            </bean>
        </property>
    </bean>


内部类也可以使用构造器的方式,但是内部bean不能被其他类引用,所以内部类完全没有必要定义id。

说的蛮多的了。。还是上代码吧

http://download.csdn.net/detail/wsrspirit/8867867
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: