bean实例化的三种方式实现

第一种:使用类的无参数构造方法创建(常用 重要)

第一种实例化方式最常用,实例化类时会通过调用无参构造方法创建。示例代码如下:

package spring.com.UserService;

public class UserService {

	public UserService() {
//该方法是无参方法
} public void AddUser(){
System.out.println("Add.........................");
}
}

 xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="userService" class="spring.com.UserService.UserService"></bean>
</beans>

 单元测试:

package spring.com.Test;

import static org.junit.Assert.*;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import spring.com.UserService.UserService; public class Test { @org.junit.Test
public void test() {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
UserService s=(UserService) context.getBean("userService");
s.AddUser();
} }

  结果:

方法正常执行

如果手动添加一个有参构造方法时,那么创建时会找不到无参构造方法;示例代码:

package spring.com.UserService;

public class UserService {

	public UserService(String name) {
//该方法是有参方法
} public void AddUser(){
System.out.println("Add.........................");
}
}

  结果:

找不到无参构造方法,所以无法实例化。必须手动添加无参构造方法

package spring.com.UserService;

public class UserService {
public UserService() {
//该方法是无参方法
}
public UserService(String name) {
//该方法是有参方法
} public void AddUser(){
System.out.println("Add.........................");
}
}

  

第二种:使用静态工厂创建(知道即可)

示例代码:

创建一个工厂类,改工厂用来创建一个类的实例

package spring.com.UserService;

public class BeanFactory {
public static UserService getBeanFactory(){
return new UserService();
}
}

 xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="factory" class="spring.com.UserService.BeanFactory" factory-method="getBeanFactory"></bean><!--创建工厂创建对象-->
</beans>

  

单元测试:

package spring.com.Test;

import static org.junit.Assert.*;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import spring.com.UserService.BeanFactory;
import spring.com.UserService.UserService; public class Test { @org.junit.Test
public void test() {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
UserService s=(UserService) context.getBean("factory");
s.AddUser();
} }

  

  结果:

第三种:使用实例工厂创建(知道即可)

示例代码:

package spring.com.UserService;

public class BeanFactory {
//普通工厂方法
public UserService getBeanFactory(){
return new UserService();
}
}

  xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="bean" class="spring.com.UserService.BeanFactory"></bean>
<bean id="factory" factory-bean="bean" factory-method="getBeanFactory"></bean>
</beans>

  单元测试:

package spring.com.Test;

import static org.junit.Assert.*;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import spring.com.UserService.BeanFactory;
import spring.com.UserService.UserService; public class Test { @org.junit.Test
public void test() {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
UserService s=(UserService) context.getBean("factory");
s.AddUser();
} }

  结果:

Spring bean管理器 bean实例化的三种方式的更多相关文章

  1. 【Spring】的【bean】管理(XML配置文件)【Bean实例化的三种方式】

    Bean实例化的三种方式 说明:通过配置文件创建对象就称为Bean实例化. 第一种:使用类的无参构造创建(重点) 实体类 package com.tyzr.ioc; public class User ...

  2. Spring中bean实例化的三种方式

    之前我已经有好几篇博客介绍Spring框架了,不过当时我们都是使用注解来完成注入的,具体小伙伴可以参考这几篇博客(Spring&SpringMVC框架案例).那么今天我想来说说如何通过xml配 ...

  3. Spring的依赖注入(DI)三种方式

    Spring依赖注入(DI)的三种方式,分别为: 1.  接口注入 2.  Setter方法注入 3.  构造方法注入 下面介绍一下这三种依赖注入在Spring中是怎么样实现的. 首先我们需要以下几个 ...

  4. spring学习(03)之bean实例化的三种方式

    bean实体例化的三种方式 在spring中有三中实例化bean的方式: 一.使用构造器实例化:(通常使用的一个方法,重点) 二.使用静态工厂方法实例化: 三.使用实例化工厂方法实例化 第一种.使用构 ...

  5. Spring实现初始化和销毁bean之前进行的操作,三种方式

    关于在spring  容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...

  6. Bean实例化的三种方式

    1. 构造器实例化 spring容器通过bean对应的默认的构造函数来实例化bean. 2. 静态工厂方式实例化 首先创建一个静态工厂类,在类中定义一个静态方法创建实例. 静态工厂类及静态方法: pu ...

  7. Spring AOP编程(二)-AOP实现的三种方式

    AOP的实现有三种方式: l         aop底层将采用代理机制进行实现. l         接口 + 实现类 :spring采用 jdk 的动态代理Proxy. l         实现类: ...

  8. 【spring】 SpringMVC返回json数据的三种方式

    配置方法一 **1.导入第三方的jackson包,jackson-mapper-asl-1.9.7.jar和jackson-core-asl-1.9.7.jar. 2.spring配置文件添加** & ...

  9. Spring配置文件-Bean实例化的三种方式

    1.无参构造方法实例化(详见我的博客) 2.工厂静态方法实例化 创建StaticFactory类 public class StaticFactory { public static UserDao ...

随机推荐

  1. python学习(二十六) 字典

    # 访问字典car = {'make':'bmw', 'model':'550i', 'year':2016}print(car)print(car['make']) # 添加元素d = {}d['o ...

  2. 后台给GridView绑定数据时给每一行添加一个JS方法

    --------JS function ReturnDictionaryValues(srcElement) { top.document.getElementById("_DialogFr ...

  3. springmvc 打包遇到的问题

    1.测试有错误,过不去,mvn install -DSkipTests 2.设置resources路径,在pom.xml中添加 <build> <resources> < ...

  4. small_vector

    folly/small_vector.h folly::small_vector<T,Int=1,...> is a sequence container that implements ...

  5. Nginx加状态监控

    安装Nginx时加上        –with-http_stub_status_module 在nginx.conf server location /nginx_status { stub_sta ...

  6. [Python] WeChat_Robot

    在微信中接入一个聊天机器人 1. WeChat 个人接口itchat 2. 图灵机器人 #-*- coding:utf-8 -*- import itchat import requests apiU ...

  7. [POJ] Bode Plot

    Description Consider the AC circuit below. We will assume that the circuit is in steady-state. Thus, ...

  8. [Z] SVN的trunk、branch、tag

    Subversion有一个很标准的目录结构,是这样的.比如项目是proj,svn地址为svn://proj/,那么标准的svn布局是 svn://proj/|+-trunk+-branches+-ta ...

  9. Rector模式

    讲到高性能IO绕不开Reactor模式,它是大多数IO相关组件如Netty.Redis在使用的IO模式,为什么需要这种模式,它是如何设计来解决高性能并发的呢? 最最原始的网络编程思路就是服务器用一个w ...

  10. 「小程序JAVA实战」swagger2的使用与接口测试(34)

    转自:https://idig8.com/2018/08/31/xiaochengxujavashizhanswagger2deshiyongyujiekouceshi34/ 我们已经开发完了一个用户 ...