绍兴网站的优化,动画设计图,迪奥网络营销方式,大地资源免费视频观看目录 一、spring容器之bean的实例化。 #xff08;1#xff09;bean基本概念。 #xff08;2#xff09;spring-bean实例化的几种方式。 二、spring容器使用构造方法的方式实例化bean。 #xff08;1#xff09;无参构造方法实例化bean。 #… 目录 一、spring容器之bean的实例化。 1bean基本概念。 2spring-bean实例化的几种方式。 二、spring容器使用构造方法的方式实例化bean。 1无参构造方法实例化bean。 2有参构造方法实例化bean。 1、新建一个类Student并交给spring容器管理。 2、使用子标签constructor-arg完成bean配置。 3、有参构造方法的参数为多个时(index与value)。 4、标签constructor-arg内使用name属性。 3使用场景。 本篇博客的主要内容是使用(构造方法或静态工厂)实现spring-bean实例化。
一、spring容器之bean的实例化。 1bean基本概念。 spring框架中总是有bean这个词出现它的本质上就是对象。spring容器管理的对象叫bean。 在Java基础的学习中创建对象通常都是使用new构造方法。对应spring容器来说它也是可以通过构造方法完成bean的创建 2spring-bean实例化的几种方式。 二、spring容器使用构造方法的方式实例化bean。 1无参构造方法实例化bean。 注意每个类会默认提供一个无参构造方法。就算未写也是调用了无参构造方法。但如果手动提供了有参构造方法一般一定记得再手动提供无参构造方法。spring容器是可以通过无参构造方法实例化bean的。下面通过demo(案例)进行演示。 spring的简单demo项目的结构组成与介绍。 spring配置文件。目前只配置了UserDaoImpl的bean ?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd!--生产UserDao实现类的对象--bean iduserDao classcom.fs.dao.impl.UserDaoImpl/bean
/beans UserDao接口。 package com.fs.dao;
//UserDao接口
public interface UserDao {void add();
}UserDaoImpl实现类。 package com.fs.dao.impl;import com.fs.dao.UserDao;
//UserDao接口的实现类
public class UserDaoImpl implements UserDao {//手动添加无参构造方法public UserDaoImpl() {System.out.println(UserDaoImpl无参构造方法执行了);}//实现UserDao接口中的add方法Overridepublic void add() {System.out.println(UserDaoImpl执行了add方法);}
}程序的测试类。 package com.fs.test;import com.fs.dao.impl.UserDaoImpl;
import org.springframework.context.support.ClassPathXmlApplicationContext;//运行测试程序
public class MainApp {public static void main(String[] args) {//使用IoC容器(ApplicationContext)获取spring容器管理的bean对象//1.创建容器对象。实例化时参数指定对应的配置文件ClassPathXmlApplicationContext context new ClassPathXmlApplicationContext(springConfig.xml);//2.通过bean标签的唯一标识id获取对应UserDao接口的实现类UserDaoImpl的实例对象Object obj context.getBean(userDao);//3.强制类型转换UserDaoImpl userDao (UserDaoImpl)obj;userDao.add();}
} demo的运行结果。 如果将无参构造方法public设置成private权限spring容器还能够帮忙实例化对象吗 在以前的new构造方法时显然是不能够的但是spring容器却可以无论提供的无参构造方法是公共的还是私有的spring容器都能够调用到该无参构造方法。 这就是涉及到spring容器内部底层工作原理——反射机制。这个后面再详细学习现在只需要知道spring容器是可以拿构造方法实例化bean就行了。 是否可以直接不做任何操作让spring容器使用有参构造方法实例化bean 答案是不行的。因为spring创建的bean的时候是默认调用无参构造方法。 查看spring的报错信息可以一层一层的往上分析。 2有参构造方法实例化bean。 package com.fs.a;public class Student {}上面得demo中spring容器默认使用无参构造方法实例化bean时。当把无参构造方法变成有参构造方法不仅仅程序中会报错xml文件中也会报错 1、新建一个类Student并交给spring容器管理。 package com.fs.a;public class Student {
//类中提供一个有参构造方法public Student(String name){System.out.println(参数是name);}
}这时像原先通过无参构造方法完成bean实例化的spring配置文件已经报错因为此时只提供了有参构造方法而未提供无参构造方法。 2、使用子标签constructor-arg完成bean配置。 bean标签中的子标签constructor-arg用于指定构造函数参数。这样以便在spring容器创建bean时传递给相应的构造函数。 value属性的值就是给对应有参构造方法的参数变量赋值。 !--配置Student类的对象--bean idstudent classcom.fs.a.Studentconstructor-arg valuezhangsan//bean 测试类MainApp02代码。也是一样的使用ApplicationContext容器的加载spring配置文件与getBean()拿取spring容器管理的对象(Student类)。 package com.fs.test;import com.fs.a.Student;
import org.springframework.context.support.ClassPathXmlApplicationContext;//测试类2
public class MainApp02 {public static void main(String[] args) {//使用IoC容器(ApplicationContext)获取spring容器管理的bean对象//1.创建容器对象。实例化时参数指定对应的配置文件ClassPathXmlApplicationContext context new ClassPathXmlApplicationContext(springConfig.xml);//2.通过bean标签的唯一标识id获取对应UserDao接口的实现类UserDaoImpl的实例对象Object obj context.getBean(student);//3.强制类型转换Student student (Student)obj;System.out.println(student);}
}测试运行 3、有参构造方法的参数为多个时(index与value)。 修改Student类的有参构造方法。 package com.fs.a;public class Student {
//类中提供一个有参构造方法public Student(String name,int age){System.out.println(参数是name,年龄是age);}
}此时spring的配置文件又出现了报错 在子标签constructor-arg中除了给value属性赋值外还需要指定参数的位置(索引)属性index的值这样一一对应了有参构造方法的参数值。 ?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd!--生产UserDao实现类的对象bean iduserDao classcom.fs.dao.impl.UserDaoImpl/bean--!--配置Student类的对象--bean idstudent classcom.fs.a.Studentconstructor-arg index0 value李四/constructor-arg index1 value18//bean/beans 此时再运行测试类MainApp02程序查看结果。 若给int类型的age赋值一个字符串spring配置文件中也会报错提示。 删去bean标签内对应的配置constructor-arg。就会报错没有默认的无参构造No default constructor found 4、标签constructor-arg内使用name属性。 用name属性指定有参构造方法的参数就不需要像index属性那样需要按顺序去赋值value属性。 ?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd!--生产UserDao实现类的对象bean iduserDao classcom.fs.dao.impl.UserDaoImpl/bean--!--配置Student类的对象--!--bean idstudent classcom.fs.a.Studentconstructor-arg namename valuewangwu/constructor-arg nameage value18//bean--bean idstudent classcom.fs.a.Studentconstructor-arg nameage value18/constructor-arg namename valuewangwu//bean/beans 3使用场景。 当我们使用第三方的技术时将它们也交给spring容器进行管理。我们学会了无参构造与有参构造方法实例化bean时就可以直接使用spring容器管理并获得bean对象。本篇博客对于博主来说还有待完善。