Spring工厂模式
作者:互联网
Spring工厂模式
//实体类
package com.msb.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Demo1 {
private String bname;
private String author;
}
创建类继承FactoryBean
package com.msb.pojo;
import org.springframework.beans.factory.FactoryBean;
public class BookFactory implements FactoryBean<Demo1> {
@Override
public Demo1 getObject() throws Exception {
Demo1 demo1 =new Demo1();
demo1.setAuthor("新设置");
demo1.setBname("设置名字");
return demo1;
}
@Override
public Class<?> getObjectType() {
return null;
}
}
spring.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="Demo1" class="com.msb.pojo.BookFactory"></bean>
</beans>
测试方法
import com.msb.pojo.Demo1;
import javafx.application.Application;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test2 {
@Test
public void test1(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext1.xml");
Demo1 demo1 = context.getBean("Demo1",Demo1.class);
System.out.println(demo1);
}
}
标签:demo1,Spring,模式,工厂,Demo1,context,org,import,public 来源: https://www.cnblogs.com/188221creat/p/16373069.html