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无参有赋值的时候
- 实体
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方法
.......
}
- 配置文件
<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有参有赋值的时候
- 实体
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;
}
}
- 配置文件
<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.第二种:工厂模式
- PersonService接口
public interface PersonService {
public void print();
}
- PersonServiceImpl实现类
public class PersonServiceImpl implements PersonService{
@Override
public void print() {
System.out.println("PersonServiceImpl方法");
}
}
- PersonFactory工厂,夹在PersonService和PersonServiceImpl之间,用来实例化PersonServiceImpl类的
public class PersonFactory{
public PersonService getPersonService() {
return new PersonServiceImpl();
}
}
- 配置文件
<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>
- 测试类
public class PersonTest {
public static void main(String[] args) {
//实例化以一个配置文件
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");
PersonService personService =(PersonServiceImpl) applicationContext.getBean("PersonService");
personService.print();
}
}
3.第三种:静态工厂模式
- PersonService接口
public interface PersonService {
public void print();
}
- PersonServiceImpl实现类
public class PersonServiceImpl implements PersonService{
@Override
public void print() {
System.out.println("PersonServiceImpl方法");
}
}
- PersonFactory2工厂,夹在PersonService和PersonServiceImpl之间,用来实例化PersonServiceImpl类的
public class PersonFactory2 {
public static PersonService getPersonService2(){
return new PersonServiceImpl();
}
}
- 配置文件
<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>
- 测试类
public class PersonTest {
public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");
PersonService personService =(PersonServiceImpl) applicationContext.getBean("PersonService");
personService.print();
}
}