首页 > 编程语言 > 详细

spring-bean实例化

时间:2021-08-14 23:47:15      阅读:30      评论:0      收藏:0      [点我收藏+]

bean的实例化有3种方式:

方式一:
默认构造方式:表示调用类的默认构造方法创建bean对象。

<bean  id=""  class=""></bean>

目录结构:

技术分享图片

  创建UserService:

package com.spring02.beanbox;

public class UserService {
public void saveUser() throws Exception{
System.out.println("UserService.saveUser()");
}
}

创建bean.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="UserService" class="com.spring02.beanbox.UserService"></bean>
</beans>

创建测试类:

package com.spring02.beanbox;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestDemo {
@Test
public void test() throws Exception{
ApplicationContext ac = new ClassPathXmlApplicationContext("com/spring02/beanbox/bean.xml");
UserService userService = (UserService) ac.getBean("UserService");
userService.saveUser();
}
}


方式二:
静态工厂方式:有一个工厂类,在类中所有方法都是静态的,作用生产目标类对象。创建工厂类,定义静态方法生产需要的bean对象。
配置:<bean. id="". class="静态工厂bean". factory-method="工厂方法"></bean>

目录结构:
技术分享图片

 

 

创建UserService:

 

package com.spring02.beanbox;

public class UserService {
public void saveUser() throws Exception{
System.out.println("UserService.saveUser()");
}
}

 

 

创建静态工厂bean:MyStaticFactoryBean

package com.spring02.beanbox;

public class MyStaticFactoryBean {
private static UserService userService;
public static UserService createUserService(){
if (userService == null){
userService = new UserService();
}
return userService;
}
}

创建bean.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="UserService2" class="com.spring02.beanbox.MyStaticFactoryBean" factory-method="createUserService"></bean>
</beans>

创建测试类:
package com.spring02.beanbox;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestDemo {
@Test
public void test() throws Exception{
ApplicationContext ac = new ClassPathXmlApplicationContext("com/spring02/beanbox/bean.xml");
UserService userService = (UserService) ac.getBean("UserService2");
userService.saveUser();
}
}


方式三:

实例工厂方式:有一个工厂类,在类中所有方法都是成员方法,作用生产目标类对象。创建工厂类,定义成员方法生产需要的bean对象。
配置文件中:先配置示例工厂bean,再利用工厂bean生产目标bean对象。
<bean id="" factory-bean="" factory-method=""></bean>

 技术分享图片

 

创建UserService:

package com.spring02.beanbox;

public class UserService {

public void saveUser() throws Exception{
System.out.println("UserService3.saveUser()");
}
}

创建实例工厂MyObjectFactoryBean:

package com.spring02.beanbox;

public class MyObjectFactoryBean {
private static UserService userService;

public UserService createUserService(){
if(this.userService == null){
this.userService = new UserService();
}
return userService;
}
}


 创建bean.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- 配置工厂bean -->
<bean id="MyFactoryBean" class="com.spring02.beanbox.MyObjectFactoryBean"></bean>

  <!-- 利用工厂bean生产目标bean -->
<bean id="UserService3" factory-bean="MyFactoryBean" factory-method="createUserService"></bean>
</beans>

 创建测试类:

package com.spring02.beanbox;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestDemo {
@Test
public void test() throws Exception{
ApplicationContext ac = new ClassPathXmlApplicationContext("com/spring02/beanbox/bean.xml");
UserService userService = (UserService) ac.getBean("UserService3");
userService.saveUser();
}
}

spring-bean实例化

原文:https://www.cnblogs.com/mataoblogs/p/15142068.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!