java bean工厂_Spring----工厂注入和bean的生命周期

本文详细介绍了Spring框架中Bean的管理,包括单例和原型两种作用域,以及依赖注入的两种方式——静态工厂和实例工厂。通过示例展示了如何在XML配置文件中定义Bean并使用注解进行简化配置。此外,还讨论了Bean的生命周期方法,如初始化和销毁方法,并演示了如何在Bean之间建立依赖关系。最后,通过测试代码验证了Bean的实例化和依赖注入过程。
摘要由CSDN通过智能技术生成

独立的集合bean

f7b6db4be373a8d058862773e5655309.png

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

示例:

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

P名空间

3e95f170235b6135c29cfeb28a9ca726.png

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

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

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

示例:

静态工厂注入

8cd501a9cee1e6c1ba47bd72dbfa00f2.png

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

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

静态工厂BinessDAOFactory:

packagecom.factory;importcom.dao.CarDAO;importcom.dao.StudentsDAO;importcom.dao.impl.CarDAOImpl;importcom.dao.impl.StudentsDAOImpl;public classBinessDAOFactory {//获得学生的DAO实例

public staticStudentsDAO getStudentsDAOImplInstance(){return newStudentsDAOImpl();

}//获得汽车的DAO实例、

public staticCarDAO getCarDAOImplInstance(){return newCarDAOImpl();

}//获得用户的DAO实例

/*public static UsersDAO getUsersDAOImplInstance(){

return new UsersDAOImpl();

}*/}

applicationContext.xml:

//静态方法

测试:

@Testpublic voidfun1(){

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

}

实例工厂注入

df092cb1753f316d4c4767aadcb4d1c5.png

工厂:

public classBinessDAOFactory {//获得学生的DAO实例

publicStudentsDAO getStudentsDAOImplInstance(){return newStudentsDAOImpl();

}

}

applicationContext.xml:

测试:

public classStudentServiceImplTest {

@Testpublic voidfun1(){

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之间的依赖关系

75d9772dfdc86721c4c2cc50c25bad3e.png

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

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

bean的生命周期

164d849e6e1f44033b774b6e6c668626.png

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

Students.java:

packagecom.entity;importjava.io.Serializable;importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.Date;importjava.util.List;importjavax.annotation.PostConstruct;importjavax.annotation.Resource;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.context.annotation.Bean;importorg.springframework.stereotype.Component;//加注解注入,起一个别名s1

@Component("haha")public class Students implementsSerializable {private String sid;//学号

private String name;//姓名

private String gender;//性别

private Date birthday;//生日

private String address;//住址

private Car car;//学生拥有的车的引用//private List cars;//初始化方法,在构造方法之后调用

public voidinitStudents(){

System.out.println("执行学生的initStudents()方法");

}//在ApplicationContext对象被销毁的时候执行

public voiddestroyStudents(){

System.out.println("执行学生的destroyStudents()方法");

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

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

}publicCar getCar() {returncar;

}public voidsetCar(Car car) {this.car =car;

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

}

}*/

publicStudents() {

System.out.println("一个学生创建啦...");

}publicString getSid() {returnsid;

}//@Value("s0006")

public voidsetSid(String sid) {this.sid =sid;

}publicString getName() {returnname;

}//@Value("IU")

public voidsetName(String name) {

System.out.println("执行了setName()方法");this.name =name;

}publicString getGender() {returngender;

}//@Value("女")

public voidsetGender(String gender) {this.gender =gender;

}publicDate getBirthday() {returnbirthday;

}//@Value("1998-07-15")

public voidsetBirthday(Date birthday) {this.birthday =birthday;

}publicString getAddress() {returnaddress;

}//@Value("韩国首尔")

public voidsetAddress(String address) {this.address =address;

}

@OverridepublicString toString() {return "Students [sid=" + sid + ", name=" + name + ", gender=" +gender+ ", birthday=" + birthday + ", address=" + address + ", car="

+ car + "]";

}

}

applicationContext.xml:

测试:

public classStudentServiceImplTest {

@Testpublic voidfun1(){

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:

packagecom.entity;importjava.io.Serializable;importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.Date;importjava.util.List;importjavax.annotation.PostConstruct;importjavax.annotation.PreDestroy;importjavax.annotation.Resource;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.DependsOn;importorg.springframework.context.annotation.Scope;importorg.springframework.stereotype.Component;//加注解注入,起一个别名s1

@Component("s")

@DependsOn(value="car")//依赖关系

@Scope("prototype")//得到的bean对象是否为单例的singleton,prototype

public class Students implementsSerializable {private String sid;//学号

private String name;//姓名

private String gender;//性别

private Date birthday;//生日

private String address;//住址

private Car car;//学生拥有的车的引用//private List cars;//初始化方法,在构造方法之后调用

@PostConstruct//初始化方法

public voidinitStudents(){

System.out.println("执行学生的initStudents()方法");

}//在ApplicationContext对象被销毁的时候执行

@PreDestroy//销毁方法

public voiddestroyStudents(){

System.out.println("执行学生的destroyStudents()方法");

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

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

}publicCar getCar() {returncar;

}

@Resource(name="car")public voidsetCar(Car car) {this.car =car;

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

}

}*/

publicStudents() {

System.out.println("一个学生创建啦...");

}publicString getSid() {returnsid;

}

@Value("s0006")public voidsetSid(String sid) {this.sid =sid;

}publicString getName() {returnname;

}

@Value("IU")public voidsetName(String name) {

System.out.println("执行了setName()方法");this.name =name;

}publicString getGender() {returngender;

}

@Value("女")public voidsetGender(String gender) {this.gender =gender;

}publicDate getBirthday() {returnbirthday;

}

@Value("1998-07-15")public voidsetBirthday(Date birthday) {this.birthday =birthday;

}publicString getAddress() {returnaddress;

}

@Value("韩国首尔")public voidsetAddress(String address) {this.address =address;

}

@OverridepublicString toString() {return "Students [sid=" + sid + ", name=" + name + ", gender=" +gender+ ", birthday=" + birthday + ", address=" + address + ", car="

+ car + "]";

}

}

Car.java:

packagecom.entity;importjava.io.Serializable;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.stereotype.Component;//注解注入,,之后的实例名称叫car

@Component("car")public class Car implementsSerializable{private String brand;//品牌

private String color;//颜色

publicCar() {

System.out.println("一辆汽车诞生啦...");

}publicCar(String brand, String color) {super();this.brand =brand;this.color =color;

}publicString getBrand() {returnbrand;

}

@Value("BMW")public voidsetBrand(String brand) {this.brand =brand;

}publicString getColor() {returncolor;

}

@Value("黑色")public voidsetColor(String color) {this.color =color;

}

@OverridepublicString toString() {return "Car [brand=" + brand + ", color=" + color + "]";

}

}

测试:

packagecom.service.impl;importorg.junit.Test;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.AbstractApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;importcom.entity.Students;public classStudentServiceImplTest {

@Testpublic voidfun1(){

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

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值