博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring----工厂注入和bean的生命周期
阅读量:4659 次
发布时间:2019-06-09

本文共 11920 字,大约阅读时间需要 39 分钟。

独立的集合bean

在多个bean之间可共享该独立bean。

示例:

在其他bean中即可共享该mycars--bean。

P名空间

使用p命名空间时需要先声明使用对应的命名空间,即在beans元素上加入:

xmlns:p="http://www.springframework.org/schema/p"

使用p命名空间的好处是简化配置信息。

示例:

静态工厂注入

 

 使用的是工厂中的静态方法。

在service层引用的dao,1.可以使用原始的方式注入,2.也可以使用静态工厂注入,自己编写一个工厂类,里面静态方法返回一个dao的实现类对象。

静态工厂BinessDAOFactory:

package com.factory;import com.dao.CarDAO;import com.dao.StudentsDAO;import com.dao.impl.CarDAOImpl;import com.dao.impl.StudentsDAOImpl;public class BinessDAOFactory {        //获得学生的DAO实例    public static StudentsDAO getStudentsDAOImplInstance(){        return new StudentsDAOImpl();    }        //获得汽车的DAO实例、    public static CarDAO getCarDAOImplInstance(){        return new CarDAOImpl();    }        //获得用户的DAO实例    /*public static UsersDAO getUsersDAOImplInstance(){        return new UsersDAOImpl();    }*/}

applicationContext.xml:

//静态方法

测试:

@Test    public void fun1(){        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");        Students s=(Students) ctx.getBean("s");        //System.out.println(s);        StudentsServiceImpl sService=(StudentsServiceImpl) ctx.getBean("sService");        sService.add(s);    }

 

 实例工厂注入

工厂:

public class BinessDAOFactory {        //获得学生的DAO实例    public StudentsDAO getStudentsDAOImplInstance(){        return new StudentsDAOImpl();    }}

applicationContext.xml:

测试:

public class StudentServiceImplTest {    @Test    public void fun1(){        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");        Students s=(Students) ctx.getBean("s");        //System.out.println(s);        StudentsServiceImpl sService=(StudentsServiceImpl) ctx.getBean("sService");        sService.add(s);    }}

 bean之间的依赖关系

  在Students类中加上Car car这个属性字段。

学生bean依赖汽车bean,在初始化的时候会先初始化Car。

 

bean的生命周期

 

1.懒加载,在用到该bean的时候,才会去初始化它,加载它。

Students.java:

package com.entity;import java.io.Serializable;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.List;import javax.annotation.PostConstruct;import javax.annotation.Resource;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.stereotype.Component;//加注解注入,起一个别名s1@Component("haha")public class Students implements Serializable {    private String sid;// 学号    private String name;// 姓名    private String gender;// 性别    private Date birthday;// 生日    private String address;// 住址    private Car car;// 学生拥有的车的引用//    private List
cars; //初始化方法,在构造方法之后调用 public void initStudents(){ System.out.println("执行学生的initStudents()方法"); } //在ApplicationContext对象被销毁的时候执行 public void destroyStudents(){ System.out.println("执行学生的destroyStudents()方法"); } public Students(String sid, String name, String gender, Date birthday, String address) { super(); this.sid = sid; this.name = name; this.gender = gender; this.birthday = birthday; this.address = address; } public Students(String sid, String name, String gender, Date birthday, String address, Car car) { super(); this.sid = sid; this.name = name; this.gender = gender; this.birthday = birthday; this.address = address; this.car = car; } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } public Students(String sid, String name, String gender, String address) { this.sid = sid; this.name = name; this.gender = gender; this.address = address; } /*//解决方法1: //在构造方法之后执行一些初始化的操作 @PostConstruct public void init(){ //在调用完构造函数之后,birthday还为null,然后使用这个直接赋值 try { this.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("2000-05-17")); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } }*/ public Students() { System.out.println("一个学生创建啦..."); } public String getSid() { return sid; }// @Value("s0006") public void setSid(String sid) { this.sid = sid; } public String getName() { return name; }// @Value("IU") public void setName(String name) { System.out.println("执行了setName()方法"); this.name = name; } public String getGender() { return gender; }// @Value("女") public void setGender(String gender) { this.gender = gender; } public Date getBirthday() { return birthday; }// @Value("1998-07-15") public void setBirthday(Date birthday) { this.birthday = birthday; } public String getAddress() { return address; }// @Value("韩国首尔") public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Students [sid=" + sid + ", name=" + name + ", gender=" + gender + ", birthday=" + birthday + ", address=" + address + ", car=" + car + "]"; }}

applicationContext.xml:

测试:

public class StudentServiceImplTest {    @Test    public void fun1(){        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");        Students s=(Students) ctx.getBean("s");        Students ss=(Students) ctx.getBean("s");        System.out.println(s==ss);//单例:true prototype:false        //System.out.println(s);        StudentsServiceImpl sService=(StudentsServiceImpl) ctx.getBean("sService");        sService.add(s);                ((ClassPathXmlApplicationContext)ctx).close();    }}

使用注解配置:

Students.java:

package com.entity;import java.io.Serializable;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.List;import javax.annotation.PostConstruct;import javax.annotation.PreDestroy;import javax.annotation.Resource;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.DependsOn;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Component;//加注解注入,起一个别名s1@Component("s")@DependsOn(value="car")//依赖关系@Scope("prototype")//得到的bean对象是否为单例的singleton,prototypepublic class Students implements Serializable {    private String sid;// 学号    private String name;// 姓名    private String gender;// 性别    private Date birthday;// 生日    private String address;// 住址    private Car car;// 学生拥有的车的引用//    private List
cars; //初始化方法,在构造方法之后调用 @PostConstruct//初始化方法 public void initStudents(){ System.out.println("执行学生的initStudents()方法"); } //在ApplicationContext对象被销毁的时候执行 @PreDestroy//销毁方法 public void destroyStudents(){ System.out.println("执行学生的destroyStudents()方法"); } public Students(String sid, String name, String gender, Date birthday, String address) { super(); this.sid = sid; this.name = name; this.gender = gender; this.birthday = birthday; this.address = address; } public Students(String sid, String name, String gender, Date birthday, String address, Car car) { super(); this.sid = sid; this.name = name; this.gender = gender; this.birthday = birthday; this.address = address; this.car = car; } public Car getCar() { return car; } @Resource(name="car") public void setCar(Car car) { this.car = car; } public Students(String sid, String name, String gender, String address) { this.sid = sid; this.name = name; this.gender = gender; this.address = address; } /*//解决方法1: //在构造方法之后执行一些初始化的操作 @PostConstruct public void init(){ //在调用完构造函数之后,birthday还为null,然后使用这个直接赋值 try { this.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("2000-05-17")); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } }*/ public Students() { System.out.println("一个学生创建啦..."); } public String getSid() { return sid; } @Value("s0006") public void setSid(String sid) { this.sid = sid; } public String getName() { return name; } @Value("IU") public void setName(String name) { System.out.println("执行了setName()方法"); this.name = name; } public String getGender() { return gender; } @Value("女") public void setGender(String gender) { this.gender = gender; } public Date getBirthday() { return birthday; } @Value("1998-07-15") public void setBirthday(Date birthday) { this.birthday = birthday; } public String getAddress() { return address; } @Value("韩国首尔") public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Students [sid=" + sid + ", name=" + name + ", gender=" + gender + ", birthday=" + birthday + ", address=" + address + ", car=" + car + "]"; }}

Car.java:

package com.entity;import java.io.Serializable;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;//注解注入,,之后的实例名称叫car@Component("car")public class Car implements Serializable{    private String brand;// 品牌    private String color;// 颜色    public Car() {        System.out.println("一辆汽车诞生啦...");    }    public Car(String brand, String color) {        super();        this.brand = brand;        this.color = color;    }    public String getBrand() {        return brand;    }    @Value("BMW")    public void setBrand(String brand) {        this.brand = brand;    }    public String getColor() {        return color;    }    @Value("黑色")    public void setColor(String color) {        this.color = color;    }    @Override    public String toString() {        return "Car [brand=" + brand + ", color=" + color + "]";    }}

测试:

package com.service.impl;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.AbstractApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.entity.Students;public class StudentServiceImplTest {    @Test    public void fun1(){        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");        Students s=(Students) ctx.getBean("s");        Students ss=(Students) ctx.getBean("s");        System.out.println(s==ss);//单例:true prototype:false        //System.out.println(s);        StudentsServiceImpl sService=(StudentsServiceImpl) ctx.getBean("sService");        sService.add(s);                ((ClassPathXmlApplicationContext)ctx).close();    }}

 

转载于:https://www.cnblogs.com/xjs1874704478/p/11202876.html

你可能感兴趣的文章
luogu11月月赛T3咕咕咕(组合数学)
查看>>
oracle linux 6.5 安装 oracle 12cR2数据库(2)-DBCA建库
查看>>
Codeforces Round #501 (Div. 3)(ABCDE)
查看>>
Oracle怎样标记坏块及一次数据恢复
查看>>
POJ 1502 MPI Maelstrom (dijkstra裸题)
查看>>
【Linux学习四】Linux下Vim命令操作
查看>>
2016 京东校招研发算法题 9.6
查看>>
python学习目录
查看>>
关于always块内for循环的执行方式
查看>>
字符串,列表
查看>>
maven的相关命令
查看>>
原型、作用域、闭包的完整解释
查看>>
Solidworks如何导入和使用模板文件
查看>>
使用命令行 Subversion 访问项目源文件(SVN)
查看>>
Lua学习系列(四)
查看>>
Docker - CentOS 安装 Docker 和 Docker-Compose
查看>>
JVM常用参数
查看>>
开源协议
查看>>
第一个HTML文档
查看>>
一道面试题作为blog的开头
查看>>