spring的三种实例方式

本文详细介绍了Spring中三种实例化对象的方式:1.通过构造器,包括无参和有参构造的情况;2.利用工厂模式创建对象;3.使用静态工厂模式。每种方式都配以实体、配置文件和测试类的说明。
摘要由CSDN通过智能技术生成

1.第一种:构造方式

1.1无参构造的情况

1.1.1无参无赋值的时候

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context" 
       xmlns:aop="http://www.springframework.org/schema/aop"      
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
	
	<!-- 无参的情况 -->
	<bean id="person2" class="com.cwj.spring.bean.Person2"></bean>
</beans>

1.1.2无参有赋值的时候

  1. 实体
public class Student {
    private Long id;
    private String name;
    private URL website;
    private List<String> aihao;
    private Integer age;
    private Date birthday;
    private boolean isNew;
    private Set<String> phones;
    private Map<String, Integer> scores;
    private Locale locale;//en_US
    private File photo;
    //下面是set方法和toString方法
    .......
}
  1. 配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context" 
       xmlns:aop="http://www.springframework.org/schema/aop"      
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

	<!-- 第一种:构造方式 -->
	<!-- 无参的情况 -->
	<bean id="person2" class="com.cwj.spring.bean.Person2"></bean>
	<!-- 有参的情况 -->
	<bean id="student" class="com.cwj.spring.bean.Student">
		<property name="id" value="12"></property>
		<property name="name" value="张三"></property>
		<property name="website" value="https://www.baidu.com/"></property>
		<property name="aihao">
			<list>
				<value>学习</value>
				<value>内卷</value>
			</list>
		</property>
		<property name="age" value="12"></property>
		<property name="birthday" value="2021/10/23"></property>
		<property name="isNew" value="true"></property>
		<property name="phones">
			<set>
				<value>110</value>
				<value>120</value>
				<value>119</value>
			</set>
		</property>
		<property name="scores">
			<map>
				<entry key="k1" value="11"></entry>
				<entry key="k2" value="22"></entry>
			</map>
		</property>
		<property name="locale" value="China"></property>
		<property name="photo" value="beans.xml"></property>
	</bean>
</beans>

1.1.3有参有赋值的时候

  1. 实体
public class Student {
   private Long id;
   private String name;
   private URL website;
   private List<String> aihao;
   private Integer age;
   private Date birthday;
   private boolean isNew;
   private Set<String> phones;
   private Map<String, Integer> scores;
   private Locale locale;//en_US
   private File photo;

   public Student() {
   }

   public Student(Long id, String name) {
       this.id = id;
       this.name = name;
   }
}
  1. 配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context" 
       xmlns:aop="http://www.springframework.org/schema/aop"      
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

    <!-- 有参构造方法 -->
	<bean id="student2" class="com.cwj.spring.bean.Student">
		<constructor-arg index="0" value="12"></constructor-arg>
		<constructor-arg index="1" value="lishi"></constructor-arg>
	</bean>
</beans>

2.第二种:工厂模式

  1. PersonService接口
public interface PersonService {
    public void print();
}
  1. PersonServiceImpl实现类
public class PersonServiceImpl implements PersonService{
    @Override
    public void print() {
        System.out.println("PersonServiceImpl方法");
    }
}
  1. PersonFactory工厂,夹在PersonService和PersonServiceImpl之间,用来实例化PersonServiceImpl类的
public class PersonFactory{
    public PersonService getPersonService() {
        return new PersonServiceImpl();
    }
}
  1. 配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context" 
      xmlns:aop="http://www.springframework.org/schema/aop"      
      xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
     <!-- 第二种:工厂模式(先实例化工厂,再调用工厂里面的类) -->
     <bean id="personFactory" class="com.cwj.spring.factory.PersonFactory"></bean>
   <bean id="PersonService" factory-bean="personFactory" factory-method="getPersonService"></bean>
</beans>
  1. 测试类
public class PersonTest {
    public static void main(String[] args) {		
		//实例化以一个配置文件
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");
        PersonService personService =(PersonServiceImpl) applicationContext.getBean("PersonService");
        personService.print();
    }
}

3.第三种:静态工厂模式

  1. PersonService接口
public interface PersonService {
    public void print();
}
  1. PersonServiceImpl实现类
public class PersonServiceImpl implements PersonService{
    @Override
    public void print() {
        System.out.println("PersonServiceImpl方法");
    }
}
  1. PersonFactory2工厂,夹在PersonService和PersonServiceImpl之间,用来实例化PersonServiceImpl类的
public class PersonFactory2 {
    public static PersonService getPersonService2(){
        return new PersonServiceImpl();
    }
}
  1. 配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context" 
       xmlns:aop="http://www.springframework.org/schema/aop"      
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
	<!-- 第三种:静态工厂模式 -->
  <bean id="PersonService" class="com.cwj.spring.factory.PersonFactory2" factory-method="getPersonService2"></bean>
 </beans>
  1. 测试类
 public class PersonTest {
    public static void main(String[] args) {
		ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");
        PersonService personService =(PersonServiceImpl) applicationContext.getBean("PersonService");
        personService.print();
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值