大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
FactoryBean+Bean
十余年的天元网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。全网整合营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整天元建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。成都创新互联公司从事“天元网站设计”,“天元网站推广”以来,每个客户项目都认真落实执行。
package com.example.demo10.entity;
//实体
public class Stu {
String name;
public Stu(String name) {
this.name = name;
}
public Stu() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.example.demo10.factory;
import com.example.demo10.entity.Stu;
import org.springframework.beans.factory.FactoryBean;
//创建一个Stu场子
public class StuFactory implements FactoryBean
@Override
public Class> getObjectType() {
return Stu.class;
}
@Override
public boolean isSingleton() {
return false;
}
@Override
public Stu getObject() throws Exception {
return new Stu();
}
}
package com.example.demo10.configuration;
import com.example.demo10.factory.StuFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class StuConfiguration {
//创建场子Bean
//这里要注意返回的是场子对象,但是正真获取的是Stu对象
@Bean
public StuFactory stuFactory(){
return new StuFactory();
}
}
package com.example.demo10;
import com.example.demo10.entity.Stu;
import com.example.demo10.factory.StuFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class Demo10Application {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(Demo10Application.class, args);
//获取场子里面的产品
Stu stu1 = context.getBean("stuFactory", Stu.class);
Stu stu2 = context.getBean("stuFactory", Stu.class);
System.out.println(stu1.getClass().getName());
System.out.println(stu1.hashCode());
System.out.println(stu2.getClass().getName());
System.out.println(stu2.hashCode());
//获取工厂自己
StuFactory stuFactory = context.getBean("&stuFactory", StuFactory.class);
System.out.println(stuFactory.getClass().getName());
context.close();
}
}