ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

spring-bean实例化

2021-08-14 22:34:09  阅读:173  来源: 互联网

标签:userService spring springframework bean 实例 UserService org public


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

标签:userService,spring,springframework,bean,实例,UserService,org,public
来源: https://www.cnblogs.com/mataoblogs/p/15142068.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有