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();}}第二种方式:静态工厂生成方式第三种方式:实例工厂生成方式
相关文章推荐
- Spring2.2——实例化Bean的3种方式
- spring容器创建对象的3种方式(bean的实例化)
- spring容器创建对象的3种方式(bean的实例化)
- Spring 三种实例化bean的方式
- spring内部创建对象的方式(或bean实例化方式)
- Spring 三种实例化Bean的方式
- spring的三种实例化bean方式
- Spring容器Bean的实例化方式和声明周期
- Spring中 三种实例化bean的方式
- spring三种实例化bean的方式
- spring IOC容器实例化Bean的方式与RequestContextListener应用
- Spring-装配Bean的3种方式-实现工厂方法方式装配Bean
- spring三种实例化bean的方式
- Spring 实例化bean的三种方式:
- 传智播客 Spring中bean的实例化方式与IOC容器总结
- Spring中实例化Bean的三种方式
- 传智播客spring学习,实例化bean的方式及Bean的作用域
- spring三种实例化bean的方式
- Spring-装配Bean的3种方式-默认构造函数方式装配Bean
- java_spring_实例化bean的3种方法