您的位置:首页 > 编程语言 > Java开发

spring(5)---bean的3种实例化方式

2017-02-08 16:45 267 查看
bean的三种生成方式分别为:类直接生成方式,静态工厂生成方式和实例工厂生成方式示例:1 .新建项目spring_createBean2 .新建com.cn.service包,并在该包下创建IUserService接口.在该接口下创建一个方法saveUser()package com.cn.service;public interface IUserService {public void saveUser();}
3 .新建com.cn.service.impl包,并在该包下创建UserServiceImpl类,该类实现IUserService接口
package com.cn.service.impl;import com.cn.service.IUserService;public class UserServiceImpl implements IUserService{public void saveUser() {System.out.println("调用方法......");}}
4 .在classpath类路径下新建源文件config,在config下创建applicationContext.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的生成方式1:通过类直接生成 --><bean id="userService" class="com.cn.service.impl.UserServiceImpl" /></beans>
5 .编写测试方法
第一种生成方式:类直接生成方式
package com.cn.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.cn.service.IUserService;/*** 通过类直接生成方式创建bean* */public class Test {public static void main(String[] args) {ApplicationContext ctx=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");IUserService userService=(IUserService) ctx.getBean("userService");//调用方法userService.saveUser();}}
第二种方式:静态工厂生成方式
1 .在com.cn.service.impl包下新建UserObject类。在该类种创建一个静态方法
package com.cn.service.impl;import com.cn.service.IUserService;public class UserObject {//静态方法public static IUserService createUser(){return new UserServiceImpl();}}
2 .在applicationContext.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的生成方式1:通过类直接生成注意:class一定要是类而不能是接口,因为接4000口不能被实例化--><bean id="userService" class="com.cn.service.impl.UserServiceImpl" /><!-- bean的生成方式2:通过静态工厂方法生成 --><bean id="userService2" class="com.cn.service.impl.UserObject" factory-method="createUser" /></beans>
3 .测试
package com.cn.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.cn.service.IUserService;public class Test2 {public static void main(String[] args) {ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");IUserService userService=(IUserService) ctx.getBean("userService2");userService.saveUser();}}
第三种方式:实例工厂生成方式
1 .在com.cn.service.impl包下新建UserObject2类
package com.cn.service.impl;import com.cn.service.IUserService;public class UserObject2 {//非静态方法public IUserService createUser(){return new UserServiceImpl();}}
2 .在applicationContext.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的生成方式1:通过类直接生成注意:class一定要是类而不能是接口,因为接口不能被实例化--><bean id="userService" class="com.cn.service.impl.UserServiceImpl" /><!-- bean的生成方式2:通过静态工厂方法生成 --><bean id="userService2" class="com.cn.service.impl.UserObject" factory-method="createUser" /><!-- bean的生成方式3:通过实例工厂方法生成 --><bean id="user3" class="com.cn.service.impl.UserObject2" /><bean id="userService3" factory-bean="user3" factory-method="createUser" /></beans>
3.测试
package com.cn.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.cn.service.IUserService;public class Test3 {public static void main(String[] args) {ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");IUserService userService=(IUserService) ctx.getBean("userService3");userService.saveUser();}}
第二种方式:静态工厂生成方式第三种方式:实例工厂生成方式
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: